我决定用C#中的VB.Net(下载)转换生成器条形码。 MSBarcodeCS(此处为form)和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)
{
}
}
}
这是Form1.cs:
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;
}
}
答案 0 :(得分:0)
永远不会调用您的EAN13Barcode_Paint函数。
我认为这是因为VB .net处理事件的方式,如果你看一下vb .net代码,在函数定义的末尾必须是一个“Handles Paint”,它告诉编译器钩住Paint事件对该功能的控制。
在InitializeComponent
之后将其添加到UserControl1构造函数:
this.Paint += EAN13Barcode_Paint;