none
Loop comboxes RRS feed

  • Question

  • I am developing a desktop application, now in the variable controls i have 8 ComboBoxes items in results.

    My question is how do i loop over the ComboBoxes in controls so i can make something like:

    if (line.StartsWith("Classes"))
                        {
                            controls.combbox1.Items.Add(line);
                            CMBXWMICLASSES.Add(line);
                        }

    I want is to add to each ComboBox in controls the belonging lines.

    The problem is how to get each ComboBox and use it from controls.

    Many thanks
    Wednesday, December 21, 2016 4:02 AM

Answers

  • Hi,

    >>The problem is how to get each ComboBox and use it from controls.

    You could use the OfType extension method:

     foreach (ComboBox comboBox in controls.OfType<ComboBox>())
                {
                    //do something with each ComboBox
                }
    

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kellyaaaaa2016 Sunday, December 25, 2016 9:31 AM
    Thursday, December 22, 2016 3:07 AM

All replies

  •             foreach (Control con in this.Controls)
                {
                    if (con is ComboBox)
                    {
                        // Do Something
                    }
                }


    It all Happenz Sendil

    Wednesday, December 21, 2016 4:38 AM
  • Sorry, I haven't described clearly.

    I mean how do i loop over IEnumerable<Control> ?

    IEnumerable<Control> controls;

    This method is GetAll is

    public static IEnumerable<Control> GetAll(Control control, Type type)
            {
                var controls = control.Controls.Cast<Control>();
    
                return controls.SelectMany(ctrl => GetAll(ctrl, type))
                                          .Concat(controls)
                                          .Where(c => c.GetType() == type);
            }

    The problem is how to get each ComboBox and use it from controls.

    Wednesday, December 21, 2016 1:24 PM
  • Hi,

    >>The problem is how to get each ComboBox and use it from controls.

    You could use the OfType extension method:

     foreach (ComboBox comboBox in controls.OfType<ComboBox>())
                {
                    //do something with each ComboBox
                }
    

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kellyaaaaa2016 Sunday, December 25, 2016 9:31 AM
    Thursday, December 22, 2016 3:07 AM