TextBox won't receive focus?

I have a Windows Form that contains one TabControl with about 20 TabPages. I'm using the "Selected" Event of the TabControl to navigate through the TabPages. Within this event I use a Select Case e.TabPage.Name construct. At the end of each Case statement, I set the focus to a specific field on the selected tab.

With that in mind, I have a generic Event Handler for the "Enter" Event for each TextBox on each TabPage. When the page first loads, I actually use the Page_Shown() Event to set the focus to a TextBox for the first TabPage (displayed by default). But all subsequent tab selections fail to fire the "txtBox_Enter" Event? Not sure why. Here is my code below:

For illustration purposes, I have eliminated most of the ".Enter" events for the TextBox controls.

    Private Sub txtBox_Enter(sender As Object, e As EventArgs) Handles txtBox.Enter, txtAngles_angle.Enter, txtAngles_sortOrder.Enter, txtChannels_channel.Enter, txtConditions_Condition.Enter
        Try
            Dim txt As TextBox = CType(sender, TextBox)

            EH.ErrorMessage = String.Empty

            txt.SelectAll()
            GetFocusBackColor(txt)

        Catch ex As Exception
            EH.ErrorMessage = "txtBox_Enter() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window


Again, for illustration purposes, I have eliminated most of the "Case" clauses. The functionality is identical for each TabPage except for the different TextBox controls on each TabPage.

    Private Sub tab1_Selected(sender As Object, e As TabControlEventArgs) Handles tab1.Selected
        Try
            If Not blnIsLoading Then
                ClearAllFields()
                If EH.ErrorMessage > String.Empty Then
                    GoTo ProcessMessage
                End If

                Select Case e.TabPage.Name
                    Case "tabAngles"  'LKUP_Angles
                        LoadGrid_Angles()
                        If EH.ErrorMessage > String.Empty Then
                            GoTo ProcessMessage
                        End If

                        LoadCombo_AnglesUnitType()
                        If EH.ErrorMessage > String.Empty Then
                            GoTo ProcessMessage
                        End If

                        txtAngles_angle.TabIndex = 0
                        cmbAngles_UnitType.TabIndex = 1
                        cmbAngles_angleUnit.TabIndex = 2
                        txtAngles_sortOrder.TabIndex = 3
                        cboAngles_active.TabIndex = 4
                        btnAddUpdate.TabIndex = 5
                        cboAngles_active.Checked = True
                        txtAngles_angle.Focus()
                    Case "tabChannels"  'LKUP_Channels
                        LoadGrid_Channels()
                        If EH.ErrorMessage > String.Empty Then
                            GoTo ProcessMessage
                        End If

                        txtChannels_channel.TabIndex = 0
                        txtChannels_sortOrder.TabIndex = 1
                        cboChannels_Active.TabIndex = 2
                        btnAddUpdate.TabIndex = 3
                        cboChannels_Active.Checked = True
                        txtChannels_channel.Focus()
                    Case "tabConditions"  'LKUP_Conditions
                        LoadGrid_Conditions()
                        If EH.ErrorMessage > String.Empty Then
                            GoTo ProcessMessage
                        End If

                        txtConditions_Condition.TabIndex = 0
                        txtConditions_SortOrder.TabIndex = 1
                        cboConditions_Active.TabIndex = 2
                        btnAddUpdate.TabIndex = 3
                        cboConditions_Active.Checked = True
                        txtConditions_Condition.Focus()
                End Select
            End If

            EH.ErrorMessage = String.Empty

ProcessMessage:

        Catch ex As Exception
            EH.ErrorMessage = "tab1_Selected() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window


If this isn't enough code, please let me know.

Thanks!
BlakeMcKennaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
I am building a sample to try your code. Meanwhile, make sure the text box is enabled before .Focus().

Also, are you stepping through your code to see what is happening? This is to make sure your logic actually gets to fire .Focus.

Mike

Comment added:
Please post your "GetFocusBackColor(txt As TextBox)" event.

Also, it seems the sequence ought to be:

Tab1.Focus()
txtTextBox1.Focus()

or

Tab2.Focus()
txtOtherTextBox.Focus()
0
BlakeMcKennaAuthor Commented:
I am stepping through the code all the way. When the tabPage gets focus, I start stepping through the code in the "Selected" Event. As soon as the last statement in the Case clause is reached (it's a .Focus command), I execute it and instead of going to the txtBox_Enter Event, it finishes through the Selected Event. Below is my code for the GetFocusBackColor.

    Public Sub GetFocusBackColor(ByVal sender As Control, Optional ByVal blnLostFocus As Boolean = False)
        Try
            Dim ctlID As Integer = 0
            Dim iBackGroundColor As Integer = 0
            Dim iFocusBackGroundColor As Integer = 0
            Dim ctl As Control = DirectCast(sender, Control)

            EH.ErrorMessage = String.Empty

            If TypeOf ctl Is Button Then
                ctlID = 1
            ElseIf TypeOf ctl Is TextBox Then
                ctlID = 4
            ElseIf TypeOf ctl Is ComboBox Then
                ctlID = 7
            End If

            Dim result = lstBGColors.Find(Function(c) c.control_ID = ctlID)

            If gblnFocusBackColor Then
                iFocusBackGroundColor = gFocusBackColor
            Else
                iFocusBackGroundColor = ctl.BackColor.ToArgb
            End If

            If gblnVisualSettings Then  'The Visual Settings in turned on for User
                If blnLostFocus Then
                    If Not IsNothing(result) Then
                        If result.checked Then
                            iBackGroundColor = result.BC
                        Else
                            If ctlID <> 1 Then
                                iBackGroundColor = Color.White.ToArgb
                            Else
                                iBackGroundColor = Control.DefaultBackColor.ToArgb
                            End If
                        End If
                    Else
                        If ctlID <> 1 Then
                            iBackGroundColor = Color.White.ToArgb
                        Else
                            iBackGroundColor = Control.DefaultBackColor.ToArgb
                        End If
                    End If

                    ctl.BackColor = Color.FromArgb(iBackGroundColor)
                Else
                    ctl.BackColor = Color.FromArgb(iFocusBackGroundColor)
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "modMain/GetFocusBackColor() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(frmMain, frmMain.sbr, EH.ErrorMessage)
    End Sub

Open in new window

0
Mike EghtebasDatabase and Application DeveloperCommented:
In my sample, I have:

TabControl1 with tabAngels and tabChannels on it therefore, I have changed the event to:

Private Sub TabControl1_Selected(sender As Object, e As TabControlEventArgs) Handles TabControl1.Selected

The names of few text boxes on each tab:

tabAngels-
                     txtAngles_angle
                    txtAngles_sortOrder

tabChannels-
                    txtChannels_channel
                    txtChannels_sortOrder

I will be reading the description again to familiarize my self with the focus logic.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

BlakeMcKennaAuthor Commented:
so far so good!
0
Mike EghtebasDatabase and Application DeveloperCommented:
Use different event (TabControl1_SelectedIndexChanged):

 Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

        TabControl1.Update()

        Select Case TabControl1.SelectedIndex
            Case 0 '"tabAngles"  'LKUP_Angles

                txtAngles_angle.Focus()

            Case 1 '"tabChannels"  'LKUP_Channels

                txtChannels_channel.Focus()

        End Select

    End Sub

Open in new window

0
Mike EghtebasDatabase and Application DeveloperCommented:
BTW,

In tab order, I think you do not need to change them dynamically. The following will do unless there is other reasons for doing so:

tabAngels-
                     txtAngles_angle                  tabindex =0
                    txtAngles_sortOrder           tabindex =1

tabChannels-
                    txtChannels_channel         tabindex =2
                    txtChannels_sortOrder     tabindex =3
0
BlakeMcKennaAuthor Commented:
Changing to SelectedIndexChanged Event didn't change anything.
0
BlakeMcKennaAuthor Commented:
The Event that I'm using should accommodate what I'm trying to do!
0
Mike EghtebasDatabase and Application DeveloperCommented:
In my sample it works. I can zip it and send it to you. Because EE is not allowing files with .sol or similar to be uploaded, I can email via my hotmail account to you (gmail doesn't allow it either). Do you have hotmail account?
0
BlakeMcKennaAuthor Commented:
I can take a look at it but all I did was just change the Event and the Select/Case clause to reflect SelectedTabIndex. My email is blakemc@techemail.com

Thanks!
0
Mike EghtebasDatabase and Application DeveloperCommented:
I was so confident about the solution that I deleted it after my last post. But, I have recovered and you should have it shortly.

BTW, make sure to select the tabcontrol in design view and access the following even via its property sheet:

TabControl1_SelectedIndexChanged

Mike

P.S. I hope your email is allowing this type of files.
0
Mike EghtebasDatabase and Application DeveloperCommented:
re:>  all I did was just change the Event

The existing event has different parameters compared to TabControl1_SelectedIndexChanged(...) and that is why you need to use property sheet to create this event.
0
Mike EghtebasDatabase and Application DeveloperCommented:
To make sure your first tab gets focus when you first start, use the following 3 events:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '  SELECT 2ND TAB FIRST AND THE 1ST TAB
        ' (this portion possibly could be improved later)
        TabControl1.SelectedIndex = 1
        TabControl1.SelectedIndex = 0

    End Sub
'-------------------------------
Private Sub SetFocusOn(TabIndex As Integer)

        TabControl1.Update()

        ' SET THE FOCUS ON THE SELECTED CONTROL
        Select Case TabIndex 'TabControl1.SelectedIndex

            Case 0 '"tabAngles"  'LKUP_Angles

                txtAngles_angle.Focus()

            Case 1 '"tabChannels"  'LKUP_Channels

                txtChannels_channel.Focus()

        End Select

    End Sub
'-------------------------------
    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

        SetFocusOn(TabControl1.SelectedIndex)

    End Sub

Open in new window


Please note that "TabControl1_SelectedIndexChanged" has different signuture compared to "tab1_Selected"; make sure to use property sheet to create this event.

Additionally, it seems you have named your tab control as tab1 whereas it should be named TabControl1.

The above changes have been tested. So, if it didn't work for you on the first try, please check your work again.

Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
I've never heard of "Property Sheet"...what is that?
0
Mike EghtebasDatabase and Application DeveloperCommented:
in design view, select tabconntrol and click on F4. It will bring Property interface or some call property sheet.
0
Mike EghtebasDatabase and Application DeveloperCommented:
more on property sheet:

Because EE could not upload the image I have prepared for you, I did email it to you instead.

Mike
0
BlakeMcKennaAuthor Commented:
Thanks Mike!
0
Mike EghtebasDatabase and Application DeveloperCommented:
Hi Blake?

Were you able to get it working?

Mike
0
BlakeMcKennaAuthor Commented:
Mike,

I initially tried what you suggested and didn't get it working and then I got pulled away on another task. I'm going to try and resume this issue today. Thank you again for your suggestions. It will eventually be resolved through trial & error.

Cheers!
0
BlakeMcKennaAuthor Commented:
Thanks for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.