开发者

linking multiple listboxes c#

I'm trying to create a listbox with categories and another listbox for items in each category. I want to be able to select a category in the first listbox and then the second listbox will change to display the items for that particular category. Its very common and I'm sure you can understand what I mean here. I wa开发者_运维知识库s looking around for it but couldn't get any idea how to do this. I've created 2 listboxes for the moment and the values I want in it, thats it. any help?


I created a winform with two list boxes listbox1 and listbox2 and this is what my Form1.cs looks like

namespace WinFormsApp
{
    public partial class Form1 : Form
    {
        private List<Category> categories;

        public Form1()
        {
            InitializeComponent();

            categories = new List<Category>();

            var categoryOne = new Category { Name = "Category 1"} ;
            categoryOne.Items.Add( new CategoryItem { Name = "Item 1"} );

            var categoryTwo = new Category { Name = "Category 2" };
            categoryTwo.Items.Add( new CategoryItem { Name = "Item 2" } );

            categories.Add( categoryOne );
            categories.Add( categoryTwo );
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            categoryBindingSource.DataSource = categories;
        }
    }

    public class Category
    {
        public string Name { get; set; }

        public List<CategoryItem> Items { get; private set; }

        public Category()
        {
            Items = new List<CategoryItem>();
        }
    }

    public class CategoryItem
    {
        public string Name { get; set; }
    }
}

and here is the InitializeComponent() code

            this.listBox1.DataSource = this.categoryBindingSource;
            this.listBox1.DisplayMember = "Name";
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(24, 24);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(242, 238);
            this.listBox1.TabIndex = 0;
            this.listBox1.ValueMember = "Items";

            this.categoryBindingSource.DataSource = typeof(Category);

            this.listBox2.DataSource = this.itemsBindingSource;
            this.listBox2.FormattingEnabled = true;
            this.listBox2.Location = new System.Drawing.Point(286, 24);
            this.listBox2.Name = "listBox2";
            this.listBox2.Size = new System.Drawing.Size(276, 238);
            this.listBox2.TabIndex = 1;
            this.listBox2.ValueMember = "Name";

            this.itemsBindingSource.DataMember = "Items";
            this.itemsBindingSource.DataSource = this.categoryBindingSource;

linking multiple listboxes c#


Working example (simplified):

private class CategoryItems
{
  public string Category { get; set; }
  public string Item { get; set; }

  public CategoryItems(string category, string item)
  {
    this.Category = category;
    this.Item = item;
  }

  public override string ToString()
  {
    return this.Item;
  }
}

private List<string> categories = new List<string>();
private List<CategoryItems> catItems = new List<CategoryItems>();

private void Form1_Load(object sender, EventArgs e)
{
  categories.Add("Cat 1");
  categories.Add("Cat 2");

  catItems.Add(new CategoryItems("Cat 1", "Cat 1 Item 1"));
  catItems.Add(new CategoryItems("Cat 1", "Cat 1 Item 2"));
  catItems.Add(new CategoryItems("Cat 2", "Cat 2 Item 1"));
  catItems.Add(new CategoryItems("Cat 2", "Cat 2 Item 2"));

  foreach (string cat in categories)
  {
    listBox1.Items.Add(cat);
  }
  listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  listBox2.Items.Clear();

  foreach (CategoryItems ci in catItems)
  {
    if (ci.Category == listBox1.SelectedItem.ToString())
      listBox2.Items.Add(ci);
  }
}


  1. Have a function that fills the second listbox based on the contents of the first.
  2. Add an event for when the first listbox changes, and call the funcion described in #1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜