【问题标题】:How to make NumericUpDown ReadOnly如何使 NumericUpDown 只读
【发布时间】:2015-02-20 04:00:49
【问题描述】:

我想让 WinForms NumericUpDown 控件不可编辑,或者至少应该禁用旋转控件。我尝试将控件设置为只读,但使用鼠标滚动值正在改变。请帮助我实现这一目标。

【问题讨论】:

    标签: winforms readonly mousewheel numericupdown


    【解决方案1】:

    尝试以下操作以将数字向上/向下设置为只读:

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

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      另一种选择是继承NumericUpDown 并覆盖DownButtonUpButton 方法,以便在设置ReadOnly 时提前返回:

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

      这似乎还可以防止向上/向下箭头键和鼠标滚动更改值。

      这种方法的好处是,您现在可以对控件上的 ReadOnly 属性进行数据绑定,并期望它使值成为只读的,而不仅仅是文本区域。

      【讨论】:

        【解决方案3】:

        还有另一种选择:NumericUpDown 控件有两个子控件,“UpDownButtons”和“UpDownEdit”。我们要禁用第一个:

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

        或者设置 Controls[0].Visible = false 来完全隐藏它。

        【讨论】:

          猜你喜欢
          • 2011-08-24
          • 2018-08-07
          • 2019-09-01
          • 2017-12-19
          • 2019-12-22
          • 2018-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多