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

A

abc my vclass

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?
 
J

Jani Järvinen [MVP]

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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
A

abc my vclass

Yes, you right, but there are many numericupdown controls (over 300), is
there any good method?
 
J

Jani Järvinen [MVP]

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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top