條碼掃描程序

在條碼掃描應用的開發過程中,一個重要的步驟是獲取掃描槍所掃描的條碼,並將條碼存儲在數據庫中。

條碼有一定的限制條件:

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

2、條碼長度爲8位。

3、條碼首位爲字母,後7位爲數字。

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

瞭解到掃描槍同鍵盤相似,能夠觸發OnKeyPressOnKeyDownOnKeyUp事件,一般的掃描槍,在掃描完條碼後會默認自動加個“回車”。比如掃描“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)
{
// 判斷是否爲回車鍵,是回車鍵才能執行
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;
// 執行數據庫保存程序
// ......
}
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
'判斷是否爲回車鍵,是回車鍵才能執行
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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章