Numericupdown 强制数千分隔符每次按键更新文本
当我使用 numbericupdown 对象并将数千个分隔符设置为 true 时,它只会在失去焦点时更新文本以正确显示逗号。有没有办法在每次值更改时强制刷新?
When I use a numbericupdown object with thousandsseperator set to true it only updates the text to display the commas correctly when it loses focus. Is there a way to force it to refresh each time the value is changed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要举办一个活动。
众所周知,千位分隔符是由焦点触发的,我们可以在键入时简单地调用它。
因此,当用户键入时,我们将焦点重新赋予该框,这是一种召回千位分隔符格式的方法。
注意:黑客的问题是奇怪的情况,需要更多的黑客...例如:光标设置回文本的前面...您将需要另一个黑客来修复它。
尝试其他事件以找到适合您情况的事件。
编辑:顺便说一句,如果您确实想更进一步……
设置 numericUpDown 控件中的光标位置
You would need to do an event.
As we know, the thounsandseperator is triggered by focus we can simply call it as we type.
So as the user type, we give the box the it focus back which is a hack to recall the thousandsseperator formatting.
Note: Problems with hacks are wierd situations which calls for more hacks... e.g: Cursor sets back to the front of the text... you would need another hack to fix it.
Experiment with the other events to find the one that fits your case.
Edit: Btw, if you do want to go even further with this ...
Setting the cursor position in numericUpDown control
要格式化控件中的文本值,您需要调用 ParseEditText(),该方法受保护,但可以从继承 NumericUpDown 的类访问。问题是在调用后光标将移动到第一个字符之前。为了控制光标的位置,您需要访问 NumericUpDown 不公开的 SelectionStart 属性。 NumericUpDown 仍然有一个名为 upDownEdit、类型为 UpDownEdit 的字段。 UpDownEdit 类虽然内部继承自 TextBox,但其行为与 TextBox 非常相似。因此,解决方案是继承 NumericUpDown 并使用反射来获取/设置 upDownEdit.SelectionStart 的值。您可以做以下工作:
To format the text value in your control you'd need a call to ParseEditText() which is protected but can be accessed from a class which inherits NumericUpDown. The problem is after your call the cursor will move before the first character. In order to control the position of the cursor you need access to the SelectionStart property which NumericUpDown don't expose. NumericUpDown still has a field named upDownEdit of type UpDownEdit. The UpDownEdit class although internal inherits from TextBox and behave much like one. So a solution would be to inherit from NumericUpDown and use reflection to get/set the value of upDownEdit.SelectionStart. Here is something you can work on: