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;

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

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

Edited 6 Years Ago by farooqaaa: n/a

This question has already been answered. Start a new discussion instead.