Home All Groups Group Topic Archive Search About

Setting the correct tabindex

Author
30 May 2006 9:32 PM
Mike Eaton
Hi there,

I've got what I hope is a simple problem to solve...

Here's the back story: I have a windows form.  On the form is a panel
containing three textboxes.  Also on the form is a text box and an ok button.

Here's the problem: On condition A, I want input focus to go to a textbox on
the panel when the form loads.  On condition B, I want the input focus to go
to the textbox that's on the form when the form loads.  Seems pretty simple.

However, I've tried changing the tabindex of each of the text controls and
setting the focus property of the relevant control on form load, but to no
avail.  I know there's got to be a simple way to do this!

Any help is appreciated :-)
Mike

Author
31 May 2006 2:01 PM
Phill W.
Mike Eaton wrote:

> Here's the problem: On condition A, I want input focus to go to a textbox on
> the panel when the form loads.  On condition B, I want the input focus to go
> to the textbox that's on the form when the form loads.  Seems pretty simple.

TabIndex only goes so far and, in this case, not far enough.
Youneed to forcibly move focus to another contorl, but you can't do that
in Form_Load.  The Form (and hence the Control) isn't visible, so won't
accept focus - you have to wait until the Form appears, which is where
the Activated event comes in.

Try this:

Private m_PendingFocus As Control = Nothing

Private Property PendingFocus() as Control
    Get
       Return m_PendingFocus
    End Get
    Set(Value as Control)
       m_PendingFocus = Value
    End Set
End Property

Private Sub Form_Activated( ... ) Handles MyBase.Activated
    If Not (m_PendingFocus Is Nothing) Then
       m_PendingFocus.Focus()
       m_PendingFocus = Nothing
    End If
End Sub

Private Sub Form_Load( ... ) Handles MyBase.Load
    If ConditionA Then
       Me.PendingFocus = TextBoxOnPanel
    Else
       ' Do Nothing - the existing Tab Order will do
    End If
End Sub

HTH,
    Phill  W.
Author
31 May 2006 3:48 PM
Mike Eaton
Hi Phil,

I tried the approach you suggested and had no success.  Here are some more
details:
The current tabindex is set as:
0 - uxTxtCheckAmount :  the text box whose parent is the form (and should
have focus most of the time)
1 - the form's OK button
2 - the form's Cancel button
3 - the panel that hosts the other three text boxes.  their tabindex is set
3.0.0, 3.0.1, and 3.0.2 according to the Tab Order viewer in the IDE.  3.0.0
is the tabindex of the text box uxTxtRouting that I want to have focus some
of the time.

Here's the relevant code I'm using based on what you suggested:

    Private _pendingFocus As Control = Nothing

    Private Property PendingFocus() As Control
        Get
            Return _pendingFocus
        End Get
        Set(ByVal Value As Control)
            _pendingFocus = Value
        End Set
    End Property

    Private Sub ItemValidationForm_Activated(ByVal sender As Object, ByVal e
As System.EventArgs) Handles Me.Activated
        If Not (_pendingFocus Is Nothing) Then
            _pendingFocus.Focus()
            _pendingFocus = Nothing
        End If
    End Sub

    Private Sub ItemValidationForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
        If (_check.MicrError) Then
            MsgBox("error..blah..blah", MsgBoxStyle.Critical, Me.Text)
            Me.PendingFocus = uxTxtRouting
        Else
            ' text box focused via tabindex
        End If
    End Sub

The PendingFocus property is being accessed correctly during the activation
event, but the form always appears with the uxTxtCheckAmount textbox .

Any ideas?
Author
1 Jun 2006 11:40 AM
Phill W.
Mike Eaton wrote:

>         If (_check.MicrError) Then
>             MsgBox("error..blah..blah", MsgBoxStyle.Critical, Me.Text)
>             Me.PendingFocus = uxTxtRouting

> The PendingFocus property is being accessed correctly during the activation
> event, but the form always appears with the uxTxtCheckAmount textbox .

Have you tried setting me.PendingFocus /before/ showing the MsgBox?
I don't know for certain (but wouldn't be in the least bit surprised)
if showing a dialog over a Form doesn't cause the Form to get Activated
first...

HTH,
    Phill  W.