472,127 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to set all numericupdown auto-select text when got focus?

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 set all numericupdown
autoselect text when gotfocus?
Dec 25 '05 #1
3 29465
Hi ABC,
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 set all
numericupdown autoselect text when gotfocus?


I'm not exactly sure if I understood you 100% clearly, but I assume you want
to have a NumericUpDown to select the whole value (text) when one tabs into
the control, like the TextBox control would function.

The NumericUpDown control has an (overloaded) method called Select, to which
you can pass in two integers: the index of the first character to be
selected, and then the number of characters to select. To select all text,
you would start from index zero, and the length would then be the number of
characters in the value.

If you call the Select method in the Enter event of the NumericUpDown
control for example like this, you should be able to emulate the TextBox
functionality:

-----------------------------
private void numericUpDown1_Enter(object sender, EventArgs e)
{
numericUpDown1.Select(0, numericUpDown1.ToString().Length);
}
-----------------------------

Note that the NumericUpDown control doesn't have a Text property, since the
Value property is used instead. Also, if you know that there will be, say,
maximum of three numbers in the value (maximum integer value 999), you could
also always call Select(0,3). It doesn't hurt if there are less than three
numbers in the value currently.

Hope this helps! Merry Christmas as well!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Dec 26 '05 #2
Yes, you right, but there are many numericupdown controls (over 300), is
there any good method?
"Jani Järvinen [MVP]" <ja***@removethis.dystopia.fi> ¼¶¼g©ó¶l¥ó·s»D:Oy**************@TK2MSFTNGP14.phx.g bl...
Hi ABC,
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 set all
numericupdown autoselect text when gotfocus?


I'm not exactly sure if I understood you 100% clearly, but I assume you
want to have a NumericUpDown to select the whole value (text) when one
tabs into the control, like the TextBox control would function.

The NumericUpDown control has an (overloaded) method called Select, to
which you can pass in two integers: the index of the first character to be
selected, and then the number of characters to select. To select all text,
you would start from index zero, and the length would then be the number
of characters in the value.

If you call the Select method in the Enter event of the NumericUpDown
control for example like this, you should be able to emulate the TextBox
functionality:

-----------------------------
private void numericUpDown1_Enter(object sender, EventArgs e)
{
numericUpDown1.Select(0, numericUpDown1.ToString().Length);
}
-----------------------------

Note that the NumericUpDown control doesn't have a Text property, since
the Value property is used instead. Also, if you know that there will be,
say, maximum of three numbers in the value (maximum integer value 999),
you could also always call Select(0,3). It doesn't hurt if there are less
than three numbers in the value currently.

Hope this helps! Merry Christmas as well!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/

Dec 26 '05 #3
Hello!
Yes, you right, but there are many numericupdown controls (over 300), is
there any good method?


Good question. The easiest way to solve this issue is to point all the Enter
event handlers of those NumericUpDown's to a single event handler.

That is, if you are in Visual Studio, you can double-click an event in the
Properties window to create an event handler for the selected control. In
all other NumericUpDown controls, you would not double-click the value
column in the Properties window, but instead select an existing event
handler from the list.

In code, this would look like the following:

------------------------
numericUpDown1.Enter += new System.EventHandler(numericUpDown1_Enter);
numericUpDown2.Enter += new System.EventHandler(numericUpDown1_Enter);
------------------------

Note how both Enter event handlers now point to the same code, i.e. the
numericUpDown1_Enter method.

The next question is obviously how to be able to determine which
NumericUpDown is in question. You can use code similar to the following:

------------------------
private void numericUpDown1_Enter(object sender, EventArgs e)
{
(sender as NumericUpDown).Select(0, 10);
}
------------------------

As I said, this is probably the easiest solution, but not the most elegant
one. If you have 300 controls (hopefully not on the same form!) I would
suggest developing a custom control that inherits from NumericUpDown, and
implements the functionality you want without writing custom event handlers.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Dec 26 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Koert | last post: by
2 posts views Thread by Patrick Blackman | last post: by
1 post views Thread by Tantra Veda | last post: by
2 posts views Thread by ReMEn | last post: by
reply views Thread by Koert | last post: by
reply views Thread by stx | last post: by
2 posts views Thread by bnob | last post: by
16 posts views Thread by tommaso.gastaldi | last post: by
1 post views Thread by =?Utf-8?B?R3VzIENodWNo?= | last post: by
reply views Thread by leo001 | last post: by

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.