How to make NumericUpDown ReadOnly
Asked Answered
A

3

14

I would like to make WinForms NumericUpDown control non-editable or at least spin control should be disabled. I tried setting the control readonly but using mouse scroll values are changing. Please help me to achieve this.

Amadoamador answered 20/2, 2015 at 4:0 Comment(0)
R
25

Try the following in order to set the numeric up/down as read-only:

numericUpDown1.ReadOnly = true;    
numericUpDown1.Increment = 0;

I hope it helps.

Riobard answered 20/2, 2015 at 4:12 Comment(0)
D
10

Another alternative is to subclass NumericUpDown and override the DownButton and UpButton methods so they return early when ReadOnly is set:

public override void DownButton()
{
   if(this.ReadOnly)
      return;

   base.DownButton();
}

public override void UpButton()
{
   if(this.ReadOnly)
      return;

   base.UpButton();
}

This appears to also prevent up/down arrow presses and mouse scrolls from changing the value.

The benefit of this approach is that you can now data bind the ReadOnly property on the control and expect it to make the value read-only instead of just the text area.

Dado answered 4/9, 2015 at 20:20 Comment(0)
D
1

Yet another alternative: the NumericUpDown control has two sub-controls, "UpDownButtons" and "UpDownEdit". We want to disable the first one:

numericUpDown1.ReadOnly = true;
numericUpDown1.Controls[0].Enabled = false; // Controls[0] is the spinner

Or set Controls[0].Visible = false to completely hide it.

Delict answered 21/2, 2022 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.