获取 NumericUpDown 的值会移动插入符位置,我可以阻止这个吗?
我注意到,当我在 C# 应用程序中请求 NumericUpDown
控件的 Value
时,插入符号会重置为位置 0。这很烦人,因为我的应用程序会定期抓取控件的值,因此如果在用户键入内容时发生这种情况,插入符号会意外移动,从而扰乱他们的输入。
有没有办法防止这种情况,或者解决方法?它似乎没有 SelectionStart
属性,否则我可以让轮询过程保存插入符号位置,获取值,然后将其设置回来作为一个不错的解决方法。
I've noticed that when I request the Value
of a NumericUpDown
control in a C# app, the caret is reset to position 0. This is annoying because my app is periodically grabbing the value of the control, so if this happens while a user is typing into it the caret unexpectedly moves, messing with their input.
Is there a way to prevent this, or a workaround? It doesn't seem to have a SelectionStart
property, otherwise I could have the polling process save the caret position, get the value, then set it back as a decent workaround.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
中间的按键会扰乱文本中的打字和光标,因为获取值会扰乱插入符位置。因此,我们采用了不确定的解决方案:获取
textboxbase
并自行设置插入符号的值。Intervening key up messes up the typing and the cursor in the text, as getting the value messes up the caret position. Hence the iffy solution of getting the
textboxbase
and setting the value of the caret ourselves.我可以用小数点重现错误。在计时器滴答事件中(或在提取值的任何位置),尝试添加以下代码:
您无法获取 SelectionStart,但如果您从当前字符串的末尾进行选择并设置
selection length< /code> 为 0,它应该将插入符号保持在正确的位置。
I can reproduce the error with the decimal point. In your timer tick event (or wherever you're pulling in the value), try adding this code:
You can't get the SelectionStart, but if you select from the end of the current string and set the
selection length
to 0, it should keep the caret in the correct place.