【问题标题】:Set fixed Values in a NumericUpDown control在 NumericUpDown 控件中设置固定值
【发布时间】:2015-12-11 10:44:53
【问题描述】:

与为NumericUpDown 设置最小值和最大值相比,如何让用户仅在不同的预设值之间进行选择? (例如 5、8、10、15、20、25)。

编辑:我可以通过 1) 单击 numericUpDown 的箭头和 2) 使用键盘手动更改值来区分 ValueChanged 事件吗?

【问题讨论】:

  • 您可以使用事件来检测变化并在之后手动设置实际值。
  • 到目前为止你尝试过什么?请显示您当前使用numericUpDown 方法的一些代码。
  • 用户应该设置商品的价格,因此代码中没有太多内容。由于 8 和 10 相差小于两个值,因此无法通过挂钩 ValueChanged 事件来工作。
  • 当然,我可以使用 RadioButton 和 numericUpDown 来解决少数情况,即必须输入偏离标准的价格,但我想知道是否只能使用 numericUpDown。单击箭头可在 5、8、10、15、20、25 之间切换。然后使用键盘输入选择值。我将编辑我的问题。

标签: c# numericupdown


【解决方案1】:

由于控件本身不支持这一点,您将不得不手动处理它。将方法附加到 ValueChanged 事件并检查值是否是其中之一。如果不是,则适当调整。

如果允许的值至少相隔两个值,您可以轻松检查它是上升还是下降,无需存储之前的值来确定。

【讨论】:

    【解决方案2】:

    由于这些值不是连续的,因此 NumericUpDown 可能不是此用例的正确控件。

    如果您只有六个左右的值,那么组合框会是更好的选择。

    如果你有更多,那么带有验证的文本框可能会更好。

    如果您确实设置了 NumericUpDown 控件,那么正如其他人指出的那样,您需要挂钩 ValueChanged 事件并在那里进行验证。

    【讨论】:

      【解决方案3】:

      试试:

      Public Class Form1
      ' NumericUpDown1
      '.Minimum = 1
      '.Maximum = 64
      '.Increment = 1
      '.value = 4
      Dim NumList() As Integer = {1, 2, 4, 8, 16, 32, 64} ' list of admitted values
      Dim NumExValue As Integer = 4 ' put the default value on numericUpDown
      Dim NumDoChange As Boolean = True ' used for not looping in changeValue event
      
      Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
      
          ' if the event is calling from this event (when set .value) do nothing
          If (NumDoChange = False) Then Exit Sub
      
          With NumericUpDown1
              Dim cv As Integer = .Value
      
              ' if no change (possible?) do nothing
              If (cv = NumExValue) Then Exit Sub
      
              ' if the value IS on array do nothing
              Dim ix As Integer = Array.IndexOf(NumList, cv)
              If (ix >= 0) Then
                  NumExValue = cv
                  Exit Sub
              End If
      
              ' if precedent value is 8 and up arrow pressed
              ' the current value is 9 so i search the index in array of
              ' value -1 and i take next element
              If (cv > NumExValue) Then ' up arrow
                  ix = Array.IndexOf(NumList, cv - 1) + 1 ' get next element
                  NumDoChange = False ' stop ValueChanged event 
                  .Value = NumList(ix) ' here start a call to this event
                  NumDoChange = True ' reset ValueChange event on
              End If
      
              ' the same but precedent element
              If (cv < NumExValue) Then ' down arrow pressed
                  ix = Array.IndexOf(NumList, cv + 1) - 1
                  NumDoChange = False
                  .Value = NumList(ix)
                  NumDoChange = True
              End If
              NumExValue = .Value
          End With
      End Sub
      End Class
      

      【讨论】:

        猜你喜欢
        • 2010-11-28
        • 2011-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多