windows forms link listbox to array code example

Example 1: windows forms link listbox to array

List listToBind = new List { "AA", "BB", "CC" };
this.listBox1.DataSource = listToBind;

Example 2: windows forms link listbox to array

public class MyClass
{
    public int Id { get; set; }
    public string Text { get; set; }
    public MyClass(int id, string text)
    {
        this.Id = id;
        this.Text = text;
    }
}

Example 3: windows forms link listbox to array

List listToBind = new List { new MyClass(1, "One"), new MyClass(2, "Two") };
this.listBox1.DisplayMember = "Text";
this.listBox1.ValueMember = "Id"; // optional depending on your needs
this.listBox1.DataSource = listToBind;

Tags:

Misc Example