vb.net中的条码扫描器点击功能
我是新的vb.net,目前我正在开发一个条形码扫描系统,用户将扫描文本框上的条形码。如果是新条形码,它将被插入数据库并显示在gridview中,否则Recheck列将更新为当前日期,CheckOut列将为空。问题是我所做的后端代码是OnButtonclick事件。如何在不点击按钮的情况下使用条形码扫描仪执行功能?例如,如果用户单击条形码扫描仪两次,它将在gridview上显示插入的条形码列表;如果单击三次,它将更改签出按钮的颜色,并将签出列更新为gridview上的当前日期vb.net中的条码扫描器点击功能,vb.net,Vb.net,我是新的vb.net,目前我正在开发一个条形码扫描系统,用户将扫描文本框上的条形码。如果是新条形码,它将被插入数据库并显示在gridview中,否则Recheck列将更新为当前日期,CheckOut列将为空。问题是我所做的后端代码是OnButtonclick事件。如何在不点击按钮的情况下使用条形码扫描仪执行功能?例如,如果用户单击条形码扫描仪两次,它将在gridview上显示插入的条形码列表;如果单击三次,它将更改签出按钮的颜色,并将签出列更新为gridview上的当前日期 Imports Sy
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If Label1.Visible = True Then
Label1.Visible = False
ElseIf Label1.Visible = False Then
Label1.Visible = True
End If
If TextBox2.Visible = True Then
TextBox2.Visible = False
ElseIf TextBox2.Visible = False
TextBox2.Visible = True
End If
Button1.Style.Add(HtmlTextWriterStyle.Color, "green")
End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim con As SqlConnection = New SqlConnection("Data Source=EQUALIZER;Initial Catalog=Barcodeapp;Integrated Security=True")
Dim cmd1 As SqlCommand = New SqlCommand("Select * from barscan", con)
con.Open()
If TextBox1.Text = "" Then
MsgBox("Please fill-up fields!", MsgBoxStyle.Exclamation, "Add LOTID!")
Else
Dim theQuery As String = "SELECT Barcode FROM barscan WHERE Barcode=@Barcode"
Dim cmd2 As SqlCommand = New SqlCommand(theQuery, con)
cmd2.Parameters.AddWithValue("@Barcode", TextBox1.Text)
Using reader As SqlDataReader = cmd2.ExecuteReader()
If reader.HasRows Then
Dim theQuery2 As String = "Update barscan SET RecheckIn=GETDATE() WHERE Barcode=@Barcode"
Dim cmd3 As SqlCommand = New SqlCommand(theQuery, con)
cmd3.Parameters.AddWithValue("@Barcode", TextBox1.Text)
Else
Dim connectionString As String = "Data Source=EQUALIZER;Initial Catalog=Barcodeapp;Integrated Security=True"
Using cn As New SqlConnection(connectionString)
cn.Open()
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO barscan (Date,Barcode,Location,CheckIn) VALUES(@Date,@Barcode,@Location,@CheckIn)"
Dim param3 As New SqlParameter()
param3.ParameterName = "@Date"
param3.Value = Date.Today()
cmd.Parameters.Add(param3)
Dim param1 As New SqlParameter()
param1.ParameterName = "@Barcode"
param1.Value = TextBox1.Text.Trim()
cmd.Parameters.Add(param1)
Dim param2 As New SqlParameter()
param2.ParameterName = "@Location"
param2.Value = TextBox2.Text.Trim()
cmd.Parameters.Add(param2)
Dim param4 As New SqlParameter()
param4.ParameterName = "@CheckIn"
param4.Value = Date.Today()
cmd.Parameters.Add(param4)
cmd.Connection = cn
cmd.ExecuteNonQuery()
cn.Close()
End Using
If barapp.Visible = True Then
barapp.Visible = False
ElseIf barapp.Visible = False Then
barapp.Visible = True
End If
End If
End Using
con.Close()
End If
End Sub
End Class
这完全取决于条形码扫描器,有些是可以直接查询的COM接口,有些是像键盘一样的HID接口(很多都可以通过任何一种方式进行配置) 我将假定它是一个通用的HID接口。其中大多数都可以配置一个后缀代码,该代码也会被发送 您要做的是确保后缀代码是用您可以轻松检测到的数据编程的(通常是TAB键或ENTER键) 一旦您将后缀编程到条形码阅读器中,您将希望使用文本框的按键或按键事件,一旦它检测到后缀代码,就会触发要采取的操作
对于您列出的场景,我喜欢将操作设置为自己的功能,并让按钮和按键(按下/按下)事件调用该功能。我经常检测条形码扫描仪输入的方法是使用
TextChanged
事件。如果有人在中键入值,则每次按键都会出现此事件,但如果他们使用扫描仪,则每次扫描都会出现一次此事件。这意味着,您可以查看文本以前是什么,现在是什么,并确定这是否来自条形码扫描仪,而不是手动输入
这也将对粘贴操作起作用,但在我看来,它们的工作原理应该与扫描相同。奖金