Fill it at beginning, after InitializeComponent for example, not inside comboBox1_SelectedIndexChanged
ComboBox Code Not working
Why is my combo box not displaying any items? Seems like this should be pretty straight forward. I want to display my own list of items.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExportImages.Document;
namespace ExportImages
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class MyColor
{
private string shortName;
private string fullName;
public MyColor(string strFullName, string strShortName)
{
this.shortName = strShortName;
this.fullName = strFullName;
}
public string ShortName
{
get { return shortName; }
}
public string FullName
{
get { return fullName; }
}
}
private object DeserializeObject<T>(DocumentType[] items)
{
throw new NotImplementedException();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ArrayList MyColors = new ArrayList();
MyColors.Add(new MyColor("Blue", "B"));
MyColors.Add(new MyColor("Red", "R"));
MyColors.Add(new MyColor("Green", "G"));
MyColors.Add(new MyColor("Cyan", "C"));
MyColors.Add(new MyColor("Black", "K"));
comboBox1.DisplayMember = "FullName";
comboBox1.ValueMember = "ShortName";
// comboBox1.DataSource = MyColors; // would normally add the items like this
foreach (MyColor c in MyColors)
{
comboBox1.Items.Add(c);
}
comboBox1.SelectedIndex = 0;
}
}
}