vb.netwinformsnumericupdown

NumericUpDown that only allows a range of values of the power of two


I have a NumericUpDown whose minimum value is set to 1 and its maximum is 64. I have to increment it, from 1 to 64, using the values of the power of 2, so it must be 1, 2, 4, 8, 16, 32, 64.

I've tried several ways to change the NUD increment but with no satisfying results.
How could I solve this issue?


Solution

  • If you reduce the size of the text portion of the NumericUpDown control so it is not visible, you can put a read-only TextBox right next to it so it looks like it is part of it, then...

    Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
        Dim nud = DirectCast(sender, NumericUpDown)
        tbForNud1.Text = (2 ^ nud.Value).ToString()
    End Sub
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        tbForNud1.ReadOnly = True
        tbForNud1.Text = "1"
    End Sub
    

    You might want to change the colour of the border of the TextBox: Change the borderColor of the TextBox.