Home All Groups Group Topic Archive Search About

select text in textbox

Author
30 Mar 2006 1:27 PM
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?

Author
30 Mar 2006 2:45 PM
Claes Bergefall
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.textBox1_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

Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:OUpyz2$UGHA.4300@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?
Author
30 Mar 2006 9:07 PM
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:
Show quoteHide quote
> 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.textBox1_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:OUpyz2$UGHA.4300@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?
>
>
Author
30 Mar 2006 10:28 PM
Cerebrus
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.
Author
31 Mar 2006 4:42 AM
Cerebrus
Hi,

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

Regards,

Cerebrus.
Author
31 Mar 2006 2:58 PM
Claes Bergefall
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

Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:u9VVE4DVGHA.4276@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.textBox1_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:OUpyz2$UGHA.4300@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?
>>
Author
31 Mar 2006 9:07 PM
Cerebrus
>> Anyway, Cerebrus was kind enough to translate it.

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

Regards,

Cerebrus.
Author
30 Mar 2006 10:26 PM
ShaneO
cj wrote:
Show quoteHide quote
> 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.
Author
31 Mar 2006 8:39 AM
Cor Ligthert [MVP]
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

Show quoteHide quote
"cj" <cj@nospam.nospam> schreef in bericht
news:OUpyz2$UGHA.4300@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?