简体   繁体   中英

How can I iterate through selected ComboBoxes controls with a loop in C#?

I am working in a simple C# form. I have a total of 20 ComboBoxes . 10 ComboBoxes will contain similar data type and will have very similar name (CB1, CB2, CB3, ... CB10). Each ComboBox was loaded with a list of 5 elements A,B,C,D,E (by this I meant that I added those values to each of the 10 "CB" ComboBoxes). My intend is that the user can have the ability to select items (one out of A,B,C,D,E) from either 1 combobox, or 2 comboboxes, . . . ., or all of them (10 comboboxes).

I wish to store the data from the ComboBoxes where an item was selected in a list or array. For that I would like to iterate through the 10 ComboBoxes (the comboboxes which names are CB1, CB2, CB3, ..., CB10), check if the an item was selected in the combobox, and store the value selected in the ComboBox into a list (in the code below the list is called symbols). At the end the length of my symbols list (number of items) will depend on how many ComboBoxes the user selected from. Here is the code I am using:

List<string> symbols = new List<string>();

for (int i = 1; i <= 10; i++)
{
var my_comboBox = this.Controls["CB" + i.ToString()];
if (null != my_comboBox.SelectedItem) 
{ symbols.add(my_comboBox.Text); }
}

when I run the code I get the following error.

Object reference not set to an instance of an object.

Could anyone please explain what I am doing wrong? I got the code from another question that was posted and answered, below is the link to that question. Thanks in advance.

How can I iterate all ComboBoxes controls with a loop in C#?

I also tried the other alternative proposed on the answers to the questions cited. But it did not work. It does not go through the foreach loop (no error is shown though) . Here is the code for option 2.

 List<string> symbols = new List<string>();

var comboBoxes = this.Controls.OfType<ComboBox>().Where(x => x.Name.StartsWith("CB"));

foreach (var cmbBox in comboBoxes)
{
if (null != my_comboBox.SelectedItem) 
{ symbols.add(my_comboBox.Text); }
}

Again if anyone can please provide me with ideas to what I am doing wrong that would be very nice. Thanks in advance once more.

The ComboBoxes belong to the form as shown in the picture below. In there the ComboBoxes are called component_CB1, component_CB2, component_CB3, ..., component_CB10 (I changed the name of the ComboBoxes in the question to CB to make it easier to understand).

screenshot of my solution explorer

Thanks to everyone who contributed to find the answer to this problem. Please read the comments to identify the contributors.

The answer is that you can iterate through selected ComboBoxes in C#. However for this to work you need to make sure you know to what container control your ComboBoxes are added.

To know to container control your ComboBoxes are added to, go to View → Other Windows → Document Outline . You can see if those controls are directly children of the form or they are children of another container control.

If the ComboBoxes are added to your form directly, then here there are two alternatives to iterate through the ComboBoxes:

ALTERNATIVE 1: (ComboBoxes added to the form directly)

List<string> symbols = new List<string>();
for (int i = 1; i <= 10; i++)
{ //CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var my_comboBox = this.Controls["CB" + i.ToString()];
if (null != my_comboBox.SelectedItem) 
{ symbols.add(my_comboBox.Text); }
}

ALTERNATIVE 2: (ComboBoxes added to the form directly)

List<string> symbols = new List<string>();
//CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var comboBoxes = this.Controls.OfType<ComboBox>().Where(x=>x.Name.StartsWith("CB")); 

foreach (var cmbBox in comboBoxes)
{
if (null != my_comboBox.SelectedItem) 
{ symbols.add(my_comboBox.Text); }
}

If the ComboBoxes are added to a different control container (eg a tab in a TabControl as in my initial case), you must specify the control container name rather than "this" . Here are two alternatives taking as example the ComboBoxes Cb1, CB2, CB3, ..., CB10 that are added to a tab called tab1.

ALTERNATIVE 1: (ComboBoxes added to a tab of a TabControl)

List<string> symbols = new List<string>();
for (int i = 1; i <= 10; i++)
{//CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var my_comboBox = tab1.Controls["CB" + i.ToString()];
if (null != my_comboBox.SelectedItem) 
{ symbols.add(my_comboBox.Text); }
}

ALTERNATIVE 2: (ComboBoxes added to a tab of a TabControl)

List<string> symbols = new List<string>();
//CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var comboBoxes = tab1.Controls.OfType<ComboBox>().Where(x  
=>x.Name.StartsWith("CB"));

foreach (var cmbBox in comboBoxes)
{
if (null != my_comboBox.SelectedItem) 
{ symbols.add(my_comboBox.Text); }
}

Thanks for the help again. I hope this can be useful for others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM