简体   繁体   中英

How I can show I list<string> or alternative in ComboBox ? (c#)

Actually I have a work for my school in c# and I need to show a ComboBox list, but I don't like display of code:

combobox.Items.Add("example")
combobox.Items.Add("example2")
combobox.Items.Add("example3")

It's very long and I try to optimize with a List or string[] but I not have an idea how I can make.

Can you help me?

Simply set the ComboBox's ItemsSource property, to anything that implements the IEnumerable interface:

combobox.ItemsSource = new string[] { "example1", "example2", "example3" };

For details see the documentation: ItemsControl.ItemsSource Property .

I try this, but, I have an error at "AddRange"

List<string> informatique = new List<string>() { "Multimédia", "Salle Informatique" };
ListSalle.Items.AddRange(informatique.ToArray());

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