Highlight text on textbox focus.
Hi Guys,
I´m trying to highlight the text when the textbox gets focus, like an
address bar of browsers.
I´m using this code so far:
Code:
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Enter
TextBox1.SelectionStart = 0
TextBox1.SelectionLength = Len(TextBox1.Text)
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Leave
TextBox1.SelectionLength = 0
End Sub
And it works fine, but I'd like to do the same thing on the click event.
Here's my problem. The same code of enter event in text box, apparently
doesn't work on mouse down and mose up.
Any suggestions?
Re: Highlight text on textbox focus.
Moved From The FAQ Section
Re: Highlight text on textbox focus.
You can use the TextBox.SelectAll method to select all of the containing text.
Re: Highlight text on textbox focus.
Quote:
Originally Posted by
ForumAccount
You can use the TextBox.
SelectAll method to select all of the containing text.
It works for Enter event of the textbox, but on click event the text will be highlited in every click.
I'd like an effect like the address bar on the web browsers for example.
Re: Highlight text on textbox focus.
You'll need to use a flag that only selects the text of the TextBox on the first Click event. Test against a flag you have defined at class level. If the flag is true it should not select the text, otherwise, it should select the text and set the flag to true. Then in an appropriate event, you seem to have decided upon the Leave event, set the flag back to false.
Re: Highlight text on textbox focus.
Thanks for the replying.
Here's what I´ve tried to do.
Code:
Private flag As Boolean = False
Private Sub TextBox1_Enter1() Handles TextBox1.Enter
TextBox1.SelectAll()
End Sub
Private Sub TextBox1_Leave1(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
flag = False
End Sub
Private Sub TextBox1_MouseUp() Handles TextBox1.MouseUp
If flag = False Then
TextBox1.SelectAll()
flag = True
Else
Exit Sub
End If
End Sub
But it doesn't work as I expected.
When I use TAB to do the focus the text remains highlighted, but If I click on the textbox it become highlighted again, repeating the same action as if I have used the TAB.
Re: Highlight text on textbox focus.
I'm not 100% sure what you're desired behavior is, but how about just setting the flag to false in the Enter event?
Re: Highlight text on textbox focus.
Thanks again for replying.
Well, I'd like a behavior like the address bar of a browser.
And the code above works fine, except in this scenario:
When I focus the textbox with TAB the text is highlited, but if I click in this same textbox righ after the tab, instead of the highlight disappear, it appears again.
I know it's kinda complicated to understand, but try to observe how the focus works in your address bar, in both events click and tab, and you'll get it.
Back to the problem. I figured out that when I click on the text box, it triggers the click, mouse down, mouse up and the enter events too. So, setting the flag as false on the enter event doesn't work too.
There's anyway of cancel an event? Because If I could cancel the click events when I click on textbox, I think it's gonna work.