Results 1 to 6 of 6

Thread: focus and tabindex help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    focus and tabindex help

    Hi,

    IŽd like to send the focus directly to the control.

    I know I can do this.

    textbox1.focus().
    ,...
    ..

    But is there a way I could do sth like:

    considering, for instance, I want to send the focus to the control with tabindex=2

    do sth like,..

    focus(tabindex(2)), or
    sendfocus(2),

    (I know this is all incorrect !, but sth like this is what I have in mind)

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,020

    Re: focus and tabindex help

    guess you could do like this:

    VB Code:
    1. For Each ctl As Control In Me.Controls
    2.             If ctl.TabIndex = 2 Then
    3.                 ctl.Focus
    4.                 Exit For
    5.             End If
    6.         Next
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    Re: focus and tabindex help

    Yes good sugestion! DidnŽt think of that way of doing it!
    Thanks!

    But would still be interested in knowing if there is a way of going directly.....

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    Re: focus and tabindex help

    hi Atheist,
    I've been trying it out and it works great!

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: focus and tabindex help

    keep in mind that container controls have their own tab order... so multiple controls COULD Be tab index 2...

    for example, if you had a form that had a groupbox on it.. the group box has its own tab order, and the controls INSIDE it are not looped through when you loop all the controls in the form using me.controls, you would have to loop any containers controls as well, but this depends on how your form is setup...

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,433

    Re: focus and tabindex help

    Why would you want to focus a control with TabIndex 2 without knowing exactly what that control is? Also, as kleinma says, there can be more than one control with the same TabIndex, even if there aren't any containers. Here's a method that will get the control at a certain position in the tab order, which doesn't necessarily mean that that is its TabIndex value:
    VB Code:
    1. Private Function GetControlAtTabPosition(ByVal position As Integer) As Control
    2.         Dim ctl As Control = Me.GetNextControl(Me, True)
    3.         Dim pos As Integer = 1
    4.  
    5.         Do Until pos = position OrElse ctl Is Nothing
    6.             ctl = Me.GetNextControl(ctl, True)
    7.             pos += 1
    8.         Loop
    9.  
    10.         Return ctl
    11.     End Function
    You could also change that to test the CanFocus property to ignore controls that cannot receive focus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width