سؤال

I have a numericupdown control on a winform and I noticed while testing that not only you have the option of changing the value by pressing up and down key but also simply entering the values from your keyboard.

I don't want that. I only want the user to be able to change the numericupdown's value only by clicking the up and down buttons within the box.

So far I simply can't find a solution.

Does anyone know how to do this?

هل كانت مفيدة؟

المحلول

To disable user from editing, set Readonly property to true.

updown.ReadOnly = true;

For more tailoring, you may refer this answer.

نصائح أخرى

Sounds bad for user experience depending on the range of values you are allowing.

To do this you need to create a control with inherits from NumericUpDown and override the OnKeyPress/OnKeyDown methods.

Using updown.ReadOnly = True; does not work for me. It seems to be a reoccuring bug. But catching any changes and then undo it does. For this bind the function updown_ValueChanged() to the updown.ValueChanged attribute.

    decimal spin = 1;
    private void updown_ValueChanged(object sender, EventArgs e)
    {
        if (updown.ReadOnly)
        {
            if (updown.Value != spin)
            {
                updown.Value = spin;
            }
        }
        else spin = updown.Value;
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top