创建C#Hang标签4x12

时间:2017-02-07 17:26:35

标签: c# sql winforms

我想创建一个标签页面,如附图所示。条形码与标签“PT01-0000011”具有相同的描述,并添加*,因此“* PT01-0000011 *”。

到目前为止,问题是我希望第二个标签有“PT01-0000012”,第三个“PT01-0000013”......“PT01-00000XX”。

enter image description here

我留下数据库的脚本,并使用C#。

  

SQL

USE [Etiquetas]
GO


    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE TABLE [dbo].[Etiquetas](
        [ID] [varchar](10) NULL,
        [NInicialEtiqueta] [varchar](10) NULL,
        [NFinalEtiqueta] [varchar](10) NULL,
        [StringEtiqueta] [varchar](20) NULL
    ) ON [PRIMARY]

INSERT INTO [dbo].[Etiquetas]
           ([ID]
           ,[NInicialEtiqueta]
           ,[NFinalEtiqueta]
           ,[StringEtiqueta])
     VALUES
           ('PT01'
           ,'000001'
           ,null
           ,'PT01-000001')

GO
  

C#

namespace EtiquetasECM
{
    public partial class Form2 : Form
    {
        public Form2(string CBarras, string label)
        {
            InitializeComponent();
            CodigoBarras.Text = CBarras;
            label1.Text = label;
            getValues();

        }


        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            // g.DrawRectangle(Pens.Black, 1, 1, 180, 95);

            Font fBody1 = new Font("Free 3 of 9 Extended", 22, FontStyle.Regular);
            Font fBody2 = new Font("Urania Czech", 12, FontStyle.Regular);
            Font rs = new Font("Urania Czech", 5, FontStyle.Bold);


            /* para imprimir um so objeto
              g.DrawString(CodigoBarras.Text, fBody1, sb, 3, 15);
                g.DrawString(label1.Text, fBody2, sb, 20, 40);
             */


            SolidBrush sb = new SolidBrush(Color.Black);

            int npaginas = Convert.ToInt32(textBox1.Text);          // numero de paginas a imprimir
            int codigoBarrasPosicaox = 3;
            int codigoBarrasPosicaoy = 28;
            int labelCodigoBarrasPosicaox = 28;
            int labelCodigoBarrasPosicaoy = 55;
            int retanguloPosicaox = 1;
            int retanguloPosicaoy = 22;
            // float scale = Convert.ToInt32((5.2));
            // label2.Text = Convert.ToString(scale);


            for (int i = 0; i < npaginas; i++) //Numero de paginas
            {
                for (int x = 0; x < 4; x++)
                {   //Numero de objetos em X
                    string myConnectionString = "Integrated Security=SSPI;Persist Security Info=False;User ID=COTESI/dafonso;Initial Catalog=Etiquetas;Data Source = SERVSQL;"; //you connectionstring goes here

                    SqlCommand cmd1 = new SqlCommand("SELECT NInicialEtiqueta FROM Etiquetas WHERE id ='" + label1.Text + "'", new SqlConnection(myConnectionString));
                    // SqlCommand cmd = new SqlCommand("SELECT NInicialEtiqueta FROM Etiquetas WHERE id ='PT01'", new SqlConnection(myConnectionString));
                    cmd1.Connection.Open();
                    label2.Text = cmd1.ExecuteScalar().ToString(); // assign to your label



                    //primeira fila
                    g.DrawRectangle(Pens.Black, retanguloPosicaox, 22, 174, 90);
                    g.DrawString(CodigoBarras.Text, fBody1, sb, codigoBarrasPosicaox, codigoBarrasPosicaoy);
                    g.DrawString(label1.Text + "-" + label2.Text, fBody2, sb, labelCodigoBarrasPosicaox, labelCodigoBarrasPosicaoy);
                   // codigoBarrasPosicaox += codigoBarrasPosicaox;
                    codigoBarrasPosicaox += 206;
                    labelCodigoBarrasPosicaox += 206;
                    retanguloPosicaox += 206;
                    retanguloPosicaoy += 100;
                    cmd1.Connection.Close();

                    for (int y = 0; y < 10; y++)    //Numero de objetos em Y
                    {
                        SqlCommand cmd2 = new SqlCommand("SELECT NInicialEtiqueta FROM Etiquetas WHERE id ='" + label1.Text + "'", new SqlConnection(myConnectionString));
                        // SqlCommand cmd = new SqlCommand("SELECT NInicialEtiqueta FROM Etiquetas WHERE id ='PT01'", new SqlConnection(myConnectionString));
                        cmd2.Connection.Open();
                        label2.Text = cmd2.ExecuteScalar().ToString(); // assign to your label
                        g.DrawRectangle(Pens.Black, 1, retanguloPosicaoy, 174, 90);
                        g.DrawString(CodigoBarras.Text, fBody1, sb, 3, codigoBarrasPosicaoy);
                        g.DrawString(label1.Text + "-" + label2.Text+1 , fBody2, sb, 28, labelCodigoBarrasPosicaoy);

                        //primeira coluna
                        g.DrawRectangle(Pens.Black, retanguloPosicaox, retanguloPosicaoy, 174, 90);
                        g.DrawString(CodigoBarras.Text, fBody1, sb, codigoBarrasPosicaox, codigoBarrasPosicaoy);
                        g.DrawString(label1.Text + "-" + label2.Text+1, fBody2, sb, labelCodigoBarrasPosicaox, labelCodigoBarrasPosicaoy);

                        //terceira coluna
                        g.DrawRectangle(Pens.Black, retanguloPosicaox + 206, retanguloPosicaoy, 174, 90);
                        g.DrawString(CodigoBarras.Text, fBody1, sb, codigoBarrasPosicaox +206, codigoBarrasPosicaoy);
                        g.DrawString(label1.Text + "-" + label2.Text+1, fBody2, sb, labelCodigoBarrasPosicaox +206, labelCodigoBarrasPosicaoy);

                        //quarta coluna
                        g.DrawRectangle(Pens.Black, retanguloPosicaox + retanguloPosicaox + 206, retanguloPosicaoy, 174, 90);
                        g.DrawString(CodigoBarras.Text, fBody1, sb, codigoBarrasPosicaox + 412, codigoBarrasPosicaoy);
                        g.DrawString(label1.Text + "-" + label2.Text+1, fBody2, sb, labelCodigoBarrasPosicaox + 412, labelCodigoBarrasPosicaoy);

                        label2.Text += 1;
                        retanguloPosicaoy += 100;
                        codigoBarrasPosicaoy += 100;
                        labelCodigoBarrasPosicaoy += 100;

                        cmd2.Connection.Close();
                    }

                }

            }

        }


        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            PrintDocument pd = new PrintDocument();
            PaperSize ps = new PaperSize("", 2100, 2970);

            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);

            pd.PrintController = new StandardPrintController();
            pd.DefaultPageSettings.Margins.Left = 0;
            pd.DefaultPageSettings.Margins.Right = 0;
            pd.DefaultPageSettings.Margins.Top = 0;
            pd.DefaultPageSettings.Margins.Bottom = 0;

            pd.DefaultPageSettings.PaperSize = ps;
            pd.Print();

        }


        private void getValues()
        {

            string myConnectionString = "Integrated Security=SSPI;Persist Security Info=False;User ID=COTESI/dafonso;Initial Catalog=Etiquetas;Data Source = SERVSQL;"; //you connectionstring goes here

            SqlCommand cmd = new SqlCommand("SELECT NInicialEtiqueta FROM Etiquetas WHERE id ='" + label1.Text + "'", new SqlConnection(myConnectionString));
            // SqlCommand cmd = new SqlCommand("SELECT NInicialEtiqueta FROM Etiquetas WHERE id ='PT01'", new SqlConnection(myConnectionString));
            cmd.Connection.Open();
            label2.Text = cmd.ExecuteScalar().ToString(); // assign to your label
            cmd.Connection.Close();


        }

    }
}

1 个答案:

答案 0 :(得分:0)

您需要将标签的一部分解析为int

var currentLabel = int.Parse(cmd1.ExecuteScalar()。ToString());

并且在您需要时使用它而不是label2.Text,因为它具有string值。 每当你需要再次将其转换为sting时,我猜你需要一个前导空值,以便你可以使用

currentLabel.ToString( “D7”)