Solved

TextBox won't receive focus?

Posted on 2014-10-27
20
99 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 500 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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 …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

632 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