How to make NumericUpDown ReadOnly

别说谁变了你拦得住时间么 提交于 2020-04-10 08:35:24

问题


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.


回答1:


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

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

I hope it helps.




回答2:


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.



来源:https://stackoverflow.com/questions/28621403/how-to-make-numericupdown-readonly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!