質問

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