Home > Back-end >  Have a list of all CheckedListBoxes
Have a list of all CheckedListBoxes

Time:07-14

I'm trying to set up an outer-loop that iterates over all the checkboxes that I've created in a windows form app in Visual Studio 2008 C#.

currently to have a list of all boxes i have:

public List<CheckedListBox> boxes = new List<CheckedListBox>();

private void button1_Click(object sender, EventArgs e)
{
  boxes.Add(checkedListBox1);
  boxes.Add(checkListBox2);
  boxes.Add(checkedListBox3);
  // this process continues until i've reached checkedListBox7.
}

Is there a better/cleaner way of doing this?

CodePudding user response:

You can iterate over all controls in your form or panel and check if it is CheckedListBox and add it to your list. Of course you can add additional check for example if name starts with "checkedListBox" etc.

I don't know what is your goal but for me I expect to clear lists before repeated appending objects to list.

    public List<CheckedListBox> boxes = new List<CheckedListBox>();

    private void button1_Click(object sender, EventArgs e)
    {
        boxes.Clear();
        foreach (var control in this.Controls)
        {
            if(control is CheckedListBox)
                boxes.Add((CheckedListBox)control);
        }
    }

CodePudding user response:

I would recommend using the Type.GetFields method. (Here's a link to the official documentation from Microsoft for more information on it. https://docs.microsoft.com/en-us/dotnet/api/system.type.getfields?view=net-6.0)

In short, Type.GetFields will get all of the public fields in the defined type. They won't be returned in order, so that is the downside to this, but you can still only add fields based on what is returned. I'll try to use your code to show what this would look like

public List<CheckedListBox> boxes = new List<CheckedListBox>();

private void button1_Click(object sender, EventArgs e)
    {
        //Get the "type" of this class
        Type objType = typeof(this);
        
        //Get all fields in this class
        FieldInfo[] info = objType.GetFields();
        
        //This is just to make the next line shorter
        BindingFlags battr = BindingFlags.Public | BindingFlags.Instance;

        //This will need to get a field with the type you want to add
        FieldInfo desiredType = typeof(this).GetField(nameof(checkedListBox1), battr);
        
        //loop through each field
        foreach(var item in info)
        {
            //if the current field is the same as the field type you want
            if(item.FieldType == desiredType.FieldType)
            {
                boxes.add(item);
            }
        }
    }

This is honestly longer than just going through the seven of them, and comments have addressed other concerns that I have about your code, but assuming you have all of that figured out, and you genuinely need to loop through each of them, this is probably the best way I've found to do what you're asking about.

CodePudding user response:

I'm trying to set up an outerloop that iterates over all the checkboxes that ive created in a windows form app in Visual Studio 2008 C#.

Focusing on the outerloop and this process continues until i've reached checkedListBox7 parts, I'm guessing you're looking for something like:

public List<CheckedListBox> boxes = new List<CheckedListBox>();

private void button1_Click(object sender, EventArgs e)
{
    boxes.Clear();
    for(int i=1; i<=7; i  ) {
        CheckedListBox clb = this.Controls.Find("checkedListBox"   i.ToString(), true).FirstOrDefault() as CheckedListBox;
        if (clb != null) {
            boxes.Add(clb);
        }
    }
}

Note that this will find the controls "by name" no matter how deeply they are nested, even if they are all in different containers.

  • Related