473,386 Members | 2,114 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,386 software developers and data experts.

select text in textbox

cj
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net
when the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all
the text (thank you both) but using the mousedown or mouseup events
doesn't work the way I want it to. I want it to work like the address
bar in MS Explorer (just an example of a "text box" that works like I
want -- this has nothing to do with the internet). When the box
receives focus with a click everything in the box is selected. If you
then click in the box again everything is not selected and the cursor
goes to the place in the contents where you selected. Like I said I
used to do that by selecting all the text in the got focus event. It
worked well because on your second click the textbox is not receiving
focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?
Mar 30 '06 #1
8 21843
Something like this seems to do what you want:

public Form1()
{
InitializeComponent();
textBox1.Text = "textBox1";
textBox2.Text = "textBox2";
shouldSelect = true;
textBox1.GotFocus += new System.EventHandler(this.textBox1_GotFocus);
textBox1.LostFocus += new System.EventHandler(this.textBox1_LostFocus);
textBox1.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.textBo x1_MouseUp);
}

private bool shouldSelect;

private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
if (shouldSelect)
{
textBox1.SelectAll();
shouldSelect = false;
}
}

private void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1.SelectAll();
}

private void textBox1_LostFocus(object sender, EventArgs e)
{
shouldSelect = true;
}

/claes

"cj" <cj@nospam.nospam> wrote in message
news:OU**************@TK2MSFTNGP14.phx.gbl...
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net when
the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all the
text (thank you both) but using the mousedown or mouseup events doesn't
work the way I want it to. I want it to work like the address bar in MS
Explorer (just an example of a "text box" that works like I want -- this
has nothing to do with the internet). When the box receives focus with a
click everything in the box is selected. If you then click in the box
again everything is not selected and the cursor goes to the place in the
contents where you selected. Like I said I used to do that by selecting
all the text in the got focus event. It worked well because on your
second click the textbox is not receiving focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?

Mar 30 '06 #2
cj
I don't know. That's C code and I'm not quite smart enough to know
where and what to change to put it in a vb program so I can see what it
does.

Darn this is irritating. It worked just fine in VB4.

Claes Bergefall wrote:
Something like this seems to do what you want:

public Form1()
{
InitializeComponent();
textBox1.Text = "textBox1";
textBox2.Text = "textBox2";
shouldSelect = true;
textBox1.GotFocus += new System.EventHandler(this.textBox1_GotFocus);
textBox1.LostFocus += new System.EventHandler(this.textBox1_LostFocus);
textBox1.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.textBo x1_MouseUp);
}

private bool shouldSelect;

private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
if (shouldSelect)
{
textBox1.SelectAll();
shouldSelect = false;
}
}

private void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1.SelectAll();
}

private void textBox1_LostFocus(object sender, EventArgs e)
{
shouldSelect = true;
}

/claes

"cj" <cj@nospam.nospam> wrote in message
news:OU**************@TK2MSFTNGP14.phx.gbl...
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net when
the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all the
text (thank you both) but using the mousedown or mouseup events doesn't
work the way I want it to. I want it to work like the address bar in MS
Explorer (just an example of a "text box" that works like I want -- this
has nothing to do with the internet). When the box receives focus with a
click everything in the box is selected. If you then click in the box
again everything is not selected and the cursor goes to the place in the
contents where you selected. Like I said I used to do that by selecting
all the text in the got focus event. It worked well because on your
second click the textbox is not receiving focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?


Mar 30 '06 #3
cj wrote:
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net
when the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all
the text (thank you both) but using the mousedown or mouseup events
doesn't work the way I want it to. I want it to work like the address
bar in MS Explorer (just an example of a "text box" that works like I
want -- this has nothing to do with the internet). When the box
receives focus with a click everything in the box is selected. If you
then click in the box again everything is not selected and the cursor
goes to the place in the contents where you selected. Like I said I
used to do that by selecting all the text in the got focus event. It
worked well because on your second click the textbox is not receiving
focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?


Hello again cj,

I understand your frustration, why did it work perfectly in non-NET VB??

The closest I can come to what you want is with the following code
(watch for line-wrapping!) -
Dim blTextBoxJustReceivedFocus As Boolean

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
blTextBoxJustReceivedFocus = True
TextBox1.SelectAll() 'Experiment with removing this line.
End Sub

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

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
blTextBoxJustReceivedFocus = False
End Sub
I hope someone else will come-up with a cleaner solution for you.

Regards,

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Mar 30 '06 #4
Hi CJ,

C# and VB.NET aren't that different, now.

For your benefit, here's Claes' code converted to VB.NET.

--------------------------------------------

Private shouldSelect As Boolean

Private Sub textBox1_MouseUp(ByVal sender As Object, ByVal e As
MouseEventArgs)
If shouldSelect = True Then
textBox1.SelectAll()
shouldSelect = False
End If
End Sub

Private Sub textBox1_GotFocus(ByVal sender As Object, ByVal e As
EventArgs)
textBox1.SelectAll()
End Sub

Private Sub textBox1_LostFocus(ByVal sender As Object, ByVal e As
EventArgs)
shouldSelect = True
End Sub

--------------------------------------------

BTW, I see you mentioned that SelectAll() does not work for you in the
GotFocus event. I see no reason for it, not to work. Claes has also
used that method in the GotFocus event. Could you post the code you're
using ?

Hope this will help you,

Regards,

Cerebrus.

Mar 30 '06 #5
Hi,

Have you tried to trap the Enter and Leave events instead of GotFocus
and LostFocus ?

Regards,

Cerebrus.

Mar 31 '06 #6
CJ,

I have forever only one answer on your kind of question.

I never use "down" or "press" events.

I use forever "up" events.

(At least they give me more information what was pressed)

I hope this helps,

Cor

"cj" <cj@nospam.nospam> schreef in bericht
news:OU**************@TK2MSFTNGP14.phx.gbl...
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net when
the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all the
text (thank you both) but using the mousedown or mouseup events doesn't
work the way I want it to. I want it to work like the address bar in MS
Explorer (just an example of a "text box" that works like I want -- this
has nothing to do with the internet). When the box receives focus with a
click everything in the box is selected. If you then click in the box
again everything is not selected and the cursor goes to the place in the
contents where you selected. Like I said I used to do that by selecting
all the text in the got focus event. It worked well because on your
second click the textbox is not receiving focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?

Mar 31 '06 #7
Ahh, sorry about that. Missed that this was a VB.NET group. Guess that's
what happens when one participate in both C# and VB.NET forums :-)
Anyway, Cerebrus was kind enough to translate it.

/claes

"cj" <cj@nospam.nospam> wrote in message
news:u9**************@TK2MSFTNGP10.phx.gbl...
I don't know. That's C code and I'm not quite smart enough to know where
and what to change to put it in a vb program so I can see what it does.

Darn this is irritating. It worked just fine in VB4.

Claes Bergefall wrote:
Something like this seems to do what you want:

public Form1()
{
InitializeComponent();
textBox1.Text = "textBox1";
textBox2.Text = "textBox2";
shouldSelect = true;
textBox1.GotFocus += new System.EventHandler(this.textBox1_GotFocus);
textBox1.LostFocus += new
System.EventHandler(this.textBox1_LostFocus);
textBox1.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.textBo x1_MouseUp);
}

private bool shouldSelect;

private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
if (shouldSelect)
{
textBox1.SelectAll();
shouldSelect = false;
}
}

private void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1.SelectAll();
}

private void textBox1_LostFocus(object sender, EventArgs e)
{
shouldSelect = true;
}

/claes

"cj" <cj@nospam.nospam> wrote in message
news:OU**************@TK2MSFTNGP14.phx.gbl...
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net
when the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all
the text (thank you both) but using the mousedown or mouseup events
doesn't work the way I want it to. I want it to work like the address
bar in MS Explorer (just an example of a "text box" that works like I
want -- this has nothing to do with the internet). When the box
receives focus with a click everything in the box is selected. If you
then click in the box again everything is not selected and the cursor
goes to the place in the contents where you selected. Like I said I
used to do that by selecting all the text in the got focus event. It
worked well because on your second click the textbox is not receiving
focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?


Mar 31 '06 #8
>> Anyway, Cerebrus was kind enough to translate it.

No problem, Claes... It happens to me as well, all the time. ;-)

Regards,

Cerebrus.

Mar 31 '06 #9

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

Similar topics

3
by: Phill | last post by:
I thought when you tabbed into a textbox that the text it contained was automatically highlighted. This use to be the default in win32. Is there an easy way to do this in .NET. I don't want this...
1
by: Mad Scientist Jr | last post by:
I'm stuck trying to work with a HTML <SELECT> control and javascript (similar to DualList but that control doesn't offer enough options to totally control the text on the buttons and control, also...
2
by: Cindy M -WordMVP- | last post by:
Hi all While working my way through a VB.NET exercise, I came across something that didn't do what the text said it should and I'm wondering if it's supposed to behave this way, or not (and...
4
by: Brett | last post by:
I have a textbox with a heigth of 384. I put a lot of text into it and must scroll down to see it all. When I do Ctrl + A, it doesn't highlight all of the text. I have to manually drag my mouse...
3
by: abc my vclass | last post by:
My win-form have many numericupdown controls to applied. But numericupdown control don't like textbox, text box control can automatic selected text when got focus. Is there any method can let me...
4
by: Dabbler | last post by:
Is there a way to mark the text in a TextBox control as selected so when the user types a new value the existing text is replaced? Thanks
12
by: Brano | last post by:
Hi I am using vb.net 2005 this is a windows application I am using this functionality in another project I have created this simple project to show the problem Basically I have two forms: ...
13
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.