Numericupdown forces a thousand statements to update text, every keystroke

When I use the numicupdown object with the thousand parameter set to true, it only updates the text to display commas correctly when it loses focus. Is there a way to get it to be updated every time the value changes?

+3
source share
2 answers

You will need to make an event. As we know, a thounsandseperator is triggered by a focus, we can just call it as you type.

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

So, as a custom type, we give a field in which it focuses, which is a hack to remember the formatting of a thousand separator.

. - , ... : ... , .

, , .

: Btw, ...

+1

ParseEditText(), , , NumericUpDown. , . , SelectionStart, NumericUpDown . NumericUpDown upDownEdit UpDownEdit. UpDownEdit, 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);
    }
}
0

All Articles