473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

NumericUpDown controls

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?
Nov 16 '05 #1
10 7107
Bonj <Bo**@discussions.microsoft.com> wrote:
I notice that the NumericUpDown only has a Value property,
Actually, it also has a Text property:
http://msdn.microsoft.com/library/de...stexttopic.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.
Nov 16 '05 #2
excellent, thanks

"C# Learner" wrote:
Bonj <Bo**@discussions.microsoft.com> wrote:
I notice that the NumericUpDown only has a Value property,


Actually, it also has a Text property:
http://msdn.microsoft.com/library/de...stexttopic.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.

Nov 16 '05 #3
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# Learner" wrote:
Bonj <Bo**@discussions.microsoft.com> wrote:
I notice that the NumericUpDown only has a Value property,


Actually, it also has a Text property:
http://msdn.microsoft.com/library/de...stexttopic.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.

Nov 16 '05 #4
C# Learner <cs****@learner.here> wrote:
http://msdn.microsoft.com/library/de...stexttopic.asp


Sorry, that should be: http://tinyurl.com/4lz5q
Nov 16 '05 #5
Bonj <Bo**@discussions.microsoft.com> wrote:
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
member. See <http://www.yoda.arachsys.com/csharp/faq/#override.new> for
details.
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. :-)
Nov 16 '05 #6
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:52**********************************@microsof t.com...
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?

Nov 16 '05 #7
> 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.
Nov 16 '05 #8
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?


Try compiling:

MessageBox.Show(myNumericUpDown.Text);

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

Nov 16 '05 #9
Bonj <Bo**@discussions.microsoft.com> wrote:
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.


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

MessageBox.Show(myNumericUpDown.Text);
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.


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).
Nov 16 '05 #10
Bonj <Bo**@discussions.microsoft.com> wrote:
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?


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)
Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
2
by: Patrick Blackman | last post by:
Can anyone tell me how to get the numericupdown control to support themes?
0
by: Sachin | last post by:
On a form I have two NumericUpDown controls to show "nUdFrom" and "nUdTo" values. I want to add validation code for the same, like value in "nUdFrom" should alway me less than that of in "nUdTo"...
2
by: bnob | last post by:
It is possible to format the numeric value of the NumericUpDown control For example : display numbers in 2 digits, instead of 1 display 01, 0 -> 00 , 9 -> 09 Any idea -- Ceci est une...
3
by: abc my vclass | last post by:
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...
16
by: tommaso.gastaldi | last post by:
Hello, A probably dumb question... does anyone know hot to avoid that if one keep the mouse pressed on an arrow of the numericUpDown it continues to fire events (it uses evidently a timer) ?...
1
by: =?Utf-8?B?R3VzIENodWNo?= | last post by:
How do I set the Maximum values of a NumericUpDown Control? If OptionBox TermsMonths.Checked = True Then Control.Maximum() = 360 Else Control.Maximum() = 30 -- Thank You Gus Chuchanis
2
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hello experts, I posted a disturbing question in here:...
7
by: pamela fluente | last post by:
My numericUpDowns show numbers in the format 1.500,56 (italy). Instead, I need *invariantly* that they show and accept the format 1,500.56, as in the USA. What's the right way to do that? I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.