0

I am working on a Windows Form via VB.net. The form contains two textboxes and an onscreen keyboard that I have programmed myself.

How can I determine which textbox the cursor is currently in, so that the onscreen keyboard will type in the correct textbox?

The application is meant to be entirely touch based.

DeanOC
  • 7,142
  • 6
  • 42
  • 56
  • I'm sorry I can't check because I'm not at a computer but I believe you're looking for If Textbox1.Focused = true then – Malcor Apr 12 '15 at 21:57
  • This is already answered here : http://stackoverflow.com/questions/5629171/how-to-check-focused-textbox-in-vb-net-winforms – Polynomial Proton Apr 12 '15 at 21:58
  • Not in VB.NET but this answer will give you a leading to reach your solution http://stackoverflow.com/questions/4428100/find-out-the-control-with-last-focus – Steve Apr 12 '15 at 22:04
  • It is the form's ActiveControl property. The odds that it is still active when you start poking your finger, well, not so good. Use osk.exe – Hans Passant Apr 12 '15 at 23:46
  • I guarantee you will run into all sorts of unexpected fun with this. It's worth doing, but keep in mind that you will likely have to create a windows hook to intercept mouse events and send keystrokes along to the active application. There is also DotNetBar from DevComponents which is not expensive and contains a great keyboard control. – Craig Johnson Apr 13 '15 at 02:59

1 Answers1

0

I'm going to guess that your onscreen keyboard is a series a buttons representing a keyboard.

Since you have two text boxes, you have to have a flag for both text boxes to indicate that they have focus, and when one has it the other doesn't.

Private Sub TextBox1_GotFocus(sender as Object, e as EventArgs) _ 
     Handles TextBox1.GotFocus

// These flags are class level variables
textbox1HasFocus = true;
textbox2HasFocus = false;

End Sub

Private Sub TextBox2_GotFocus(sender as Object, e as EventArgs) _ 
     Handles TextBox2.GotFocus

// These flags are class level variables
textbox2HasFocus = true;
textbox1HasFocus = false;

End Sub

For your keyboard events

Private Sub KeyBoard_Click(sender As Object, e As EventArgs) Handles ButtonA.Click, ButtonB.Click, ButtonC.Click, etc...
    ' I don't know how your using your keyboard, but buttonClickLetter could be determined in multiple ways.  I would use either the Name of the control or even the Tag property for retrieving which letter to use
    String buttonClickLetter = DirectCast(sender, Button).Tag.ToString() 
    If textbox1HasFocus Then
        TextBox1.Text += buttonClickLetter
    ElseIf textbox2HadFocus
        TextBox2.Text += buttonClickLetter
    End If
End Sub
Shar1er80
  • 9,001
  • 2
  • 20
  • 29