By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,326 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,326 IT Pros & Developers. It's quick & easy.

select text in textbox

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Hi,

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

Regards,

Cerebrus.

Mar 31 '06 #6

P: n/a
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

P: n/a
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

P: n/a
>> 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 discussion thread is closed

Replies have been disabled for this discussion.