Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tab Control - Setting focus to first field within a tab page

Status
Not open for further replies.

rw409168

Programmer
Jul 16, 2009
95
GB
Greetings,

I have a tab control and for when a tab page has focus I would like to set focus to the first field on the page.

This has to be done manually (I assume, as cannot see a suitable property on tab control or page).

I could choose the suitable event and then hardcode the value of the first control on the form and set focus i.e. txtbox1.focus()

The shortcoming being if the fields change order I would have to change the code.

Is the only other way to loop around and find the control with the lowest tabindex?

Any ideas (simpler the better as a newbie) to allow focus to be set the first control on a tab page would be appreciated.

Thanks
Rob
 

How are the controls being added to the tab page?



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
They are added at design time.

Another important note is that each tab has groupboxes within it.

I could not loop around ALL the controls using Tabcollection.SelectedTab.Controls as the hierachery appears to be group box controls first.

So looped to find the lowest tab order of the group boxes and then another loop to search items within the group box.

Hoping there is an easier way though and my request makes sense.

Rob


 

You could always name the first field the same, e.g., Field1, FirstField, etc. Then you could use the GetControlByName function found in the FAQs: faq796-5698

Public Function GetControlByName(ByVal Name As String) As Control

'now, why would I put a "_" in front of the name?
Dim info As System.Reflection.FieldInfo = Me.GetType().GetField("_" & Name, _
System.Reflection.BindingFlags.NonPublic Or _
System.Reflection.BindingFlags.Instance Or _
System.Reflection.BindingFlags.Public Or _
System.Reflection.BindingFlags.IgnoreCase)

If info Is Nothing Then Return Nothing
Dim o As Object = info.GetValue(Me)
Return o

End Function


You would call it like this:

Dim c As Control = GetControlByName("FirstField")

c.Focus()



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks for the reply, I will have to read up on vb.net and reflection.

In the end I solved the problem using LINQ

Dim FirstGroupBoxControl = (From c In tabAddMember.SelectedTab.Controls _
Select Control = DirectCast(c, Control) _
Order By Control.TabIndex).First

Dim firstEditableControl = (From c In FirstGroupBoxControl.Controls _
Select Control = DirectCast(c, Control) _
Where Control.TabStop = True _
Order By Control.TabIndex).First

firstEditableControl.Focus()

Hope this helps someone else too :)
 
Like jebenson said I would name the controls in a manner so that you always know which should be the first. That said if you are using 2005/2008 I don't really see a reason to use reflection as you can already call for a control by its name.

Code:
    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Dim ctrl As Control = TabControl1.SelectedTab.Controls("tb1")

        If ctrl IsNot Nothing Then
            ctrl.Focus()
        End If
    End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top