Solved

TextBox won't receive focus?

Posted on 2014-10-27
20
90 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
  • 11
  • 9
20 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
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 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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
 

Author Comment

by:BlakeMcKenna
Comment Utility
so far so good!
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
Changing to SelectedIndexChanged Event didn't change anything.
0
 

Author Comment

by:BlakeMcKenna
Comment Utility
The Event that I'm using should accommodate what I'm trying to do!
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
Comment Utility
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
Comment Utility
I've never heard of "Property Sheet"...what is that?
0
 
LVL 33

Expert Comment

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

Expert Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
Thanks Mike!
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
Hi Blake?

Were you able to get it working?

Mike
0
 

Author Comment

by:BlakeMcKenna
Comment Utility
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
Comment Utility
Thanks for your help!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now