Hey guys,

I am having trouble with keypress event for a button that I have.
This button is programmed to run a function to add data to the database when the button is pressed.

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        funcAdd()

    End Sub

    Private Sub btnAdd_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles btnAdd.KeyPress

        funcAdd()


    End Sub

    Public Function funcAdd()
        If txtModel.Text = Nothing Or txtSerial.Text = Nothing Or txtControlID.Text = Nothing Then
            MessageBox.Show("Please complete required fields", "Incomplete Fields", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Return Nothing
            Exit Function
        ElseIf txtControlID.TextLength <> 10 Then
            MessageBox.Show("Invalid Control ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return Nothing
            Exit Function
        End If
        'Creates and instance where the function funcAddItem is called.
        'Values that are entered into the database is passed onto the function as parameter
        Try
            shipCls.funcAddItem(txtOutID.Text, txtModel.Text, txtSerial.Text, txtControlID.Text)
            Me.SelectViewByItemTableAdapter.FillBy(Me.GCR_ICDataSet.SelectViewByItem, txtOutID.Text)
            Me.SelectViewByModelTableAdapter.Fill(Me.GCR_ICDataSet.SelectViewByModel, txtOutID.Text)
        Catch ex As Exception
            MsgBox("A problem was detected while adding data" & vbCrLf & _
                   "Please contact system administrator for further assistance", MsgBoxStyle.OkOnly, "System Error")
        End Try

       
        DataGridView_ByItem.Refresh()

        txtModel.Text = Nothing
        txtSerial.Text = Nothing
        txtControlID.Text = Nothing

        txtModel.Focus()

        Return Nothing

    End Function

Currently all of the data that are entered is done by using a barcode scanner, and when the user need to press button to execute function, the user need to come to the computer after each item is scanned to press or click btnAdd.
But I want the user to be able to scan an extra barcode which will then register as keypress event and activate the button.
The problem is our scanner guns are programmed to tab over to next index after each scan so when I scan an extra barcode, the program will activate the button but also same time print rest of the scanned characters in the next index tab with is a textbox.
I was wondering if there is a way for the event to excute only when tab is pressed.

Attachments aftBtnPress.png 84.63 KB bfrBtnPress.png 81.51 KB

If you do find a way to run the code when TAB is pressed, wouldn't that mean that your code would run after every scan?
Why don't you check if the text entered is equal to the barcode you plan the users to scan for finishing the data entry? Just make sure that the barcode is a text imposible to find in real life.

now my question is how do i create a function which will only execute when tab key is pressed while the event handler is selected or highlighted?

I tried MSDN and other forums but I could not find anything on how to activate on keys.tab.

What do you mean by "only execute when tab key is pressed while the event handler is selected or highlighted" ?

Right now, while the button is highlighted, the function associated with the button is executed with any key stroke by the user (button.keypress). So when the user scans an extra barcode, it reads the text not as a string but as single characters which means the first character on the barcode executes the button.keypress command and rest of the characters from the barcode is then entered into a textbox which is next tab index.
I want my applicatioin to ignore any keypress when tab index gets to the button except for tab key because all of our scan gun are programmed so that it adds a tab key at the end of each scan.

You can create a sub that will handle button.keydown, which will check if the button has the focus and if e.keycode <> vbKeyTab then suppress it.
You can read about it here: http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.suppresskeypress.aspx

If I am correct you are building a checkout process. Since time is of the essence, why do you need the button to run the function? Why don't you use the textbox.lostfocus event to run your function after the last textbox has been populated?

Anyway, you have to take into account that users working with barcode scanners tend to forget to check the screen. You might want your program to beep them an error sound, in order to alert them that something went wrong with their scanning.

Thanks for your help. I tried keypress sub but it seems like whenever tab is pressed, it will skip over to the next tab index so no function nor event runs when the tab key is pressed.
Frustrated....

I the tab seems to work with lostfocus function but I wont be sure until I connect to the database and test it. The database that I've been working on is offline due to poor maintainence... sigh...

I am very frustrated with Server Problems and VB problems. Anyone here familiar with SBS 2003 and its components such as SQL and sharepoint?

With a little help, my sql server is back up and running. I got the scanner gun to work using lostfocus but the catch is when there is an error, it will enter this infinite loop and the application crashes, which I think I can handle myself.
I want to thank AdamK for this input.

This question has already been answered. Start a new discussion instead.