n0bod1
n0bod1

Reputation: 365

Export checked Form1.checkBoxes to Form2.listBox

I have a form design that has 5 check boxes. I'll call them Bus1, Bus2...Bus5.

I have "Edit" button that brings up Form2 that has listBox.

When I press "Edit" button, I want to take only checked boxes and put them in listBox.

I was thinking of checking individual check boxes, and if true, add them to the list.

private void button1_Click(object sender, EventArgs e)
{
    if(checkBox1.Checked==true)
    {
        form2.listBox1.Items.Add("checkBox1.Text");
    }
    if(checkBox2.Checked==true)
    {
        ...
    }
    ...
}

Is this a correct way or is there a better one?

Thanks

Edit--------------------------------------

private void button1_click(object sender, EventArgs e)
{
   Form2 form2 = new Form2();
   form2.Show();
   foreach(var checkBox in this.Controls.OfType<CheckBox>().Where(c=>c.Checked))
   {
      form2.listBox1.Items.Add(checkBox.Text);
   }
}

Upvotes: 1

Views: 149

Answers (2)

John Alexiou
John Alexiou

Reputation: 29244

You will need to expose a public method in Form2 that populates the list box

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public void PopulateListBox(params CheckBox[] checkboxes)
    {
        foreach(var item in checkboxes.Where((cb) => cb.Checked))
        {
            listBox1.Items.Add(item.Text);
        }
    }
}

Then you call this method from Form1 with the checkboxes you want as parameters

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2=new Form2();
        form2.Show();

        form2.PopulateListBox(
            checkBox1,
            checkBox2,
            checkBox3,
            checkBox4,
            checkBox5);
    }

}

This way you choose which boxes to include in the check. If all are needed then the above is similarly coded as:

    public void PopulateListBox(IEnumerable<CheckBox> checkboxes)
    {
        foreach(var item in checkboxes.Where((cb) => cb.Checked))
        {
            listBox1.Items.Add(item.Text);
        }
    }

and

        form2.PopulateListBox( Controls.OfType<CheckBox>() );

Thanks @Andrey Tretyak

Upvotes: 2

Andrey Tretyak
Andrey Tretyak

Reputation: 3221

This should work if you need all check boxes on the form:

foreach (var checkBox in this.Controls.OfType<CheckBox>().Where(c => c.Checked))
{
   form2.listBox1.Items.Add(checkBox.Text);
}

if you need also info about check box variable name like string "checkBox1.Text" you can consider creating Tuple<string,CheckBox>[] or Dictionary<string,CheckBox> and use nameof for filling it.

Edit: there also a version without foreach loop, but it will result in creation of redundant array:

   form.listBox1.Items.AddRange(this.Controls.OfType<CheckBox>()
                                    .Where(c => c.Checked)
                                    .Select(c => c.Text)
                                    .ToArray<object>()); 

Upvotes: 2

Related Questions