Results 1 to 4 of 4

Thread: Inconsistent Text Selection

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Inconsistent Text Selection

    In a VB.Net 2017 Windows Forms application I'm developing, I have a data entry form with multiple text boxes. When the application runs and the user navigates between textboxes using the Tab or Shift-Tab keys, what happens when each textbox gets the focus is not consistent. In some cases, the cursor is located at the end of the existing text, in other cases the entire existing text is selected, and in others only part of the existing text is selected. For a few of the textboxes I've coded a Leave event handler, each of which merely calls one of the routines shown below, but I've not coded ANY Enter or GotFocus event handlers, nor have I included any code that attempts to manually select any text. This makes no sense to me because the Leave event only fires when the textbox loses focus, and should have no effect on what happens when the textbox gets the focus.

    Can anyone explain why this is happening?

    Code:
        Public Sub DateBoxLeave(txt As TextBox)
            With txt
                .Text = .Text.Trim
                If (.Text.Length > 0) Then
                    If IsDate(.Text) Then
                        .Text = Format(Convert.ToDateTime(.Text), DateShowFormat)
                        .BackColor = SystemColors.Window
                    Else
                        .BackColor = WarningBackColor
                    End If
                Else
                    .BackColor = SystemColors.Window
                End If
            End With
        End Sub
        Public Sub PhoneBoxLeave(txt As TextBox)
            With txt
                .Text = .Text.Trim
                If IsAllDigits(.Text) Then
                    Select Case .Text.Length
                        Case 7
                            .Text = .Text.Substring(0, 3) & "-" & .Text.Substring(3, 4)
                        Case 10
                            .Text = .Text.Substring(0, 3) & "-" & .Text.Substring(3, 3) & "-" & .Text.Substring(6, 4)
                        Case Else
                            'do nothing for other phone number lengths
                    End Select
                End If
            End With
        End Sub
        Public Sub StateBoxLeave(txt As TextBox)
            With txt
                .Text = .Text.Trim
                If .Text.Length = 0 Then
                    .BackColor = SystemColors.Window
                Else
                    If IsValidState(.Text) Then
                        .Text = .Text.ToUpper
                        .BackColor = SystemColors.Window
                    Else
                        .BackColor = WarningBackColor
                    End If
                End If
            End With
        End Sub
        Public Sub ZipCodeBoxLeave(txt As TextBox)
            With txt
                .Text = .Text.Trim
                If .Text.Length = 0 Then
                    .BackColor = SystemColors.Window
                Else
                    If IsValidZipCode(.Text) Then
                        If .Text.Length = 9 Then
                            .Text = .Text.Substring(0, 5) & "-" & .Text.Substring(5, 4)
                        End If
                        .BackColor = SystemColors.Window
                    Else
                        .BackColor = WarningBackColor
                    End If
                End If
            End With
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Inconsistent Text Selection

    The TextBox maintains selection when it's not focused. What you see when it receives focus is what was the case when it lost focus. If you set HideSelection to False then you'll see the selection maintained even when it doesn't have focus.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Inconsistent Text Selection

    If you want some specific behaviour when a TextBox receives focus then you can handle the Enter event and do what you need there. For instance, you might create a single method to handle the Enter event of every TextBox and, via the sender parameter, call SelectAll on the currently focused TextBox to consistently select all the text on focus.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Re: Inconsistent Text Selection

    Thanks, jmcilhinney - this is quite helpful.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width