Vb.net 2008 - Reference control collection by name?

  • Thread starter Thread starter Rob W
  • Start date Start date
R

Rob W

Greetings,

I want to select focus to the first item on a tab page of a tab control.

I wrote some code below which loops around the controls within the current
selected tab page (At the time I wasn't aware it only looped around
container components on my form - groupboxes).

Not the most efficient code but by the end, I have stored the groupbox name
with the lowest tab index i.e. at top of the tab.

I have only referenced items before by index i.e.
Me.Controls.Item(1).Controls how can I reference the groupbox item by name
as retrieved by my code in a string variable ?

i.e. To enable me to loop through the textboxes to find that with the lowest
tab index to set focus to
For each textbox in Me.Controls(grpcontrolname).controls ??
Hope this makes sense (see code below to select groupbox with the lowest tab
index) .....



For Each grpcontrol In tabAddMember.SelectedTab.Controls
If grpcontrol.TabIndex < grpIndexLow Then

'Set string variable to store control name

grpcontrolname = grpcontrol.Name

'set new lowest index to be that of control item

grpIndexLow = grpcontrol.TabIndex

End If

Next grpcontrol
 
Rob said:
Greetings,

I want to select focus to the first item on a tab page of a tab control.


I'm not a LINQ specialist, but this should work: (untested...)

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

'FirstControl' points to the control with the lowest TabIndex.


Armin
 
Rob W said:
Greetings,

I want to select focus to the first item on a tab page of a tab control.

I wrote some code below which loops around the controls within the current
selected tab page (At the time I wasn't aware it only looped around
container components on my form - groupboxes).

Not the most efficient code but by the end, I have stored the groupbox
name with the lowest tab index i.e. at top of the tab.

I have only referenced items before by index i.e.
Me.Controls.Item(1).Controls how can I reference the groupbox item by name
as retrieved by my code in a string variable ?

i.e. To enable me to loop through the textboxes to find that with the
lowest tab index to set focus to
For each textbox in Me.Controls(grpcontrolname).controls ??
Hope this makes sense (see code below to select groupbox with the lowest
tab index) .....



For Each grpcontrol In tabAddMember.SelectedTab.Controls
If grpcontrol.TabIndex < grpIndexLow Then

'Set string variable to store control name

grpcontrolname = grpcontrol.Name

'set new lowest index to be that of control item

grpIndexLow = grpcontrol.TabIndex

End If

Next grpcontrol

Perhaps you could look at the tab control SelectedIndexChanged event and
designate which control gets focus by calling the Select method of the
control
 
Thanks, I'm a newbie to vb.net and never even heard of LINQ prior to this
post.

I changed the code slightly as not all the controls could be read in one
instance , hierarchy appears that only containers can be retrieved in the
initial select.

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()



Cheers I'm still a newbie to vb.net and well programming, but feel I'm
learning every day :-)
 
Back
Top