Numericupdown 强制数千分隔符每次按键更新文本

发布于 2024-10-20 19:05:59 字数 88 浏览 5 评论 0原文

当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怕倦 2024-10-27 19:05:59

你需要举办一个活动。
众所周知,千位分隔符是由焦点触发的,我们可以在键入时简单地调用它。

 private void numericUpDown1_KeyUp(object sender, KeyEventArgs e)
        {
            numericUpDown1.Focus();
            //Edit:
            numericUpDown1.Select(desiredPosition,0)
        }

因此,当用户键入时,我们将焦点重新赋予该框,这是一种召回千位分隔符格式的方法。

注意:黑客的问题是奇怪的情况,需要更多的黑客...例如:光标设置回文本的前面...您将需要另一个黑客来修复它。

尝试其他事件以找到适合您情况的事件。

编辑:顺便说一句,如果您确实想更进一步……

  1. 跟踪光标。
  2. 当调用 keyup 时,将光标放回正确的位置。
    设置 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.

 private void numericUpDown1_KeyUp(object sender, KeyEventArgs e)
        {
            numericUpDown1.Focus();
            //Edit:
            numericUpDown1.Select(desiredPosition,0)
        }

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 ...

  1. Keep Track of the cursor.
  2. Put back the cursor at the right position when keyup is called.
    Setting the cursor position in numericUpDown control
你怎么这么可爱啊 2024-10-27 19:05:59

要格式化控件中的文本值,您需要调用 ParseEditText(),该方法受保护,但可以从继承 NumericUpDown 的类访问。问题是在调用后光标将移动到第一个字符之前。为了控制光标的位置,您需要访问 NumericUpDown 不公开的 SelectionStart 属性。 NumericUpDown 仍然有一个名为 upDownEdit、类型为 UpDownEdit 的字段。 UpDownEdit 类虽然内部继承自 TextBox,但其行为与 TextBox 非常相似。因此,解决方案是继承 NumericUpDown 并使用反射来获取/设置 upDownEdit.SelectionStart 的值。您可以做以下工作:

public class NumericUpDownExt : NumericUpDown
{
    private static FieldInfo upDownEditField;
    private static PropertyInfo selectionStartProperty;
    private static PropertyInfo selectionLengthProperty;

    static NumericUpDownExt()
    {
        upDownEditField = (typeof(UpDownBase)).GetField("upDownEdit", BindingFlags.Instance | BindingFlags.NonPublic);
        Type upDownEditType = upDownEditField.FieldType;
        selectionStartProperty = upDownEditType.GetProperty("SelectionStart");
        selectionLengthProperty = upDownEditType.GetProperty("SelectionLength");
    }

    public NumericUpDownExt() : base()
    {
    }

    public int SelectionStart
    {
        get
        {
            return Convert.ToInt32(selectionStartProperty.GetValue(upDownEditField.GetValue(this), null));
        }
        set
        {
            if (value >= 0)
            {
                selectionStartProperty.SetValue(upDownEditField.GetValue(this), value, null);
            }
        }
    }

    public int SelectionLength
    {
        get
        {
            return Convert.ToInt32(selectionLengthProperty.GetValue(upDownEditField.GetValue(this), null));
        }
        set
        {
            selectionLengthProperty.SetValue(upDownEditField.GetValue(this), value, null);
        }
    }

    protected override void OnTextChanged(EventArgs e)
    {
        int pos = SelectionStart;
        string textBefore = this.Text;
        ParseEditText();
        string textAfter = this.Text;
        pos += textAfter.Length - textBefore.Length;
        SelectionStart = pos;
        base.OnTextChanged(e);
    }
}

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:

public class NumericUpDownExt : NumericUpDown
{
    private static FieldInfo upDownEditField;
    private static PropertyInfo selectionStartProperty;
    private static PropertyInfo selectionLengthProperty;

    static NumericUpDownExt()
    {
        upDownEditField = (typeof(UpDownBase)).GetField("upDownEdit", BindingFlags.Instance | BindingFlags.NonPublic);
        Type upDownEditType = upDownEditField.FieldType;
        selectionStartProperty = upDownEditType.GetProperty("SelectionStart");
        selectionLengthProperty = upDownEditType.GetProperty("SelectionLength");
    }

    public NumericUpDownExt() : base()
    {
    }

    public int SelectionStart
    {
        get
        {
            return Convert.ToInt32(selectionStartProperty.GetValue(upDownEditField.GetValue(this), null));
        }
        set
        {
            if (value >= 0)
            {
                selectionStartProperty.SetValue(upDownEditField.GetValue(this), value, null);
            }
        }
    }

    public int SelectionLength
    {
        get
        {
            return Convert.ToInt32(selectionLengthProperty.GetValue(upDownEditField.GetValue(this), null));
        }
        set
        {
            selectionLengthProperty.SetValue(upDownEditField.GetValue(this), value, null);
        }
    }

    protected override void OnTextChanged(EventArgs e)
    {
        int pos = SelectionStart;
        string textBefore = this.Text;
        ParseEditText();
        string textAfter = this.Text;
        pos += textAfter.Length - textBefore.Length;
        SelectionStart = pos;
        base.OnTextChanged(e);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文