溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

條碼掃描程序

發(fā)布時間:2020-05-26 18:22:34 來源:網(wǎng)絡(luò) 閱讀:2963 作者:mamaosheng 欄目:編程語言

在條碼掃描應(yīng)用的開發(fā)過程中,一個重要的步驟是獲取掃描槍所掃描的條碼,并將條碼存儲在數(shù)據(jù)庫中。

條碼有一定的限制條件:

1、條碼類型為一維條碼。

2、條碼長度為8位。

3、條碼首位為字母,后7位為數(shù)字。

只有符合條件的條碼,例如:A1234567,才能獲得通過。

了解到掃描槍同鍵盤相似,能夠觸發(fā)OnKeyPress、OnKeyDown、OnKeyUp事件,一般的掃描槍,在掃描完條碼后會默認(rèn)自動加個“回車”。比如掃描“A1234567”,實際是返回“A1234567回車”。明白了這一點,編碼就很簡單了。

 

c#:

using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.ute1.Focus();
}
private void ute1_KeyPress(object sender, KeyPressEventArgs e)
{
// 判斷是否為回車鍵,是回車鍵才能執(zhí)行
if (e.KeyChar == 13)
{
// 獲取文本框中的最后8為字符
string barcode = ute1.Text.Substring(ute1.Text.Length - 8, 8).ToUpper();
// 檢查8位字符是否符合要求
if (!BarcodeUtility.CheckBarcode(barcode))
{
// 符合要求的8位條碼
ul.Text = barcode;
// 執(zhí)行數(shù)據(jù)庫保存程序
// ......
}
else
{
ul.Text = "條碼掃描錯誤";
}
// 將文本框1清除
ute1.Clear();
ute1.Focus();
}
}
}
}

 

 

VB.NET:

Imports WindowsApplication1.Class1
Public Class BarcodeChcek
Private Sub BarcodeChcek_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Focus()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
'判斷是否為回車鍵,是回車鍵才能執(zhí)行
If e.KeyCode = 13 Then
'獲取文本框中的最后8為字符
Dim barcode As String
barcode = Microsoft.VisualBasic.Right(Me.TextBox1.Text, 8)
'檢查8位字符是否符合要求
If mCodeCheck(barcode) = True Then
Me.Label1.Text = "條碼掃描錯誤"
Else
'符合要求的8位條碼
Me.Label1.Text = barcode
End If
'將文本框1清除
Me.TextBox1.Clear()
Me.TextBox1.Focus()
End If
End Sub
End Class

 

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI