WinForm NumericUpDown 控件中的可变增量步骤取决于是否 Shift/Ctrl/etc。被压
我想要的有点类似于 Visual Studio(例如 VS2010)中 WinForm 设计器中发生的情况。如果我放置一个按钮并选择它并使用箭头键,它将在我通过按右键选择的任何方向上移动 5 个像素。现在,如果我在执行此操作时按住 Shift 或 Ctrl 修饰符(对不起,忘了是哪一个),则按钮一次只会移动 1 个像素。
我希望在 C# WinForm 应用程序中的 NumericUpDown 控件中发生这种情况。假设默认增量为 100.0,较小的增量为 10.0。更小的增量(如果可能)可以是 1.0。关于我该怎么做的任何提示?
希望我不需要将其作为一个单独的问题来问:我也在考虑让增量取决于输入的当前值的想法。假设我可以输入 1 到 1000 亿之间的任意金额。然后我希望默认的、小的和更小的增量值取决于输入的值。我自己可以算出准确的公式。
What I want is somewhat similar to what happens in a WinForm designer in Visual Studio, say VS2010. If I place a button and select it and use arrow keys, it will move around by 5 pixels in whatever direction I chose by pressing the right key. Now, if I hold either a Shift or a Ctrl modifier as I do that (forgot which one, sorry), then the button will move only by 1 pixel at a time.
I want this to happen with my NumericUpDown control in a C# WinForm app. Let's say a default increment is 100.0, and a smaller increment is 10.0. An even smaller increment (if possible) can be 1.0. Any hints on how can I do that?
Hopefully I do not need to ask this as a separate question: I am also toying with the idea of having the increment be dependent on the current value entered. Say, I can enter a dollar amount anywhere between 1 and 100 billion. I then want the default, small, and smaller increment values be dependent on the value entered. I can figure out the exact formula myself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 NumericUpDown 派生您自己的类并重写 UpButton() 和 DownButton() 方法:
根据需要进行调整以赋予其他键不同的效果。
Derive your own class from NumericUpDown and override the UpButton() and DownButton() methods:
Tweak as necessary to give other keys different effects.
这有点粗糙,但比其他答案简单一点(但不是更好),对于问题的第二部分,您只需将 100/10/1 替换为基于当前值的计算。
将默认增量设置为 100(或其他值)并在 NumericUpDown (nUpDown) 的 keydown 事件中
This is a bit crude but it's a little simpler than the other answer (not better though) and for the 2nd part of the question you just need to replace the 100/10/1 with a calculation based on the current value.
Set the default incrememnt to 100 (or whatever) and in the keydown event of the
NumericUpDown
(nUpDown)