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!

Get Control by TabIndex 1

Status
Not open for further replies.

mansii

Programmer
Oct 18, 2002
641
ID
Thank's to Chrissie for posting faq796-5698.

Now, is it possible to get a control by it's TabIndex? How? :)

TIA
mansii
 
Essentially you would use something like:

for each ctl as control in me.controls
if ctl.tabindex = xxx then
return ctl
exit for
next

HOWEVER - this will only work in part. Some controls (eg panels) are container controls and as such have their own control collections with relative tabindexes therefore you could have several controls with the same tabindex.

Hope this helps.
 
Samurai,
I need to get a control by it's tabindex bcs there are many controls in the form.
Maybe the design is not too good, but I do not record each control's name. All I want to do is to access the control by it's tabindex. Yes, we can use Sendkeys to go to next control, but it's another issue.

E&F,
It looks like that the function will loop until it found the desired ctl.
There are lots of ctls in the form.
It doesn't look efficient if the function is called oftenly.

Other ideas?

TIA
mansii
 
The problem with using TabIndex is:

Assume that on a Form you have -

TextBox1
Panel1
Panel1 -> TextBox1
TextBox2

The TabIndex values will be:
0 TextBox1
1 Panel1
0 Panel1 -> TextBox1
2 TextBox2

Hope this helps.
 
Got your point, E&F.

What about:
Code:
Dim xTxt as Object
For Each xTxt in Me.Controls
    If TypeOf xTxt is TextBox Then
        If xTxt.TabIndex = xxx Then
            Return xTxt
            Exit For
        End If
    End If
Next
?

Or maybe I must use it's name.
Suggestions, please?

mansii
 
Yes but ....

You could in theory have more than one TextBox with the same TabIndex (unless you haven't used any container controls)

Hope this helps.
 
If you only want to loop textboxes do

Code:
dim xTxt as textbox
for each xTxt in me.controls
   If xTxt.TabIndex = xxx Then
      Return xTxt
      Exit For
   End If
Next

that automatically sort out anything thats not a textbox if I correctly remember how for each loops works.


- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
I see.
Seems that the TabIndex plays a different rule in VB.

Anyways, thank's for your time, E&F.

Regards,
mansii
 
Sorry I couldn't have been more help, however if you don't have any container controls the last code you posted should work fine.
 
Yep. That one's in my plan too.
Let's see how far I can go.

Ahh.. SMURF.. I didn't see you. (sigh..)

The codes gave me
[blue]
An unhandled exception of type 'System.InvalidCastException' occurred in SomeExe.exe

Additional information: Specified cast is not valid.
[/blue]

But it's okay now since I plan to take another route to Rome. Thank's.

Regards,
mansii
 
Yup... sorry about that. Some silly idea I got about a for each loop disregaring elements it cannot convert.. I blame on too little beer along with too much sun this weekend ;)

Dunno what plan you gonna take now, but have you checked out the tag property?

- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
too little beer along with too much sun this weekend

As John Lennon said, "Missery.." ;)

Anyways, since I have container ctl, here's my plan:
Code:
For Each xTxt in Me.Controls
    [blue]If Not xTxt Is Nothing Then[/blue]
        If TypeOf xTxt is TextBox Then
            If xTxt.TabIndex = xxx Then
                Return xTxt
                Exit For
            End If
        End If
    End If
Next

Any comments? Will this mess up my week end?
 
You can remove the test for Nothing by assigning xTxt as Control:

Code:
For Each xTxt as Control in Me.Controls
  If TypeOf xTxt Is TextBox Then
    If xTxt.TabIndex = xxx Then
      Return xTxt
      Exit For
    End If
  End If
End If

However, this still doesn't solve the container problem.

What you could do -

[Thinking out loud at 1:50 am]
- is return an array of TextBoxes that have that TabIndex, then, if there is only one no problem you've found the right TextBox, otherwise check the Parent property of each item in the array to see if it is the correct TextBox -
[/Thinking out loud at 1:50 am]


I've stopped thinking now, but its something to play with.
 
Crossed my mind, too.
But I think that it would be easier to ask the other member of the team to make the TabIndex unique.

I have to stop thinking too, E&F.
Coffee [morning]?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top