我試圖創建一個列表框與類別和另一個列表框中每個類別的項目。我希望能夠在第一個列表框中選擇一個類別,然後第二個列表框將變爲顯示該特定類別的項目。它很常見,我相信你能理解我的意思。我一直在尋找它,但不知道如何做到這一點。我創建了2個列表框,我想要的值,就是這樣。任何幫助?鏈接多個列表框c#
0
A
回答
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;
0
- 有一個函數根據第一個的內容填充第二個列表框。
- 添加事件用於第一列表框改變時,並調用在#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);
}
}
相關問題
- 1. C#多路鏈接列表
- 2. C中的多個鏈接列表
- 3. 雙重鏈接列表與C/C++中的多重鏈接列表
- 4. C - 鏈接列表
- 5. C++鏈接列表
- 6. C++鏈接列表
- 7. 帶多文件的C++鏈接列表
- 8. c/C++鏈接列表
- 9. 將表單鏈接到多個列表框
- 10. 在VB.NET中鏈接兩個或多個列表框的索引
- 11. 鏈接4列表框
- 12. Excel VBA列表框鏈接
- 13. C中的單個鏈接列表
- 14. 兩個鏈接列表的聯合 - C++
- 15. C# - 鏈接兩個列表有效
- 16. 在C#中鏈接兩個列表
- 17. 創建多個鏈接列表
- 18. 顯示多個值鏈接列表
- 19. 多個鏈接列表(可變輸入)
- 20. 在Java中的多個鏈接列表
- 21. R排序多個(鏈接)列表
- 22. C#多列列表框
- 23. 加倍鏈接列表C++
- 24. 鏈接列表頭C++
- 25. 指針鏈接列表C
- 26. C通用鏈接列表
- 27. 鏈接列表操作C++
- 28. 鏈接列表排序C
- 29. 鏈接列表問題C++
- 30. 通用C++鏈接列表
感謝例子。我還需要通過代碼單獨訪問listbox2項目並設置它們的內容。通過你的方法,它不能在listbox2中創建項目,因爲它們只是在運行中創建而不是在代碼中設置。對不起,我的英語不好,對我來說很難解釋,但這對你有意義嗎? – Yosi199
@ user933977我更新了示例。很多不同的方式來做到這一點,其中包括DataBindings,LINQ等。你沒有提供任何代碼來顯示你正在處理的數據,所以我試着做一個簡單的例子。 – LarsTech