How to create a drop down menu in WinForms and C#

前端 未结 5 812
庸人自扰
庸人自扰 2020-12-16 15:40

I am new to using Visual Studio/WinForms/C#

I am trying to create a simple drop down menu where each item can have a value and a label.

This is what I would do

5条回答
  •  -上瘾入骨i
    2020-12-16 16:10

    It seems like the value is just a reference to what item is selected, correct? Then you can use the index of the combobox, makes it a lot easier.

    Not sure if your items are known before build, if yes, then just add them in the designer, properties of the combobox. If not, then you can add them dynamically by just doing:

            List items = new List() { "item1", "item2" };
            comboBox1.DataSource = items;
    

    And to know what item is selected:

            int index = comboBox1.SelectedIndex;
    

提交回复
热议问题