NumericUpDown控件 [英] NumericUpDown controls

查看:68
本文介绍了NumericUpDown控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的表单上使用了许多NumericUpDown控件,它控制了

打印机设置,例如页数。

但是我想覆盖其中一个控件的默认行为,

即当它被选中(选中)时,我想要显示的数字

被选中,所以用户可以直接输入它。

为此我正在使用事件处理程序:

private void UpDown_Enter(object sender,System.EventArgs e)

{

NumericUpDown n =(NumericUpDown)发送者;

n.Select(0,n.Value.ToString()。Length); < br $>
}


我注意到NumericUpDown只有一个Value属性,它是一个

十进制数,但是ToString方法是这总是给出实际显示在控件中的文件

?即这是否健全?

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?

推荐答案

Bonj< Bo ** @ discuss.microsoft.com>写道:
Bonj <Bo**@discussions.microsoft.com> wrote:
我注意到NumericUpDown只有一个Value属性,


实际上,它还有一个Text属性:
http ://msdn.microsoft.com/library/de...stexttopic.asp

这是一个小数,但是ToString方法总是给出文本
实际显示在控件中?


好​​吧,当调用Value属性的get-accessor时,控件

验证内容并在必要时更新可见文本。对于

的例子,如果有人将一些非数字数据粘贴到控件中(是的,

它是可能的),那么当你调用Value的get-accessor时,验证失败后,控件的'

文本将会改变。


但是,由于文本在从

get-accessor,在返回值上调用ToString应该总是给你

a字符串与
$ b中该控件的可见文本相同的值$ b时间。

即这会很健壮吗?
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?




我相信如此。


在任何情况下,对于你正在做的事情,使用

Text属性可能更合乎逻辑,它只是从TextBox派生的

中检索文本。 NumericUpDown内部使用的控件。



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.


非常好,谢谢


C#Learner写道:
excellent, thanks

"C# Learner" wrote:
Bonj< Bo ** @ discuss.microsoft.com>写道:
Bonj <Bo**@discussions.microsoft.com> wrote:
我注意到NumericUpDown只有一个Value属性,
I notice that the NumericUpDown only has a Value property,



实际上,它还有一个Text属性:
http ://msdn.microsoft.com/library/de...stexttopic.asp

这是一个小数,但是这个的ToString方法总是提供实际显示在控件中的文本?
which is a
decimal, but will the ToString method of this always give the text that is
actually displayed in the control?



那么,当调用Value属性的get-accessor时,控件
会验证内容并在必要时更新可见文本。例如,如果有人将一些非数字数据粘贴到控件中(是的,
它是可能的),那么当你调用Value的get-accessor时,控件就是
但是,由于文本在从
get-accessor返回值之前更新,因此在返回值上调用ToString应始终为您提供
一个字符串,与
时间内该控件的可见文本具有相同的值。



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.

即这是否可靠?



我是这么认为的。

无论如何,对于你正在做的事情,使用简单检索的
Text属性可能更合乎逻辑NumericUpDown内部使用TextBox派生的
控件中的文本。



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.



这实际上提出了另一个有趣的问题 - 如何实现

在派生类中,它摆脱 QUOT;基础属性之一

类?

例如,我有一些来自组合框的控件,但它们是

make没有意义,如果他们不是所有者绘制,所以我想让DrawMode

属性不可访问,而它可以在组合框中访问,所以它

传播到我的派生类。所以有可能摆脱它的价值,就像NumericUpDown已经摆脱了一样。 Text属性?


" C#Learner"写道:
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 ** @ discuss.microsoft.com>写道:
Bonj <Bo**@discussions.microsoft.com> wrote:
我注意到NumericUpDown只有一个Value属性,
I notice that the NumericUpDown only has a Value property,



实际上,它还有一个Text属性:
http ://msdn.microsoft.com/library/de...stexttopic.asp

这是一个小数,但是这个的ToString方法总是提供实际显示在控件中的文本?
which is a
decimal, but will the ToString method of this always give the text that is
actually displayed in the control?



那么,当调用Value属性的get-accessor时,控件
会验证内容并在必要时更新可见文本。例如,如果有人将一些非数字数据粘贴到控件中(是的,
它是可能的),那么当你调用Value的get-accessor时,控件就是
但是,由于文本在从
get-accessor返回值之前更新,因此在返回值上调用ToString应始终为您提供
一个字符串,与
时间内该控件的可见文本具有相同的值。



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.

即这是否可靠?



我是这么认为的。

无论如何,对于你正在做的事情,使用简单检索的
Text属性可能更合乎逻辑NumericUpDown内部使用TextBox派生的
控件中的文本。



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.



这篇关于NumericUpDown控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆