Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TextBox won't receive focus?

Posted on 2014-10-27
20
Medium Priority
?
100 Views
Last Modified: 2014-12-24
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!
0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
20 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40408126
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
 

Author Comment

by:BlakeMcKenna
ID: 40408395
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
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40409000
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BlakeMcKenna
ID: 40409041
so far so good!
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40409234
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
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40409345
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
 

Author Comment

by:BlakeMcKenna
ID: 40409382
Changing to SelectedIndexChanged Event didn't change anything.
0
 

Author Comment

by:BlakeMcKenna
ID: 40409384
The Event that I'm using should accommodate what I'm trying to do!
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40409511
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
 

Author Comment

by:BlakeMcKenna
ID: 40409539
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
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40409567
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
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40409650
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
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 2000 total points
ID: 40410705
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
 

Author Comment

by:BlakeMcKenna
ID: 40410938
I've never heard of "Property Sheet"...what is that?
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40411265
in design view, select tabconntrol and click on F4. It will bring Property interface or some call property sheet.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40411369
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
 

Author Comment

by:BlakeMcKenna
ID: 40411391
Thanks Mike!
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40414734
Hi Blake?

Were you able to get it working?

Mike
0
 

Author Comment

by:BlakeMcKenna
ID: 40415593
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
 

Author Closing Comment

by:BlakeMcKenna
ID: 40516781
Thanks for your help!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question