locked
numericUpDown RRS feed

  • Question

  • is there a way to set a numericUpDown to only dispay the numers 2 , 10, and 310 ?
    Sunday, November 21, 2010 11:06 PM

Answers

  • i think No, unless you subclass it :

    class CustomNumUpDown : NumericUpDown
     {
     public override void UpButton()
     {
      if (Value == Maximum)
      Value = 10;
      else if (Value == 10)
      Value = Minimum;
     }
     public override void DownButton()
     {
      if (Value == Minimum)
      Value = 10;
      else if (Value == 10)
      Value = Maximum;
     }
     }
    

    you can write more resuable class by adding a constructor accepts an array as parameter:

    class CustomNumUpDown : NumericUpDown
      {
        List<decimal> numbers;
        public CustomNumUpDown(List<decimal> numbers)
          : base()
        {
          this.numbers = numbers;
          Minimum = numbers[0];
          Maximum = numbers[numbers.Count-1];
        }
        public override void UpButton()
        {
          int index = numbers.IndexOf(Value);
          if (index != numbers.Count -1)
            Value = numbers[index + 1];
        }
        public override void DownButton()
        {
          int index = numbers.IndexOf(Value);
          if (index != 0)
            Value = numbers[index - 1];
        }
      }
    at the end you dont need of that =D, i did it for fun, you have more than one control (ComboBox, ListBox ..) are usefull for your case.

     


    Alan-SY
    • Marked as answer by Alan_chen Tuesday, November 30, 2010 2:29 AM
    Monday, November 22, 2010 12:12 AM

All replies

  • of course you could handle this in the Validating-Event, but IMHO it's better here to use a combobox with DropDownStyle set to DropDownList and as ItemsCollection the 3 numbers the users could choose from.

    Regards,

      Thorsten

    Sunday, November 21, 2010 11:57 PM
  • i think No, unless you subclass it :

    class CustomNumUpDown : NumericUpDown
     {
     public override void UpButton()
     {
      if (Value == Maximum)
      Value = 10;
      else if (Value == 10)
      Value = Minimum;
     }
     public override void DownButton()
     {
      if (Value == Minimum)
      Value = 10;
      else if (Value == 10)
      Value = Maximum;
     }
     }
    

    you can write more resuable class by adding a constructor accepts an array as parameter:

    class CustomNumUpDown : NumericUpDown
      {
        List<decimal> numbers;
        public CustomNumUpDown(List<decimal> numbers)
          : base()
        {
          this.numbers = numbers;
          Minimum = numbers[0];
          Maximum = numbers[numbers.Count-1];
        }
        public override void UpButton()
        {
          int index = numbers.IndexOf(Value);
          if (index != numbers.Count -1)
            Value = numbers[index + 1];
        }
        public override void DownButton()
        {
          int index = numbers.IndexOf(Value);
          if (index != 0)
            Value = numbers[index - 1];
        }
      }
    at the end you dont need of that =D, i did it for fun, you have more than one control (ComboBox, ListBox ..) are usefull for your case.

     


    Alan-SY
    • Marked as answer by Alan_chen Tuesday, November 30, 2010 2:29 AM
    Monday, November 22, 2010 12:12 AM