NumericUpDown controls

G

Guest

I'm using a number of NumericUpDown controls on my form, which controls
printer settings such as number of pages.
However I want to override one of the default behaviours of the control,
namely when it is tabbed into (selected), I want the number that is displayed
in it to become selected, so the user can just type straight over it.
For this I'm using the event handler:

private void UpDown_Enter(object sender, System.EventArgs e)
{
NumericUpDown n = (NumericUpDown)sender;
n.Select(0, n.Value.ToString().Length);
}

I notice that the NumericUpDown only has a Value property, which is a
decimal, but will the ToString method of this always give the text that is
actually displayed in the control? i.e. will this be robust?
 
C

C# Learner

Bonj said:
I notice that the NumericUpDown only has a Value property,

Actually, it also has a Text property:
http://msdn.microsoft.com/library/d...ystemwindowsformsupdownbaseclasstexttopic.asp
which is a
decimal, but will the ToString method of this always give the text that is
actually displayed in the control?

Well, when calling the get-accessor of the Value property, the control
validates the content and updates the visible text if necessary. For
example, if someone pasted some non-numerical data into the control (yes,
it's possible), then when you call the get-accessor of Value, the control's
text will change after validation failed.

However, since the text is updated before the value is returned from the
get-accessor, calling ToString on the returned value should always give you
a string with the same value as the control's visible text at that point in
time.
i.e. will this be robust?

I believe so.

In any case, for what you're doing, it's probably more logical to use the
Text property which simply retrieves the text from the TextBox-derived
control used internally by the NumericUpDown.
 
G

Guest

excellent, thanks

C# Learner said:
Actually, it also has a Text property:
http://msdn.microsoft.com/library/d...ystemwindowsformsupdownbaseclasstexttopic.asp


Well, when calling the get-accessor of the Value property, the control
validates the content and updates the visible text if necessary. For
example, if someone pasted some non-numerical data into the control (yes,
it's possible), then when you call the get-accessor of Value, the control's
text will change after validation failed.

However, since the text is updated before the value is returned from the
get-accessor, calling ToString on the returned value should always give you
a string with the same value as the control's visible text at that point in
time.


I believe so.

In any case, for what you're doing, it's probably more logical to use the
Text property which simply retrieves the text from the TextBox-derived
control used internally by the NumericUpDown.
 
G

Guest

That actually raises another interesting question - how do you implement that
in a derived class whereby it "gets rid" of one of the properties of the base
class?
For example, i have some controls that are derived from comboboxes, but they
make no sense if they are not owner drawn, so I want to make the DrawMode
property inaccessible, whereas it is accessible in the combobox, so it
propogates through to my derived class. So is it possible to "get rid" of it,
like the NumericUpDown has "got rid" of the Text property?
 
C

C# Learner

Bonj said:
That actually raises another interesting question - how do you implement that
in a derived class whereby it "gets rid" of one of the properties of the base
class?

You can't, really. The closest you can get is to create a new public
member with the same name as the inherited one, thus "hiding" the inherited
For example, i have some controls that are derived from comboboxes, but they
make no sense if they are not owner drawn, so I want to make the DrawMode
property inaccessible, whereas it is accessible in the combobox, so it
propogates through to my derived class.
So is it possible to "get rid" of it,

Not through inheritance, no. You could use encapsulation to achieve this,
however -- create a UserControl which contains the particular control and
only expose certain members of that control.
like the NumericUpDown has "got rid" of the Text property?

NumericUpDown didn't get rid of it. However, it apparently decorated the
member with some attributes which cause it to not show up in the form
designer property grid and to not show up in intellisense.

Try compiling:

MessageBox.Show(myNumericUpDown.Text);

and you'll see that it does in fact exist. :)
 
N

Nicholas Paldino [.NET/C# MVP]

Bonj,

Why not use the Text property? I would assume that it would return the
actual text in the window, and not whatever is returned by ToString. You
can't always use ToString, because if a format is applied to the number (say
there are separators like commas for every three digits), then the text in
the box will be different from what is returned from ToString.

Hope this helps.
 
G

Guest

Bonj,
Why not use the Text property?

Because I wasn't thinking out of the box - the Text property is a property
of the base class (UpDownBase), and not a property of the actual
NumericUpDown control. Now I know to cast to UpDownBase, I can retrieve the
Text property no problem.
I would assume that it would return the
actual text in the window, and not whatever is returned by ToString. You
can't always use ToString, because if a format is applied to the number (say
there are separators like commas for every three digits), then the text in
the box will be different from what is returned from ToString.

Exactly, that's precisely what I was hoping to avoid. But I think I have
done, as if you put non-numeric values into it, then it still replaces it
with either zero or what was in there before and doesn't throw any
exceptions, so I'm happy with it.
 
G

Guest

NumericUpDown didn't get rid of it. However, it apparently decorated the
member with some attributes which cause it to not show up in the form
designer property grid and to not show up in intellisense.

Yeah - that would be enough, for it not to show up in intellisense. I don't
actually *need* to get rid of it, as in absolutely require its death beyond
all circumstances. It would just be nice to know you're not supposed to use
it.

Any idea how you do that?
 
C

C# Learner

Bonj said:
Because I wasn't thinking out of the box - the Text property is a property
of the base class (UpDownBase), and not a property of the actual
NumericUpDown control. Now I know to cast to UpDownBase, I can retrieve the
Text property no problem.

Text *is* a member of NumericUpDown, and it overrides UpDownBase.Text.
Try:

MessageBox.Show(myNumericUpDown.Text);
Exactly, that's precisely what I was hoping to avoid. But I think I have
done, as if you put non-numeric values into it, then it still replaces it
with either zero or what was in there before and doesn't throw any
exceptions, so I'm happy with it.

But the point here is that if you were to set
NumericUpDown.ThousandSeparators to true, for example, then the result of
Value.ToString might be different from Text (something I inadvertently
neglected to mention earlier).
 
C

C# Learner

Bonj said:
Yeah - that would be enough, for it not to show up in intellisense. I don't
actually *need* to get rid of it, as in absolutely require its death beyond
all circumstances. It would just be nice to know you're not supposed to use
it.

Any idea how you do that?

Hide from property grid:

Browsable(false)

Hide from intellisense (only works from a different project (in VS.NET),
IIRC):

System.ComponentModel.EditorBrowsable(
System.ComponentModel.EditorBrowsableState.Never)
 

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