STS1SS
STS1SS

Reputation: 229

Can't force focus to a textbox when entering a tabpage

I have a tabcontrol. On load I am able to force focus to a textbox as required. If the user opens the second tab I cannot get focus back automatically to the required textbox.

I have tried: tabpage1.entering tabpage1.validated tabpage1.gotfocus tabcontrol1.selectedindexchanged

What is strange to me is if the user goes to second tab page then back, the gotfocus event apparently does not fire for the first tab page??

No luck. Any ideas would be great

Here's an example of code I have tried..

 Private Sub TabPage1_Click(sender As Object, e As System.EventArgs) Handles TabPage1.Click

        TB_Input.Clear()
        TB_Input.Focus()

    End Sub

Upvotes: 0

Views: 957

Answers (3)

MAC
MAC

Reputation: 686

Try to look under textbox properties... see if the TabIndex number is the first of all other tab indexes.

Mostly, .Focus() only works when called which excludes form_load. I've tried myself adding .Focus() upon form load but didn't work and checked the Tab Index.. The first input box has, for example, #43 and the second input box has #42... given the numbers, the cursor will surely focus at the second input box. If the other control, like image or grid, has lower number than input boxes then surely, there will be no focused box.

The tab index is auto-generated. The number depends on who was created first than who. This works in mine, if this works in your problem too... please let me know. :)

Upvotes: 0

Shar1er80
Shar1er80

Reputation: 9041

Use the TabControl.SelectedIndexChanged event, and try something like this...

Private Sub TabControl1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
    If (TabControl1.SelectedIndex > -1) Then
        If TabControl1.SelectedIndex = 0 Then ' First tab
            TextBox2.Focus()
        ElseIf TabControl1.SelectedIndex = 1 Then ' Second tab
            TextBox3.Focus()
        End If
    End If
End Sub

UPDATE

The event fires for me. In your Visual Studio, make sure, in the Designer View, that the TabContol's SelectedIndexChange event has been assigned.

enter image description here

Upvotes: 1

CristiC777
CristiC777

Reputation: 481

OK ! we assume you have TabControl1
with TabItem1 and TabControlPanel1 where you drag a TextBox1
and TabItem2 and TabControlPanel2 where you drag a TextBox2

Paste this in your .vb file

  Private Sub TabItem1_Click(sender As Object, e As EventArgs) Handles TabItem1.Click
    TextBox1.Focus()
End Sub
Private Sub TabItem3_Click(sender As Object, e As EventArgs) Handles TabItem2.Click
    TextBox2.Focus()
End Sub

Upvotes: 0

Related Questions