简体   繁体   中英

C# can't update ComboBox when Item List is filled

I have a function which clears all Comboboxes on the form and then try to update the text. The new text is only shown if no Items are in the Item list. Does anyone have an idea whats wrong?

I deleted all functions from the project so that only the necessary part is available.

https://www.dropbox.com/s/ibst7enrteyk9jb/Digitales_Auftragsformular.zip?dl=0

The project is attached. Simply start, go to tab "Werkzeuganfrage" there are two Comboboxes in red. One without Items = working, one with Items which is not working.

 public Oberflaeche()
    {
        InitializeComponent();           


        List<ComboBox> myComboBoxes = GetControlsByType<ComboBox>(this, typeof(ComboBox));

        foreach (ComboBox txt in myComboBoxes)
        {
            //txt.Text = "";
            txt.SelectedIndex = -1;

        }
        Werkzeuganfrage_Combobox_rhino1_1.Text = "Rhino1_1";
        Werkzeuganfrage_Combobox_rhino1_2.Text = "Rhino1_2";

        comboBox1.Text = "Rhino1_1";
        comboBox2.Text = "Rhino1_2";
    }



    public List<T> GetControlsByType<T>(Control container, Type type)
    {


        List<T> result = new List<T>();
        foreach (Control c in container.Controls)
        {
            if (c.GetType() == type)
            {
                result.Add((T)Convert.ChangeType(c, typeof(T)));
            }

            if (c.HasChildren)
            {
                result.AddRange(GetControlsByType<T>(c, type));
            }
        }
        return result;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM