2015-12-01 68 views
0

我是新的vb.net,目前我正在開發一個條碼掃描器系統,用戶將在其上掃描文本框上的條形碼。如果它是新的條形碼,它將被插入數據庫並顯示在gridview中,否則recheckIn列將更新爲當前日期,並且CheckOut列將清空。問題是我做的後端代碼是OnButtonclick事件。無需點擊按鈕,如何使用條碼掃描器執行功能?例如,如果用戶點擊兩次條形碼掃描器,它會在網格視圖中顯示插入的條形碼列表,如果三次它將更改結帳按鈕的顏色並將結帳列更新爲gridview上的當前日期。vb.net中的條碼掃描器點擊功能

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 [email protected]" 
      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 [email protected]" 
        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 

User Interface

回答

2

這一切真的取決於條碼掃描器,有些人就可以直接查詢COM接口和別人是像一個鍵盤(有很多可配置兩種方式),HID接口。

我會認爲它是一個普通的通用HID接口。其中大多數可以配置一個SUFFIX代碼,也可以發送。

你想要做的是確保SUFFIX代碼編程有數據,你可以很容易地檢測(通常是一個TAB或ENTER鍵)。

將後綴編入條形碼閱讀器後,您將希望使用文本框的KeyPress或KeyDown事件,並且一旦檢測到SUFFIX代碼,就會觸發要執行的操作。

對於你列出的場景,我喜歡使它具有自己的功能,並讓Button和Key(按下/下)事件調用該函數。

0

我經常檢測到條形碼掃描儀輸入的方式是使用TextChanged事件。如果某人正在鍵入一個值,每次按鍵都會得到這個事件,但是如果他們正在使用掃描儀,您將在一次掃描中獲得一次該事件。這意味着您可以查看文本之前的內容和現在的內容,並確定它是否來自條形碼掃描儀,而不是手動輸入。

這也將對付粘貼操作,但在我眼中,他們應該像掃描一樣工作。獎金!