Hi there, I am creating a application for a touch screen computer/till of course, this means that I have to make a keyboard in my application so you don't have to mess around getting the keyboard out. I have multiple textbox's on my application and would like to know how to make the keys type into the textbox that is focused.

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        NameTextBox.Text = NameTextBox.Text & "M"
    End Sub

^ At the moment I was using this but this only puts the letters into the NameTextbox and also the NameTextbox doesn't appear to be focused..

How would I get the keys to type into the textbox that is focused?

Dani AI

Generated

Short summary: the problem in 's screenshots is not that keys are mysterious — it's that clicking a Button steals focus from the textbox so future key code writes to whichever control the code explicitly uses (or whatever was hard‑coded). Several useful ideas are already in the thread: used sender.Text, tracked which box had focus, and suggested SendKeys. The cleanest, most reliable pattern is to store a TextBox reference when a box receives focus (Enter/GotFocus) and have all keyboard buttons insert into that stored TextBox at its SelectionStart/SelectionLength. That keeps the caret position, supports selections, and avoids brittle SendKeys behavior.

Example (wire the Enter handlers for the textboxes and route all key buttons to one Click handler):

Private currentTextBox As TextBox = Nothing
Private capsOn As Boolean = False

Private Sub TextBox_Enter(sender As Object, e As EventArgs)
    currentTextBox = TryCast(sender, TextBox)
End Sub

Private Sub KeyButton_Click(sender As Object, e As EventArgs)
    If currentTextBox Is Nothing Then Return
    Dim key As String = CType(sender, Button).Text
    If Not capsOn Then key = key.ToLower()
    Dim s As Integer = currentTextBox.SelectionStart
    Dim l As Integer = currentTextBox.SelectionLength
    currentTextBox.Text = currentTextBox.Text.Remove(s, l).Insert(s, key)
    currentTextBox.SelectionStart = s + key.Length
    currentTextBox.SelectionLength = 0
    currentTextBox.Focus()
End Sub

' Backspace: remove selection or previous char, then restore caret
' Caps toggle: flip capsOn and update button visual

Notes/troubleshooting:

  • 's idea (focus tracking) is fine but safer to store the TextBox object rather than an integer index (it avoids copy/paste bugs like writing always to NameTextBox).
  • Avoid SendKeys on tills — it depends on OS focus and keyboard layout.
  • If the keyboard lives in a separate form, expose a public property/method on the keyboard form (e.g., SetTarget(TextBox)) or let the main form pass its currentTextBox to the keyboard form before showing it. Ensure the keyboard writes to that reference and calls Focus on the target after inserts.

Recommended Answers

All 12 Replies

Hi,

Here's something I made with numbers.

Private Sub Digit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttn1.Click, bttn2.Click, bttn3.Click, _
    bttn4.Click, bttn5.Click, bttn6.Click, bttn7.Click, bttn8.Click, bttn9.Click, bttn0.Click
      
        lblDisplay.Text = lblDisplay.Text + sender.Text
    End Sub

Like you can see it's for numbers.
Add ten buttons and name them like I did, set there text from 1 to 0 and to show the result I used a label with name lblDisplay.

Way you need to set the focus for some textboxes?

Hi, thanks for your reply but I'm not sure if you understood what I ment, I have 3 textbox's on my application each named NameTextbox, DOBTextbox and IDTextbox, I have also made a keyboard below these box's (A-Z, 0-9, backspace and clear) Now, what I need is when I click a textbox when I press a button it puts that letter/number into that focused textbox because at the moment the code I have puts the text into a certian textbox but doesn't keep the textbox focused. (See code above).

Hi,

You can try this:

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        NameTextBox.Text = (NameTextBox.Text & "M")
    End Sub

I really appriciate the help but its not getting me anywhere :/, Do you understand what I'm trying to say? I will try to explain in pictures to see if it helps.

Ok you see here I have clicked inside the ID box, when I click any key I want the letter/number to go inside that box (A Focused box?) but at the moment its going into the "Name Box"


And here I clicked inside the Name box and all numbers and letters go into here..

You see what I'm on about now? When I click inside a box I want that key to go inside there.

You should try this

Dim box As Integer
  Private Sub NNameTextbox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles NameTextbox.GotFocus
        box = 1

    End Sub
 Private Sub DOBTextbox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DOBTextbox.GotFocus
        box = 2

    End Sub
 
Private Sub IDTextbox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles IDTextbox.GotFocus
        box = 3

    End Sub


    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

if box = 1 then      
  NameTextBox.Text = NameTextBox.Text & "M"
end if
if box = 2 then      
  DOBTextbox.Text = NameTextBox.Text & "M"
end if
if box = 3 then      
  IDTextbox.Text = NameTextBox.Text & "M"
end if
    End Sub

Hi,

meffe beat me on this one ;)

how to do the backspace code?
and the caps lock code?

Hi,

Something like this;

Private Sub NameTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NameTextBox.KeyDown
        If e.KeyCode = Keys.Back Then
            MessageBox.Show("You hit the Backkey")
        End If
        If e.KeyCode = Keys.CapsLock Then
            MsgBox("You hit the Capslockkey")
        End If
    End Sub

Thanks Luc001 for the quick respond.
what i mean is, I'm doing on screen keyboard also. if i add a caps lock button which enable caps word i can't seem to solve it

and last question how can i make the "if condition" to check 2 condition at the same time?

the code for the ????? i can't slove.
try boolean and int declare can't get it work


If box = 1 Then
textbox1.Text = textbox1.Text & "A"
???????
textbox1.Text = textbox1.Text & "a"
End If

If box = 2 And caps2 = True Then
textbox2.Text = textbox2.Text & "A"
???????
else
textbox2.Text = textbox2.Text & "a"
End If

I think you are maybe over complicating this I don't think you need to keep track of the box in focus. I suspect all you need for each button is to send in a simulated key stroke.

Private Sub QButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

if caps then Sendkeys.Send(QButton.Text) else sendkeys.send(QButton.Text.toLower)

... nvm i solve already

Private Sub btn_A_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_A.Click

If box = 1 And capsStatus = "On" Then
textbox1.Text = textbox1.Text & "A"

ElseIf box = 2 And capsStatus = "On" Then
textbox2.Text = textbox2.Text & "A"

ElseIf box = 1 And capsStatus = "Off" Then
textbox1.Text = textbox1.Text & "a"
Else
textbox2.Text = textbox2.Text & "a"
End If

End Sub

You should try this

Dim box As Integer
  Private Sub NNameTextbox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles NameTextbox.GotFocus
        box = 1

    End Sub
 Private Sub DOBTextbox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DOBTextbox.GotFocus
        box = 2

    End Sub
 
Private Sub IDTextbox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles IDTextbox.GotFocus
        box = 3

    End Sub


    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

if box = 1 then      
  NameTextBox.Text = NameTextBox.Text & "M"
end if
if box = 2 then      
  DOBTextbox.Text = NameTextBox.Text & "M"
end if
if box = 3 then      
  IDTextbox.Text = NameTextBox.Text & "M"
end if
    End Sub

Hello my frinds .
I am newby in vb and i want to ask something .
i develope a touch screen app.
i open my touch screen keboard with different winform and my problem is only in the first text box i can write in the other form . any help please ???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.