2011-09-15 36 views
0

我試圖創建一個列表框與類別和另一個列表框中每個類別的項目。我希望能夠在第一個列表框中選擇一個類別,然後第二個列表框將變爲顯示該特定類別的項目。它很常見,我相信你能理解我的意思。我一直在尋找它,但不知道如何做到這一點。我創建了2個列表框,我想要的值,就是這樣。任何幫助?鏈接多個列表框c#

回答

1

我創建一個winform與兩個列表框ListBox1中和listbox2這就是我的Form1.cs的樣子

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; } 
    } 
} 

,這裏是在InitializeComponent()代碼

  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; 

enter image description here

0
  1. 有一個函數根據第一個的內容填充第二個列表框。
  2. 添加事件用於第一列表框改變時,並調用在#1
1

工作實施例(簡化的)中描述的funcion:

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); 
    } 
} 
+0

感謝例子。我還需要通過代碼單獨訪問listbox2項目並設置它們的內容。通過你的方法,它不能在listbox2中創建項目,因爲它們只是在運行中創建而不是在代碼中設置。對不起,我的英語不好,對我來說很難解釋,但這對你有意義嗎? – Yosi199

+0

@ user933977我更新了示例。很多不同的方式來做到這一點,其中包括DataBindings,LINQ等。你沒有提供任何代碼來顯示你正在處理的數據,所以我試着做一個簡單的例子。 – LarsTech