1

Hi I neeed to do this one in a loop, but I don't have any idea how to do. I can't do this by just simply incrementing it.

CheckBox[] checkboxarray;

checkboxarray = new CheckBox[] { 
    txtChckBx0,  txtChckBx1,  txtChckBx2,  txtChckBx3,  txtChckBx4,  txtChckBx5, 
    txtChckBx6,  txtChckBx7,  txtChckBx8,  txtChckBx9,  txtChckBx10, txtChckBx11, 
    txtChckBx12, txtChckBx13, txtChckBx14, txtChckBx15, txtChckBx16, txtChckBx17, 
    txtChckBx18, txtChckBx19, txtChckBx20, txtChckBx21, txtChckBx22, txtChckBx23, 
    txtChckBx24, txtChckBx25, txtChckBx26, txtChckBx27, txtChckBx28, txtChckBx29, 
    txtChckBx30, txtChckBx31, txtChckBx32, txtChckBx33, txtChckBx34, txtChckBx35, 
    txtChckBx36, txtChckBx37, txtChckBx38, txtChckBx39, txtChckBx40, txtChckBx41, 
    txtChckBx42, txtChckBx43, txtChckBx44, txtChckBx45, txtChckBx46, txtChckBx47, 
    txtChckBx48, txtChckBx49, txtChckBx50, txtChckBx51, txtChckBx52, txtChckBx53,
    txtChckBx54, txtChckBx55, txtChckBx56, txtChckBx57, txtChckBx58, txtChckBx59, 
    txtChckBx60, txtChckBx61, txtChckBx62, txtChckBx63, txtChckBx64, txtChckBx65, 
    txtChckBx66, txtChckBx67, txtChckBx68, txtChckBx69, txtChckBx70, txtChckBx71, 
    txtChckBx72, txtChckBx73, txtChckBx74, txtChckBx75, txtChckBx76, txtChckBx77, 
    txtChckBx78, txtChckBx79, txtChckBx80
};

3 Answers 3

4

If you know that the checkboxes are all on a form:

var list = new List<CheckBox>();
foreach(var control in this.Controls)
{
    var checkBox = control as CheckBox;
    if(checkBox != null)
    {
        list.Add(checkBox);
    }
}

var checkBoxArray = list.ToArray();

If you don't know where the controls are then you will have to search for them.

BTW: The code above uses WinForms. If you are using WPF, Silverlight, Metro,... the container will be named differently.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't do new and then

checkboxarray = new CheckBox[] { txtChckBx0, ....}

it's two different ways to define an array. you need to do:

CheckBox[] checkboxarray = { txtChckBx0, ....}; 

If you want it to work.

Good luck.

Comments

0

In WinForm

List<CheckBox> checkBox = new List<CheckBox>();
// Adding checkboxes for testing...
for (int i = 0; i <= 80; i++)
{
    var cbox = new CheckBox();
    cbox.Name = "txtChckBx"+ i.ToString();
    checkBox.Add(cbox);
    Controls.Add(cbox);

}

List<CheckBox> checkBoxfound = new List<CheckBox>();
// loop though all the controls 
foreach (var item in Controls)
{
    // filter for checkboxes and name should start with "txtChckBx"
    if (item is CheckBox && ((CheckBox)item).Name.StartsWith("txtChckBx", StringComparison.OrdinalIgnoreCase))
    {
        checkBoxfound.Add((CheckBox)item);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.