473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to Make a TextBox Behave...

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.
Nov 20 '05 #1
12 1664
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.

"Gene Hubert" <gw******@hotmail.com> wrote in message
news:7e**************************@posting.google.c om...
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.

Nov 20 '05 #2
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
==================================
"Imran Koradia" <no****@microsoft.com> wrote in message news:<eZ**************@tk2msftngp13.phx.gbl>...
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.

"Gene Hubert" <gw******@hotmail.com> wrote in message
news:7e**************************@posting.google.c om...
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.

Nov 20 '05 #3
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.

"Gene Hubert" <gw******@hotmail.com> wrote in message
news:7e**************************@posting.google.c om...
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
==================================
"Imran Koradia" <no****@microsoft.com> wrote in message

news:<eZ**************@tk2msftngp13.phx.gbl>...
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.

"Gene Hubert" <gw******@hotmail.com> wrote in message
news:7e**************************@posting.google.c om...
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.

Nov 20 '05 #4
"Imran Koradia" <no****@microsoft.com> wrote in message news:<OZ**************@tk2msftngp13.phx.gbl>...
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.
Nov 20 '05 #5
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
///
Nov 20 '05 #6
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.

"Cor Ligthert" <no**********@planet.nl> wrote in message news:<OF**************@TK2MSFTNGP09.phx.gbl>...
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
///

Nov 20 '05 #7
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
///
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.

Nov 20 '05 #8
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.

"Cor Ligthert" <no**********@planet.nl> wrote in message news:<#v**************@TK2MSFTNGP11.phx.gbl>...
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
///
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.

Nov 20 '05 #9
Well, it is still not acting quite right.

The derived textbox object on the form is AutoSelectTextBox2.

I've got this code on the form:
====================================
Private first As Boolean

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles AutoSelectTextBox2.GotFocus
first = True
End Sub

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles
AutoSelectTextBox2.MouseDown
If first Then
Me.TextBox1.SelectAll()
End If
first = False
End Sub
=====================================

I've got this code for the ancestor:
===============================================
Option Strict On
Option Explicit On

Public Class AutoSelectTextBox
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
====================================

Is this what you intended?

Gene H.

"Cor Ligthert" <no**********@planet.nl> wrote in message news:<#v**************@TK2MSFTNGP11.phx.gbl>...
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
///
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.

Nov 20 '05 #10
There was a typo in the code I posted about an hour ago. Here's the
corrected portion only with the underscores marking the fix...

I've got this code on the form:
====================================
Private first As Boolean

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles
AutoSelectTextBox2.MouseDown
If first Then
____Me.TextBox1____.SelectAll()
End If
first = False
End Sub
=====================================

It should have been:

I've got this code on the form:
====================================
Private first As Boolean

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles
AutoSelectTextBox2.MouseDown
If first Then
____AutoSelectTextBox2____.SelectAll()
End If
first = False
End Sub
=====================================

It comes closer to working with the fix but is still not quite there.
Nov 20 '05 #11
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
Nov 20 '05 #12
After tabbing into the textbox, I have to click two times to get into edit mode.

Everything else is ok, I think.

Gene H.

"Cor Ligthert" <no**********@planet.nl> wrote in message news:<eJ**************@tk2msftngp13.phx.gbl>...
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

Nov 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Gray | last post by:
Greetings all, I'm working on a program that allows a user to enter notes in a multiline textbox. I would like to be able to read the contents of the textbox (as records - one per line) and...
1
by: amith | last post by:
Hi, I have a javascript, calendar.js which i use to enable my client to select the date. This calendar pops up on the click of a gif image. But the problem is that this poped up window is not...
5
by: malcolm | last post by:
I'm trying to make a combo box custom control (Windows Forms .NET 1.1 c#) that can behave like a label programatically at run time. This is actually a strong feature request by our customers. I...
1
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
0
by: andytsummers | last post by:
Hi I have tried to implement drag and drop by using the following code but I have several problems. 1. When you put your mouse over selected text the cursor is still the i-beam and therefore...
5
by: hufaunder | last post by:
I would like to create a specialized TextBox (WPF) that for instance only accept integers/decimals/currency/etc. Since I want it to behave like a TextBox but don't want to write one myself I would...
1
by: Mario1776 | last post by:
I have several textboxes programmatically bound to several database fields. For some reason, when I move from one particular textbox (lets say "FieldG") to another control on the form the original...
4
by: HenrikL | last post by:
Hi. I have a textbox that have a binding on it with converter and validationrules. When a validation error ouccur the foreground of the textbox will change to red cause it has a style.triggers...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.