Hello,

I'm pretty new to C# and .NET. I have been working with the arrays within comboBoxes

I have created a method to add multiple comboBoxes, each with defined items to select from (1,2,3,4,5,6,7,8,9,10)

When I run the program, comboboxes get created automatically. When I select the value from a combobox I want to store that selected value as 'double' and then I want to reuse that stored value outside of the method within a new button. Here is my code for the method: Any input is appreciated

private int AddCreditHoursComboBox(int count, ComboBox[] creditHours, int z, int m)
        {
            
            
            for (int i = 0; i < count; i++)
            {

                creditHours[i] = new ComboBox();
                creditHours[i].Items.Add("1");
                creditHours[i].Items.Add("2");
                creditHours[i].Items.Add("3");
                creditHours[i].Items.Add("4");
                creditHours[i].Items.Add("5");
                creditHours[i].Items.Add("6");
                creditHours[i].Items.Add("7");
                creditHours[i].Items.Add("8");
                creditHours[i].Items.Add("9");
                creditHours[i].Items.Add("10");
                creditHours[i].Location = new System.Drawing.Point(z, m);
                creditHours[i].Size = new Size(80, 70);
                creditHours[i].TabIndex = 1;
                
                this.Controls.Add(creditHours[i]);
                
                this.ResumeLayout(false);
                m += 40;
                
            }
                return m;

You should store the "1,2,3...." in an array and then use AddRange() to add it to the comboBox:

[B]string[] numbers = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
[/B]
for (int i = 0; i < count; i++)
{

creditHours[i] = new ComboBox();
[B]creditHours[i].Items.AddRange(numbers);[/B]
.......

Add this double variable above "public Form1() {......}" so that its accessible from everywhere (inside the class):

double selectedItem;

Use this code for assigning the selected comboBox item to that double variable:

selectedItem = Convert.ToDouble(comboBox.SelectedItem.ToString());

Now you can use the variable selectedItem inside any method.

Thanks

Thanks!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.