Forcing a NumericUpDown control to only accept powers of two

前端 未结 4 803
天涯浪人
天涯浪人 2021-01-16 03:51

I present the user a NumericUpDown control so he can specify the size of a texture. It is important that this texture is of power of two size (32, 64, 128...).

I hav

相关标签:
4条回答
  • 2021-01-16 04:21

    Hmm, well you could make your own. Using a textbox and a couple picture boxes you can set up a form that sets only powers of two.

    0 讨论(0)
  • 2021-01-16 04:31

    My suggestion is to use a ComboBox, you will have your numbers dropped down, you support auto complete and at the same time escape the validation of user input, code like:

    //generate your array.
    List<string> twos = new List<string>();
    
    int item = 2;
    int max = int.MaxValue / 2;
    while ((item = 2 * item) < max)
    {
        twos.Add(item.ToString());
    }
    
    ComboBox comboBox1 = new ComboBox();
    
    comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
    comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
    comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    comboBox1.FormattingEnabled = true;
    
    //comboBox1.Items.Clear();
    comboBox1.Items.AddRange(twos.ToArray());
    comboBox1.AutoCompleteCustomSource.AddRange(twos.ToArray());
    
    this.Controls.Add(comboBox1);
    
    0 讨论(0)
  • 2021-01-16 04:33

    You should inherit the UpDownBase class.

    0 讨论(0)
  • 2021-01-16 04:45

    You're going to run into a problem in 2+ digit numbers. If I want to put 32 and you see the 3 as I am typing, then your program would just tell me I'm wrong. Validate it when you submit your data.

    0 讨论(0)
提交回复
热议问题