Google Groups unterstützt keine neuen Usenet-Beiträge oder ‑Abos mehr. Bisherige Inhalte sind weiterhin sichtbar.

Re: SelectAll() within TextBox.Enter event

61 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Sijin Joseph

ungelesen,
11.08.2004, 10:58:3111.08.04
an
Have you tried the Textbox.GotFocus event?

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


"Guido Kraus" <gu...@somewhere.com> wrote in message
news:C062ADC4-FD8A-42BC...@microsoft.com...
> I would like to select all text within a TextBox every time the TextBox
gets
> focus (like IE's address bar). This seems very easy: Just wire up the
Enter
> event and use the SelectAll() method. However, it only works if the user
uses
> the TAB key to enter the TextBox. If the user clicks on the TextBox to
enter
> it the TextBox ignores the SelectAll() method.
>
> Any easy way to overcome this?
>
> Thanks for your ideas,
> Guido


Jeffrey Tan[MSFT]

ungelesen,
11.08.2004, 21:42:3311.08.04
an
Hi Guido,

Based on my understanding, you want to make the textbox select all the text
when it gets focus.

==================================
You may wire up both the TextBox.Enter and TextBox.KeyDown events to get
what you want, like this:
private void textBox1_Enter(object sender, System.EventArgs e)
{
this.textBox1.SelectAll();
}

private void textBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.textBox1.SelectAll();
}

================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Guido Kraus

ungelesen,
15.08.2004, 07:43:0215.08.04
an
Hi Jeffrey


> Based on my understanding, you want to make the textbox select all the text
> when it gets focus.

Yes it's as simple as that. In fact I'd like to have a similar behavior as
Internet Explorer's address bar. The first time you click on it all text is
selected. The second time you click on it the selection is gone and the text
insertion point moves to where you have clicked. Therefore your suggestion to
use the MouseDown event does not work too well.

However I've found the following workaround:

//------------------------------------
private bool _selectAll;

private void textBox1_Enter(object sender, System.EventArgs e) {

_selectAll = true;
textBox1.SelectAll();
}

private void textBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e) {

if (_selectAll)
textBox1.SelectAll();
}

private void textBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e) {
_selectAll = false;
}

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e) {
_selectAll = false;
}
//------------------------------------

The problem seems so simple but I need an instance variable and three events
to solve it. In my opinion this is a design flaw of the .Net framework. It
seems as if the event processing order is

Enter
GotFocus
MouseDown

where it should be

Enter
MouseDown
GotFocus

Guido

Jeffrey Tan[MSFT]

ungelesen,
16.08.2004, 04:02:1216.08.04
an
Hi Guido,

Thanks for your feedback.

Yes, my code snippet is not perfect, it only leads you to the right
direction :-). I am also glad you figure out the following things.

Yes, the .Net winform events order is:
Enter
GotFocus
MouseDown

I think this order makes sense: when we move mouse to a textbox and click
into it, first, when the mouse enter its client, the Enter event will fire,
then when click, first the TextBox will get focus, because without getting
focus, a control(or window) can not get keyboard stroke or mouse event. So
only after getting focus, it can response mousedown.

0 neue Nachrichten