-
Oct 24th, 2019, 04:00 PM
#1
Thread Starter
Lively Member
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
-
Oct 24th, 2019, 06:25 PM
#2
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.
-
Oct 24th, 2019, 06:33 PM
#3
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.
-
Nov 5th, 2019, 01:40 PM
#4
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|