user225269
user225269

Reputation: 10893

How to check focused TextBox in vb.net winforms?

I have multiple textbox in a form. How do I know what textbox the cursor currently is? Trying to do something like this:

If TextBox2.Focus() = True Then
            MessageBox.Show("its in two")
        ElseIf TextBox3.Focus = True Then
            MessageBox.Show("its in three")
        End If

But I think its not working.

Upvotes: 0

Views: 22442

Answers (3)

Javed Akram
Javed Akram

Reputation: 15354

Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.

You can do two things:

Make a combined Focus event for all TextBoxes and check its Sender object.

Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
    Dim currTextBox As TextBox = sender

    If currTextBox.Equals(TextBox2) Then
        MessageBox.Show("it's in two")
    ElseIf currTextBox.Equals(TextBox3) Then
        MessageBox.Show("it's in three")
    End If

End Sub

OR

Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.

 Dim str As String  
 Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles TextBox2.Enter
     str = "two"
 End Sub

 Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles TextBox3.Enter
     str = "three"
 End Sub

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click
      MessageBox.Show("it's in " & str)
 End Sub

Upvotes: 3

SeriousSamP
SeriousSamP

Reputation: 436

I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.

Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each control As Control In Me.Controls
        If control.GetType.Equals(GetType(TextBox)) Then
            Dim textBox As TextBox = control
            AddHandler textBox.Enter, Sub() focussedTextBox = textBox
        End If
    Next
End Sub

This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:

If Not focussedTextBox Is Nothing Then
    ...
End If

Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.

Upvotes: 4

cwharris
cwharris

Reputation: 18125

TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)

In fact, all form controls have the Focused property.

Upvotes: 4

Related Questions