RockGuitarist1 RockGuitarist1 - 6 months ago 11x Question

Set Minimum Number in ComboBox

I am using this line to generate a collection of numbers for my ComboBox.

Main.ComboBox.Items.AddRange(Enumerable.Range(1, maxNum).Select(Function(s) s.ToString()).ToArray())

I was wondering how I could set the minimum for it? For example, 6-100, instead of its default 1-100.


I am using numberSetting with a Value of 0 in Project Properties Settings tab.

numberSetting is getting its actual value from:

My.Settings.numberSetting = CInt(ComboBox.Text)

Once numberSetting has its value, I use it with maxNum to set the maximum number of options in the ComboBox:

Dim maxNum = My.Settings.numberSetting
Dim winners = Enumerable.Range(1, maxNum).OrderBy(Function(r) rand.Next()).Take(5).ToArray()

This allows me to get 100 checkboxes if I select 100 as my option from the ComboBox. What I am looking to do is the set the minimum option to 6, so that 1, 2, 3, 4, and 5 are not options in the ComboBox.


Private Sub Options_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim maxNum = My.Settings.numberSetting2
NumberCombo.Items.AddRange(Enumerable.Range(6, maxNum - 5).Select(Function(s) s.ToString()).ToArray())
End Sub

Where My.Settings.numberSetting2 has a default value of 100.

Ian Ian

Yes, you can. However, notice that the arguments are start and count (not max). Thus, you should adjust your ComboBox initialization like this:

Dim minNum As Integer = 6
Dim maxNum As Integer = 100
Main.ComboBox.Items.AddRange(Enumerable.Range(minNum, maxNum - minNum + 1).Select(Function(s) s.ToString()).ToArray())

Note that you should change Range(1, maxNum) to Range(minNum, maxNum - minNum + 1)


This is how the result of the program will be

Before: there is no item in the ComboBox

enter image description here

The Code: shows the use of Enumerable.Range

enter image description here

After: shows the result in the ComboBox from the beginning after the Button Click

enter image description here

If you use it right and you have made sure to clear the previous ComboBox items, the Enumerable.Range(minNum, maxNum - minNum + 1) should work fine.

Edit 2:

If you still have some values after all those, then probably you have a hardcoded value in your ComboBox. In that case, maybe you want to check your designer -> Click on the ComboBox -> check its Collection property. There might be some hardcoded values there. Alternative, try to implement ComboBox.Items.Clear() before adding the items to make sure that everything is cleared up.