Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UserConrol上没有显示条形码_C#_Vb.net_Visual Studio 2012 - Fatal编程技术网

C# UserConrol上没有显示条形码

C# UserConrol上没有显示条形码,c#,vb.net,visual-studio-2012,C#,Vb.net,Visual Studio 2012,我决定在C#中转换VB.Net(已下载)中的生成器条形码。有两个项目MSBarcodeCS(这里是表单)和Shtrikh_kod(这里是UserControl)。表单上应显示UserConrol,但不显示。别告诉我有什么问题?还是有错误 来源: UserControl1.cs: using System; using System.ComponentModel; using System.Collections.Generic; using System.Drawing; using Syste

我决定在C#中转换VB.Net(已下载)中的生成器条形码。有两个项目MSBarcodeCS(这里是表单)和Shtrikh_kod(这里是UserControl)。表单上应显示UserConrol,但不显示。别告诉我有什么问题?还是有错误

来源:

UserControl1.cs:

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace Shtrikh_kod
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        #region "Consts"
        const string StartMark = "101";
        const string SplittingMark = "01010";
        const string EndMark = "101";
        #endregion

        #region "Declarations"
        private struct Ean13Tables
        {
            public string TableA;
            public string TableB;
            public string TableC;
        }
        private Ean13Tables[] Tables = new Ean13Tables[10];
        private StringBuilder BarcodeValue;
        #endregion



        private double M_BarWidth;
        //[ComponentModel.Category("Appearance"), ComponentModel.Description("rrrrrr")]
        public double BarWidth
        {
            get { return M_BarWidth; }
            set { M_BarWidth = value; }
        }
        private double M_BarHeight;
        //[ComponentModel.Category("Appearance"),
        //ComponentModel.Description("rrrrrr")]
        public double BarHeight
        {
            get { return M_BarHeight; }
            set { M_BarHeight = value; }
        }

        private string m_BarcodeText;
        //[ComponentModel.Category("Barcode"), ComponentModel.Description("The Barcode Value" + Constants.vbCrLf + "must consist of 12 digit"), 
        //ComponentModel.RefreshProperties(ComponentModel.RefreshProperties.All)]
        public string Value
        {
            //Overrides
            get { return m_BarcodeText; }
            set
            {
                if ((value.Length == 12) & (Information.IsNumeric(value)))
                {
                    m_BarcodeText = value;
                    this.Refresh();
                }
                else if ((value.Length != 12))
                {
                    InitBarcode();
                    throw new Exception("EAN13 Barcode Must Consist Of 12 Digits");
                }
                else if (!(Information.IsNumeric(value)))
                {
                    InitBarcode();
                    throw new Exception("EAN13 Barcode Must Consist Of Digits Only");

                }
            }
        }

        private bool m_ShowBarcodeText;
        //[ComponentModel.Category("Barcode"), ComponentModel.Description("show Barcode value under barcode bars"), 
        //ComponentModel.RefreshProperties(ComponentModel.RefreshProperties.All)]
        public bool ShowBarcodeText
        {
            //Overrides
            get { return m_ShowBarcodeText; }
            set
            {
                m_ShowBarcodeText = value;
                this.Refresh();
            }
        }

        private byte m_CheckSum;
        //[ComponentModel.Category("Barcode"), 
        //ComponentModel.Description("Check Digit Value")]
        public byte CheckSum
        {
            get
            {
                CalculateCheckSum();
                return m_CheckSum;
            }
        }

        public void New()
        {
            this.ResizeRedraw = true;
            InitBarcode();
            InitEAN13Tables();
            M_BarWidth = 0.33;
            // mm
            this.Font = new Font("Arial", 18);
            // This call is required by the Windows Form Designer.
            InitializeComponent();
            // Add any initialization after the InitializeComponent() call.
        }

        #region "Init Procedures"

        private void InitBarcode()
        {
            m_BarcodeText = "000000000000";
        }

        public void InitEAN13Tables()
        {
            //          Zero
            Tables[0].TableA = "0001101";
            Tables[0].TableB = "0100111";
            Tables[0].TableC = "1110010";
            //          One
            Tables[1].TableA = "0011001";
            Tables[1].TableB = "0110011";
            Tables[1].TableC = "1100110";
            //          Two
            Tables[2].TableA = "0010011";
            Tables[2].TableB = "0011011";
            Tables[2].TableC = "1101100";
            //          Three
            Tables[3].TableA = "0111101";
            Tables[3].TableB = "0100001";
            Tables[3].TableC = "1000010";
            //          Four
            Tables[4].TableA = "0100011";
            Tables[4].TableB = "0011101";
            Tables[4].TableC = "1011100";
            //          Five
            Tables[5].TableA = "0110001";
            Tables[5].TableB = "0111001";
            Tables[5].TableC = "1001110";
            //          Six
            Tables[6].TableA = "0101111";
            Tables[6].TableB = "0000101";
            Tables[6].TableC = "1010000";
            //          Seven
            Tables[7].TableA = "0111011";
            Tables[7].TableB = "0010001";
            Tables[7].TableC = "1000100";
            //          Eight
            Tables[8].TableA = "0110111";
            Tables[8].TableB = "0001001";
            Tables[8].TableC = "1001000";
            //          Nine
            Tables[9].TableA = "0001011";
            Tables[9].TableB = "0010111";
            Tables[9].TableC = "1110100";

        }
        #endregion

        private bool CalculateCheckSum()
        {
            int X = 0;
            int Y = 0;
            int j = 11;
            try
            {
                for (int i = 1; i <= 12; i++)
                {
                    if (i % 2 == 0)
                    {
                        X += Conversion.Val(m_BarcodeText[j]);
                    }
                    else
                    {
                        X += Conversion.Val(m_BarcodeText[j]);
                    }
                    j -= 1;
                }

                int Z = X + (3 * Y);
                double m_CheckSum = ((10 - (Z % 10)) % 10);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        private void CalculateValue()
        {
            // Clear any previous Value
            BarcodeValue = new StringBuilder(95);
            try
            {
                // Add The Start Mark
                BarcodeValue.Append(StartMark);
                switch (m_BarcodeText[0])
                {
                    case '0':
                        for (int i = 1; i <= 6; i++)
                        {
                            BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                        }

                        break;
                    case '1':
                        for (int i = 1; i <= 6; i++)
                        {
                            if ((i == 1) | (i == 2) | (i == 4))
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                            }
                            else
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableB);
                            }
                        }

                        break;
                    case '2':
                        for (int i = 1; i <= 6; i++)
                        {
                            if ((i == 1) | (i == 2) | (i == 5))
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                            }
                            else
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableB);
                            }
                        }

                        break;
                    case '3':
                        for (int i = 1; i <= 6; i++)
                        {
                            if ((i == 1) | (i == 2) | (i == 6))
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                            }
                            else
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableB);
                            }
                        }

                        break;
                    case '4':
                        for (int i = 1; i <= 6; i++)
                        {
                            if ((i == 1) | (i == 3) | (i == 4))
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                            }
                            else
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableB);
                            }
                        }

                        break;
                    case '5':
                        for (int i = 1; i <= 6; i++)
                        {
                            if ((i == 1) | (i == 4) | (i == 5))
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                            }
                            else
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableB);
                            }
                        }

                        break;
                    case '6':
                        for (int i = 1; i <= 6; i++)
                        {
                            if ((i == 1) | (i == 5) | (i == 6))
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                            }
                            else
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableB);
                            }
                        }

                        break;
                    case '7':
                        for (int i = 1; i <= 6; i++)
                        {
                            if ((i == 1) | (i == 3) | (i == 5))
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                            }
                            else
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableB);
                            }
                        }

                        break;
                    case '8':
                        for (int i = 1; i <= 6; i++)
                        {
                            if ((i == 1) | (i == 3) | (i == 6))
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                            }
                            else
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableB);
                            }
                        }

                        break;
                    case '9':
                        for (int i = 1; i <= 6; i++)
                        {
                            if ((i == 1) | (i == 4) | (i == 6))
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableA);
                            }
                            else
                            {
                                BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableB);
                            }
                        }

                        break;
                }
                // Add The Splitting Mark
                BarcodeValue.Append(SplittingMark);
                for (int i = 7; i <= (m_BarcodeText.Length - 1); i++)
                {
                    BarcodeValue.Append(Tables[Conversion.Val(m_BarcodeText[i])].TableC);
                }
                // Add Checksum
                BarcodeValue.Append(Tables[CheckSum].TableC);
                // Add The End Mark
                BarcodeValue.Append(EndMark);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //return CalculateValue();
        }

        private void DrawBarcodeText(PaintEventArgs e)
        {

            // Create font and brush. 
            //Dim drawFont As New Font("Arial", 18)
            dynamic drawFont = this.Font;
            SolidBrush drawBrush = new SolidBrush(this.ForeColor);

            // Create rectangle for drawing. 
            float x = 3.61F;
            float y = (30 + (5 * 0.33f));
            //31.4F

            // Create string to draw. 
            String drawString = m_BarcodeText;
            //If ShowCheckSum = True Then
            drawString += CheckSum.ToString();
            x -= 1.2f;
            //End If

            // Measure string. 
            SizeF stringSize = new SizeF();
            stringSize = e.Graphics.MeasureString(drawString, drawFont);

            RectangleF drawRect = new RectangleF(x, y, stringSize.Width, stringSize.Height);

            // Set format of string. 
            StringFormat drawFormat = new StringFormat();
            drawFormat.Alignment = StringAlignment.Center;


            // Draw string to screen.
            e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
        }

        private void EAN13Barcode_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            CalculateValue();
            // Change the page scale.  
            e.Graphics.PageUnit = GraphicsUnit.Millimeter;
            // Dim units As GraphicsUnit = GraphicsUnit.Millimeter
            float s = 3;
            for (int i = 0; i <= 94; i++)
            {
                if (BarcodeValue[i] == '1')
                {
                    switch (i)
                    {
                        // Case 94
                        //    e.Graphics.FillRectangle(New SolidBrush(Me.ForeColor), s + 0.2F, 10, 0.33F, (15 + (5 * 0.33F)))
                        case 0:
                        case 1:
                        case 2:
                        case 45:
                        case 46:
                        case 47:
                        case 48:
                        case 49:
                        case 92:
                        case 93:
                        case 94:
                            e.Graphics.FillRectangle(new SolidBrush(this.ForeColor), s + 0.11f, 10, 0.5f, (20 + (5 * 0.33f)));
                            break;
                        default:
                            e.Graphics.FillRectangle(new SolidBrush(this.ForeColor), s + 0.11f, 10, 0.5f, 20);
                            break;
                    }
                }
                else if (BarcodeValue[i] == '0')
                {
                    switch (i)
                    {
                        case 0:
                        case 1:
                        case 2:
                        case 45:
                        case 46:
                        case 47:
                        case 48:
                        case 49:
                        case 92:
                        case 93:
                        case 94:
                            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), s + 0.11f, 10, 0.5f, (20 + (5 * 0.33f)));
                            break;
                        default:
                            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), s + 0.11f, 10, 0.5f, 20);
                            break;
                    }
                }

                s += 0.5f;
            }

            //if (ShowBarcodeText == true)
                DrawBarcodeText(e);
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {

        }
        }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace MSBarcodeCS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

                userControl11.Value= textBox1.Text;
}
}

您的EAN13Barcode_Paint函数将永远不会被调用

我认为这是因为VB.net处理事件的方式,如果您查看VB.net代码,在函数定义的末尾必须有一个“handles Paint”,它告诉编译器将控件的Paint事件挂接到该函数

在初始化组件之后,将其添加到UserControl1构造函数中:

this.Paint += EAN13Barcode_Paint;

摆脱使用该代码调用InitializeComponent两次的
public void New()
。将所需的行移到
public UserControl1()
块中。还不清楚你是否已经把油漆事件联系起来了。无论哪种方式,最好使用OnPaint替代。在您的值设置中也调用
this.Invalidate()
,以调用绘制消息。需要移动哪些行?我假设除InitializeComponent之外的所有行都已存在。这不起作用,因为某些内容为空,请使用异常和生成它的行更新问题。以及行…还有,新函数代码必须放在类的构造函数中,因为VB上的新函数是它的构造函数。这些代码太乱了,使用自动转换器或者学习VB是如何工作的。我还问了哪一行产生了错误,但你没有告诉我…为什么它这么复杂?与其使用自定义控件手动进行渲染,不如在文本框上使用EAN13标准的字体,如下图所示:我很多年前就使用过这种字体,没有任何问题……不,你不明白我的意思,我的意思是“如果你可以在不编码的情况下使用字体实现相同的结果,为什么要让创建自定义控件的生活变得复杂”;)