0

I am trying to populate a Combobox in Form2 using checkeditems in Form1, however I keep getting System.Windows.Forms.CheckedListBox + CheckedItemCollection populated in my Combobox, please can you advise, Cheers.

Form1:

{
    public object checkbox
    {
        get { return this.checkedListBox2.CheckedItems; }
    }

}

In Form2

{ 
     private Form1 frm1;

     private void Form2_Load(object sender, EventArgs e)
     {
          frm1 = new Form1();
          try
          {
              foreach (object item in frm1.checkbox.ToString())
              {
                 comboBox1.Items.Add(item);
              }
          }
          catch (System.Exception excep)
          {
              MessageBox.Show(excep.Message);    
          }
     }
}
boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
zizobiko25
  • 33
  • 7
  • 1
    First of all, change `foreach (object item in frm1.checkbox.ToString())` to `foreach (object item in frm1.checkbox` and tell the result. Work accordingly if any error is found – boop_the_snoot Oct 03 '17 at 16:39
  • [`DataRow`, `string`, `bool`... does not matter what type you try to pass around... the concept stays the same. Have a form constructor ready to accept the parameter you want to pass around, pass said parameter to said constructor parameter, and voila.](https://stackoverflow.com/questions/46306622/c-sharp-how-to-get-all-values-of-all-rows-in-a-datagridview-and-pass-it-into-ano/46308860) – blaze_125 Oct 03 '17 at 16:55
  • Possible duplicate of [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – Fabio Oct 03 '17 at 17:02

1 Answers1

1

You need to change your foreach loop :

try
{
    foreach (object item in frm1.checkbox)
    {
        comboBox1.Items.Add(item);
    }
}
catch (System.Exception excep)
{
    MessageBox.Show(excep.Message);
}

You also have to change your return statement because foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

public CheckedListBox.CheckedItemCollection checkbox
{
    get { return this.checkedListBox1.CheckedItems; }
}

Also you need to pass your Form1 to the other Form to get the information. Wherever you're creating your second form pass the current form

Form2 form2 = new Form2(this);
form2.Show();

Then your Form2 would look like this

private Form1 frm1;

public Form2(Form1 frm1)
{
    InitializeComponent();
    this.frm1 = frm1;
    try
    {
        foreach (object item in frm1.checkbox)
        {
            comboBox1.Items.Add(item);
        }
    }
    catch (System.Exception excep)
    {
        MessageBox.Show(excep.Message);
    }
}

You could also just pass the CheckedItems list at the place of the whole form.

boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
I.B
  • 2,925
  • 1
  • 9
  • 22
  • @AdrianoRepetti You're right haven't even realized how he was getting his reference to Form1 – I.B Oct 03 '17 at 16:54
  • Thanks @CNuts for the detailed answer, I followed exactly what you advised and still getting empty ComboBox in Form2! – zizobiko25 Oct 03 '17 at 17:25
  • @zizobiko25 Is any of the checkbox checked in your first form? – I.B Oct 03 '17 at 17:28
  • My apologies, I was using another CheckedListBox name in my actual code, but now it works, many thanks @CNuts – zizobiko25 Oct 03 '17 at 17:41