How to Make a TextBox Behave...

  • Thread starter Thread starter Gene Hubert
  • Start date Start date
G

Gene Hubert

How do I make a TextBox behave like the address bar in IE? That is...

If focus is not on the tb, select all text if clicking on the tb or
tabbing into the tb. Clicking again deselects the text and sets the
insertion point to where clicked.

I've tried several options inheriting from TextBox but none work quite
right.

Thanks Much,
Gene H.
 
you could do something like this:

Private bJustGotFocus as boolean '(class level boolean variable)

private sub TextBox1_GotFocus(byval sender as object, _
byval e as System.EventArgs) Handles TextBox1.GotFocus
bJustGotFocus = True
end sub

private sub TextBox1_LostFocus(byval sender as object, _
byval e as System.EventArgs) Handles TextBox1.LostFocus
bJustGotFocus = False
end sub

private sub TextBox1_MouseDown(byval sender as object, _
byval e as System.Windows.Forms.MouseEventArgs) _
Handles TextBox1.MouseDown
if bJustGotFocus then
TextBox1.Select(0, TextBox1.TextLength)
bJustGotFocus = false
end if
end sub

should work like the address bar textbox..
hope this helps..
imran.
 
I think this is what you had in mind but it does not work. It handles
the case of clicking into the textbox but does not handle tabbing into
the textbox. I've tried several variations of this before and after
my original post but none worked quite right.

This is such a common UI element that I thought I'd find the answer
right away after searching this group but I don't think it is there.

It seems simple enough. Anyone else want to have a run at it?

Gene H.

==============================
Option Strict On
Option Explicit On

Public Class BaseTextBox
Inherits System.Windows.Forms.TextBox

Private bJustGotFocus as boolean '(class level boolean variable)

private sub TextBox1_GotFocus(byval sender as object, byval e as
System.EventArgs) Handles MyBase.GotFocus
bJustGotFocus = True
end sub

private sub TextBox1_LostFocus(byval sender as object, byval e as
System.EventArgs) Handles MyBase.LostFocus
bJustGotFocus = False
end sub

private sub TextBox1_MouseDown(byval sender as object, byval e as
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
if bJustGotFocus then
me.Select(0, me.TextLength)
bJustGotFocus = false
end if
end sub
End Class
==================================
 
hmmm..I see your point. If you click the first time, it will select the
whole text. the second time you click, it just sets the cursor to that
position. after that, everytime you tab into that textbox using the tab key,
it just shows the cursor and does not select the entire text. so - lets try
this again and this time with 2 booleans :)

Private bHasFocus As Boolean 'class level boolean variable
Private bJustGotFocus As Boolean 'class level boolean variable


Private Sub TextBox1_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.Select(0, TextBox1.TextLength)
bHasFocus = True
End Sub

Private Sub TextBox1_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.LostFocus
bHasFocus = False
bJustGotFocus = False
End Sub

Private Sub TextBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles TextBox1.MouseDown
If Not bHasFocus Or Not bJustGotFocus Then
TextBox1.Select(0, TextBox1.TextLength)
bHasFocus = True
bJustGotFocus = True
End If
End Sub

this works on my machine - or atleast i think so :) give it a try and let me
know if it helps..
imran.
 
Imran Koradia said:
hmmm..I see your point. If you click the first time, it will select the
whole text. the second time you click, it just sets the cursor to that
position. after that, everytime you tab into that textbox using the tab key,
it just shows the cursor and does not select the entire text. so - lets try
this again and this time with 2 booleans :)
....

Thanks much. It is very close and works the same as some of the
versions that I have tried. The only imperfection is this. Once you
tab into the textbox you have to click two times to get into edit
mode. The first click simply selects all the text a second time.

It seems simple enough but has proven elusive. Maybe one of the
VB.net luminaries will catch this thread and put it to rest.

Gene H.
 
Hi Gene,

Nice chalenge

Can you try this one?

Cor
\\\You need 2 textboxes to test it, to get it out of focus.
Private first As Boolean
Private Sub TextBox1_MouseDown(ByVal sender _
As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles TextBox1.MouseDown
If first Then
Me.TextBox1.SelectAll()
End If
first = False
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.LostFocus
first = True
End Sub
///
 
Thanks Cor.

This handles clicking into the textbox as advertised but the text is
not selected when I tab into it.

Hmmm, I'm thinking that maybe I need to increase the reward for a
correct solution!??? Or maybe contact Mr. Gates directly?

Gene H.
 
Hi Gene,

I did not understand what you was meaning with that tb

Add this class and use this textbox in the code I have sended in the message
before (where is as system.bla bla textbox in your designer code part change
that to Mytextbox on 2 places)

And give it a try, I am curious if this needs more what your wish because I
am not sure of that sentence of the tb of you? I did not fine tune it if it
goes in all circumstances.

I hope this helps?

Cor

\\\
Class MyTextbox
Inherits TextBox
Protected Overrides Function ProcessCmdKey _
(ByRef msg As Message, ByVal keyData As Keys) As Boolean
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) _
And keyData = Keys.Tab Then
If SelectionLength <> Text.Length Then
SelectAll()
Else
SelectionLength = 0
End If
End If
End Function
End Class
///
 
I'll give it a try in the morning Cor and post the outcome here.

Sorry for the misunderstanding of tb. I meant textbox by it. i.e.
"If focus is not on the textbox, select all text if clicking on the
textbox or
tabbing into the textbox. Clicking again deselects the text and sets
the
insertion point to where clicked."

Gene H.
 
Hi Gene,

What is it what is not worked as inteded, with this information I and I
think nobody can do something, for me it is working.

Cor
 
After tabbing into the textbox, I have to click two times to get into edit mode.

Everything else is ok, I think.

Gene H.
 
Back
Top