0

I've created a program with a Panel, with a button I can add a UserControl in to the Panel. The UserControl contains only a ComboList

What I would like to do is to get all the UserControl in my Panel and get their value back. I tried this but my program does not detect any ComboBoxes, so the table is empty:

private void button_add_outil_Click(object sender, EventArgs e)
        {
            // Récupère tous les élèves présents
            List<string> eleve = new List<string>();

            foreach (Control ctrl in panel_eleve.Controls)
            {

                if (ctrl is ComboBox)
                {
                    ComboBox c = ctrl as ComboBox;
                    eleve.Add(c.SelectedText);
                }
            }

            addOutil add_outil_window = new addOutil(eleve);
            add_outil_window.ShowDialog();
        }

Does anyone know how to convert a UserControl into a Control at the same time? Thank you.

d219
  • 2,707
  • 5
  • 31
  • 36
  • 2
    A `UserControl` *is* a `Control`. [Look at the inheritance hierarchy](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.usercontrol?view=netframework-4.8). Is your question actually about recursively finding all controls nested in a parent control? – Matthew Watson Nov 20 '19 at 14:59
  • I think you'll find the ComboBoxes within the ctrl classes . You need to type check against your UserControl type first – d219 Nov 20 '19 at 15:02
  • I want to have access to the control that is in my UserControl I tried to put it in Public, which worked because now I have access to it thanks to `userControl.ComboBox_eleve`but I get an error:the object reference is not defined to an instance of an object :( – user12404703 Nov 20 '19 at 15:05
  • What does the code look like where you get an error? – d219 Nov 20 '19 at 15:12
  • This is not a console error but an error with a window displayed – user12404703 Nov 20 '19 at 15:15
  • This addresses your problem: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Nov 20 '19 at 15:18

2 Answers2

0

This is pseudo code (and I'm assuming your UserControl is a container) but, as mentioned in my comment, I think you'll need to do something like the following:

        // Récupère tous les élèves présents
        List<string> eleve = new List<string>();

        foreach (Control ctrl in panel_eleve.Controls)
        {

            if (ctrl is UserControl) // You may be able to be more specific with this type
            {

                   foreach (Control innerControl in ctrl.Controls )
                   {
                       if (innerControl is ComboBox)
                       {
                            ComboBox c = innerControl as ComboBox;
                            eleve.Add(c.SelectedText);
                       }

                   }

            }
        }

        addOutil add_outil_window = new addOutil(eleve);
        add_outil_window.ShowDialog();
d219
  • 2,707
  • 5
  • 31
  • 36
  • Thank you for your answer I have already changed `foreach (Control innerControl in ctrl)` on `foreach (Control innerControl in ctrl.Controls)` But I get an error at `eleve.Add(c.SelectedText);` The error is: `the object reference is not defined to an instance of an object` – user12404703 Nov 20 '19 at 15:13
  • For clarity could you show what your code looks like now? – d219 Nov 20 '19 at 15:20
0

My problem is solved Here is the final code with some modifications

            foreach (Control ctrl in panel_eleve.Controls)
        {

            if (ctrl is UserControl_reponsable)
            {
                foreach (Control innerControl in ctrl.Controls)
                {                     
                    ComboBox c = innerControl as ComboBox;
                    eleve.Add(c.Text);
                }

            }
        }

Thank you very much!!