Hi,

I have a form with a datagrid and a textbox (to put the question in most simplistic way) and I like to keep textbox focused all the time to enable barcode scanner (as a keyboard wedge) entry. When the user clicks on some datagrid cells, I open another form for data entry and after they finish, I ilke the textbox focused again. I use mousedown event to detect user interaction and set focus with mouseup event.
Code:
    Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown 
        Debug.Write(vbCrLf) 
        Dim myGrid As DataGrid = CType(sender, DataGrid) 
        Dim hti As System.Windows.Forms.DataGrid.HitTestInfo 
        hti = myGrid.HitTest(e.X, e.Y) 
        If hti.Type = DataGrid.HitTestType.Cell Then 
            Select Case hti.Column 
                Case 1 
                    Debug.Write("Case1 ") 
                Case 2 
                    Debug.Write("Case2 ") 
                    Dim frm As New KeypadNumeric 
                    frm.ShowDialog() 
                    'DataGrid1.CurrentCell = New DataGridCell(0, 0) 'did not help 
                    'Button11.PerformClick() 'did not help 
            End Select 
        End If 
        Debug.Write("before focus:" & TextBox1.Focus.ToString & " ") 
        TextBox1.Select() 
        TextBox1.Focus() 
        Debug.Write("after focus:" & TextBox1.Focus.ToString & " ") 
    End Sub 

    Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp 
        Debug.Write("Mouse Up Before focus:" & TextBox1.Focus.ToString & " ") 
        TextBox1.Focus() 
        Debug.Write("Mouse Up After focus:" & TextBox1.Focus.ToString & " ") 
    End Sub 

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click 
        TextBox1.Focus() 
    End Sub
Although the debug message tells me that the textbox is in focus, I can not see the cursor flashing in the textbox after a second form is showed and closed. Below is the debug print:
Code:
Case1 before focus:True after focus:True Mouse Up Before focus:True Mouse Up After focus:True 
Case2 before focus:True after focus:True 
before focus:True after focus:True Mouse Up Before focus:True Mouse Up After focus:True
the third line is printed after clicking a non-cell area on the datagrid. First and third lines focus the taxtbox but not the second one (case2)

To investigate the matter further, I put two commented out senteces to force it but they did not help either. To make the matter worse, if I delete the mouseup event, I never get textbox focused (cursor blinking in the textbox). But If I make the datagrid read only, I can get the textbox focused on every second click on datagrid. It is really bizzare. My conclusion is that I have to have a mouseup event to set the focus on textbox but that event never gets fired if a form (or even a msgbox) is opened in mousedown event. As a matter of fact, I do not understand why should I need a mouseup event but after bashing my head to walls, I am happy to use a workaround as long as I am able to focus on the textbox after opening and closing a form in mousedown event handler.

The closest question to my problem is by R.Sole ("Problem with SetFocus to a Text Box") in this forum but I do not think he has resolved it either. Both of our observations are quite the same.

I believe the question is very challenging and requires someone with a lot of experience or knowledge to resolve it. I appreciate your help.

Regards
ps: I am using VB.NET V1.1