【发布时间】:2021-12-11 13:37:50
【问题描述】:
我正在编写应用程序,它不仅可以从组合框生成条形码,而且可以将条形码扫描到文本框中。生成条码运行良好,但使用 Device Zebra DS3608 扫描时效果稍差。在这个项目中,我添加了一个库:
using USB_Barcode_Scanner;
然后我在公共方法中添加和修改:
public PrintLabel()
{
InitializeComponent();
textBox1.Focus();
BarcodeScanner barcodeScanner = new BarcodeScanner(textBox1);
barcodeScanner.BarcodeScanned += BarcodeScanner_BarcodeScanned;
Fillcombox();
}
并且自动生成了新方法。
private void BarcodeScanner_BarcodeScanned(object sender, BarcodeScannerEventArgs e)
{
textBox1.Text = e.Barcode;
}
经过此修改后,它编译正确,但在启动它而不是专注于文本框后,我尝试扫描,但它从按钮开始并扫描到下面的组合框是应用程序的图像:
我怎样才能改变它?它属于此条码扫描仪设置还是此代码?也许下面的代码有帮助:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Zen.Barcode;
using USB_Barcode_Scanner;
namespace IT_equipment_program
{
public partial class PrintLabel : Form
{
MySqlConnection connection;
MySqlCommand command;
MySqlDataReader dr;
public PrintLabel()
{
InitializeComponent();
textBox1.Focus();
BarcodeScanner barcodeScanner = new BarcodeScanner(textBox1);
barcodeScanner.BarcodeScanned += BarcodeScanner_BarcodeScanned;
Fillcombox();
}
private void BarcodeScanner_BarcodeScanned(object sender, BarcodeScannerEventArgs e)
{
textBox1.Text = e.Barcode;
}
void Fillcombox()
{
try
{
string con = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
MySqlConnection SelectConnection = new MySqlConnection(con);
string Selectquery = "SELECT DISTINCT Equipment FROM equipments";
MySqlCommand com = new MySqlCommand(Selectquery, SelectConnection);
SelectConnection.Open();
MySqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
cmb_Equip.Items.Add(dr.GetString("Equipment"));
}
SelectConnection.Close();
}
catch
{
MessageBox.Show("please check internet connection.");
}
}
void Fillcombox2()
{
try
{
string conn2 = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
string query2 = "SELECT IT_Equipments_No FROM equipments WHERE Equipment = @EQUIP";
using (connection = new MySqlConnection(conn2))
{
using (command = new MySqlCommand(query2, connection))
{
command.Parameters.AddWithValue("@EQUIP", cmb_Equip.Text);
connection.Open();
command.ExecuteNonQuery();
dr = command.ExecuteReader();
while (dr.Read())
{
cmb_IT_Equip_NO.Items.Add(dr.GetString("IT_Equipments_No"));
}
connection.Close();
}
}
}
catch
{
MessageBox.Show("Please check internet connection.");
}
}
private void btn_exit_Click(object sender, EventArgs e)
{
Close();
}
private void cmb_Equip_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string conn3 = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
string query3 = "SELECT Equipment FROM equipments WHERE Equipment = @EQUIP";
using (connection = new MySqlConnection(conn3))
{
using (command = new MySqlCommand(query3, connection))
{
command.Parameters.AddWithValue("@EQUIP", cmb_Equip.Text);
connection.Open();
command.ExecuteNonQuery();
dr = command.ExecuteReader();
while (dr.Read())
{
string name = (string)dr["Equipment"].ToString();
txt_equip.Text = name;
cmb_IT_Equip_NO.SelectedIndex = -1;
}
cmb_IT_Equip_NO.Enabled = true;
cmb_IT_Equip_NO.Items.Clear();
Fillcombox2();
connection.Close();
}
}
}
catch
{
MessageBox.Show("Please check internet connection.");
}
}
private void cmb_IT_Equip_NO_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string conn4 = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
string query4 = "SELECT IT_Equipments_No FROM equipments WHERE IT_Equipments_No = @EQUIP_NO";
using (connection = new MySqlConnection(conn4))
{
using (command = new MySqlCommand(query4, connection))
{
command.Parameters.AddWithValue("@EQUIP_NO", cmb_IT_Equip_NO.Text);
connection.Open();
command.ExecuteNonQuery();
dr = command.ExecuteReader();
while (dr.Read())
{
string number = (string)dr["IT_Equipments_No"].ToString();
txt_IT_NO.Text = number;
}
btn_generate.Enabled = true;
connection.Close();
}
}
}
catch
{
MessageBox.Show("Please check internet connection.");
}
}
private void btn_init_Click(object sender, EventArgs e)
{
cmb_IT_Equip_NO.SelectedIndex = -1;
cmb_Equip.SelectedIndex = -1;
cmb_IT_Equip_NO.Items.Clear();
cmb_Equip.Items.Clear();
btn_Print.Enabled = false;
btn_generate.Enabled = false;
cmb_IT_Equip_NO.Enabled = false;
txt_equip.Text = string.Empty;
txt_IT_NO.Text = string.Empty;
Fillcombox();
}
private void btn_generate_Click(object sender, EventArgs e)
{
Code128BarcodeDraw Barcode = BarcodeDrawFactory.Code128WithChecksum;
pictureBox1.Image = Barcode.Draw(txt_IT_NO.Text, 50);
btn_Print.Enabled = true;
}
private void btn_Print_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
PrintDocument pDoc = new PrintDocument();
pDoc.PrintPage += PrintPicture;
pd.Document = pDoc;
if (pd.ShowDialog() == DialogResult.OK)
{
pDoc.Print();
}
}
private void PrintPicture(object sender, PrintPageEventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
e.Graphics.DrawImage(bmp, 0, 0);
bmp.Dispose();
}
}
}
提前谢谢你。
【问题讨论】:
-
你根本不需要图书馆,扫描仪将充当键盘
-
你能显示你的 USB_Barcode_Scanner 命名空间的代码吗?避免键盘/焦点问题的一种方法是使用虚拟 COM 驱动程序。另一种方法是将扫描仪配置为在条形码数据之前发送特定的键序列。但是如果没有看到 USB_Barcode_Scanner,就很难说出它想要做什么。
-
@user700390 我从这个链接下载了这个 dll 文件:jumpshare.com/v/OaBfkKCNr8duXzMXoakB 并添加到我的项目的引用中。
-
如果您没有源代码或文档,避免使用该库可能更容易。扫描仪是否配置为使用键盘或串行/虚拟 COM 端口接口?
-
@user700390 配置为只扫描任意条码然后输入key再扫描。
标签: c# textbox barcode scanning