Question Focus on textfield upon tab change

vukovicb

New member
Joined
Oct 12, 2013
Messages
4
Programming Experience
1-3
Hello I`m fairly new to VB but I`m trying to do my best.
However, sometimes stupid problems like this make me unhappy :(

I have one tabcontrol and two tabs:
tab1 (default),
tab2

so, I`m trying to set a focus to a text field within the tab2 after I click on tab2, so I use:

VB.NET:
Private Sub TabControl1_Changed(sender As Object, e As SelectionChangedEventArgs) Handles TabControl1.SelectionChanged
        If (tab2.IsSelected) Then
            tab2.Focus()
            txtField.Text = ""
            txtField.Focus()
            txtField.SelectAll()
        End If
    End Sub

So, if I put a breakpoint inside that IF statement and debug my application.
Uppon clicking on the tab2, it WILL enter that scope.
Even if I try to add something to that txtField like txtField.Text = "123" it WILL enter that value.

The only thing it is not doing is the focus. So, what am I doing wrong???

Thanks folks
 
This works exactly as you'd expect for me:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
    If TabControl1.SelectedTab Is TabPage2 Then
        TextBox4.Select()
        TextBox4.SelectAll()
    End If
End Sub
Note the use of the Select method rather than Focus, as specified by the documentation that you appear not to have read. That said, it worked for me using Focus too.
 
Thank you,

however, I`m still puzzled.

VB.NET:
Error	5	Event 'SelectedIndexChanged' cannot be found.	
Error	6	'SelectedTab' is not a member of 'System.Windows.Controls.TabControl'.
Error	7	Argument not specified for parameter 'length' of 'Public Sub Select(start As Integer, length As Integer)'.	
Error	8	Argument not specified for parameter 'start' of 'Public Sub Select(start As Integer, length As Integer)'.

I have tried to import the missing libraries and to declare it (SelectedTab error) but no luck
Imports System.Windows.Forms.TabPage

'Declaration
<BrowsableAttribute(False)> _
Public Property SelectedTab As TabPage

Thanks
 

Attachments

  • 2013-10-13_0120.png
    2013-10-13_0120.png
    8.4 KB · Views: 37
You posted this question in the Windows Forms forum but your project is WPF, not Windows Forms. I will move it for you this time but please ensure that you post in the correct forum in future.
 
So is there a way to use "SelectedIndexChanged" in WPF? SelectedIndexChanged just does not show up in intellisense for me and then is marked as an error as the event is not found.
 
Back
Top