How to add items to our combobox very easy in .Net (C#) WinForms
1) First we need to add a combobox to our WinForm, name comboBox1
2) Second in c# code create a class that will our custom combobox item like this:
3) And finally add some items to our myCombo:
Remember to puth the custom class after the main clase (public partial class Form1 : Form)
Result: this will show the first item value.
Thanks for reading.
maksim20032@hotmail.com
2) Second in c# code create a class that will our custom combobox item like this:
public class ComboBoxItem
{
public string Value;
public string Text;
public ComboBoxItem(string val, string text)
{
Value = val;
Text = text;
}
public override string ToString()
{
return Text;
}
}
3) And finally add some items to our myCombo:
private void Form1_Load(object sender, EventArgs e)
{
// Add item
comboBox1.Items.Add(new ComboBoxItem("item1 text", "item1 value"));
// Show item
MessageBox.Show(((ComboBoxItem)comboBox1.Items[0]).Value);
}
Remember to puth the custom class after the main clase (public partial class Form1 : Form)
Result: this will show the first item value.
Thanks for reading.
maksim20032@hotmail.com