// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections; using System.Drawing; using System.Windows.Forms; public class X { private string a; private string b; public X(string a, string b) { this.a = a; this.b = b; } public string A { get { return a; } } public string B { get { return b; } } } public class MainForm : Form { private ComboBox cbx; public MainForm() { try { InitializeComponent(); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void Main(string[] args) { Application.Run(new MainForm()); } private void InitializeComponent() { cbx = new ComboBox(); SuspendLayout(); cbx.Location = new Point(50, 50); cbx.Size = new Size(100, 25); cbx.Name = "Dynamic Combo Box"; cbx.SelectedIndexChanged += new EventHandler(ChooseHandler); ArrayList list = new ArrayList(); list.Add(new X("Et", "Dette er et")); list.Add(new X("To", "Dette er to")); list.Add(new X("Tre", "Dette er tre")); cbx.DataSource = list; cbx.DisplayMember = "B"; cbx.ValueMember = "A"; ClientSize = new Size(200, 200); Name = "Main Form"; Text = "Main Form"; Controls.Add(cbx); ResumeLayout(false); } private void ChooseHandler(object o, EventArgs e) { MessageBox.Show("Du valgte " + ((ComboBox)o).SelectedValue); } }