0
votes

In contrast to setting the minimum and maximum value for a NumericUpDown how do I get the user to select only between different Preset Values? (e.g. 5, 8, 10, 15, 20, 25).

EDIT: Can I differentiate between the ValueChanged Event by 1) clicking on the arrows of numericUpDown and by 2) changing the Value manually with the keybaord?

3
You could use events to detect change and set the actual value manually afterwards.Num Lock
What have you tried so far? And please show some code of where you currently use your numericUpDown method.Kevin Cruijssen
The user is supposed to set the Price of an item, hence there is not much going on in the code. Since 8 and 10 are less than two values apart it will not work by hooking into the ValueChanged event.AzulShiva
Sure I could use a RadioButton AND numericUpDown for the rare cases where one would have to enter a price that deviates from the standard but I wanted to know if it is possible only with the numericUpDown. Click the arrows to switch between 5, 8, 10, 15, 20, 25. And enter a Value of choice using the keyboard. I will edit my question.AzulShiva

3 Answers

1
votes

As the control itself does not support this, you will have to handle it manually. Attach a method to the ValueChanged event and check if the value is one of those. If not, then adjust appropriately.

If the allowed values are at least two values apart, you can easily check if it is going up or down and don't need to store the previous value to determine that.

0
votes

As these values are not consecutive a NumericUpDown is probably not the right control for this use case.

If you only have a half dozen or so values then a combo box would be a better choice.

If you have more then a text box with validation might be better.

If you are really set on a NumericUpDown control then, as others have pointed out, you'll need to hook into the ValueChanged event and validate there.

0
votes

Try:

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