在 NumericUpDown 控件内的数字后面有文本
WinForms 中是否可以在 NumericUpDown 控件内显示文本?例如,我想显示我的 numericupdown 控件中的值是微安,所以它应该像“1 uA”。
谢谢。
Is it possible in WinForms to show a text inside a NumericUpDown control? For example I want to show the value in my numericupdown control is micro ampers so it should be like "1 uA".
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
标准控件中没有内置此类功能。但是,通过创建一个继承自 NumericUpDown 类并覆盖
UpdateEditText
方法 相应地设置数字格式。例如,您可能有以下类定义:
或者,要获得更完整的实现,请参阅此示例项目: NumericUpDown with单位度量
There's no such functionality built into the standard control. However, it's fairly easy added by creating a custom control that inherits from the
NumericUpDown
class and overrides theUpdateEditText
method to format the number accordingly.For example, you might have the following class definition:
Or, for a more complete implementation, see this sample project: NumericUpDown with unit measure
使用CodeGray的答案,Fabio的< /a> 关于 ValidateEditText 失败的评论,以及 NumericUpDown 文档 我已经找到了使用简单的 NumericUpDownWithUnit 组件。您可以按原样复制/粘贴:
Using CodeGray's answer, Fabio's comment about it failing ValidateEditText, and the NumericUpDown documentation I've come up with a simple NumericUpDownWithUnit component. You can copy/paste as is:
这是我用来显示前缀为 0x 的至少 2 位十六进制 NumericUpDown 的方法。
它将文本放入控件中,并通过使用 .Net 提供的方法避免使用“debounce”
字段
更改文本
Here's what I used for showing at least 2 digits for a hexadecimal NumericUpDown that are prefixed by 0x.
It puts text in the control and avoids using "debounce" by using the .Net provided
field
ChangingText
我最近偶然发现了这个问题,并找到了 Cody Gray 的精彩答案。我利用它来发挥自己的优势,但最近对他的答案中的一条评论产生了共鸣,其中一条评论谈到,如果后缀仍然存在,文本将如何验证失败。我为此创建了一个可能不太专业的快速解决方案。
基本上,
this.Text
字段是读取数字的。一旦找到数字,它们就会被放入
this.Text
中,但是需要进行去抖动或任何您想要调用的内容,以确保我们不会创建堆栈溢出 。一旦只有数字的新文本进入,就会调用正常的
ParseEditText();
和UpdateEditText();
来完成该过程。这不是最资源友好或最有效的解决方案,但当今大多数现代计算机应该完全可以满足这一要求。
您还会注意到,我创建了一个用于更改后缀的属性,只是为了更方便地在编辑器中使用。
如果有问题,请随时改进我的答案或纠正我,我是一个自学成才的程序员,仍在学习。
I recently stumbled across this issue and found Cody Gray's awesome answer. I used it to my advantage but recently resonated with one of the comments on his answer talking about how the text will fail validation if the suffix is still there. I've created a probably not-so-professional quick fix for this.
Basically the
this.Text
field is read for numbers.Once the numbers are found they are put into
this.Text
, but a debounce or whatever you want to call it is needed to make sure we don't create a stack overflow.Once the new text with only number is in, the normal
ParseEditText();
andUpdateEditText();
are called to complete the process.This isn't the most resource friendly or efficient solution, but most modern computers today should be perfectly fine with that.
Also you'll notice I've created a property for changing the suffix just for easier use in editor.
Please feel free to better my answer or correct me if something is wrong, I'm a self-taught programmer still learning.
您的操作在对象发生更改后开始,您必须从属性部分双击 ValueChanged。
在代码中执行此操作后,您可以看到此代码:
在 //Body 端,您可以编写代码和控制器。正如其他人所说。
your action start after change on you Object, well you have to Double Click on ValueChanged from properties part.
After this Action In Code You Can See This Code:
In //Body side you can write your Code and Your Controller. As Others person said.