2011-09-15 10 views
0

各カテゴリの項目のリストボックスとリストボックスを作成しようとしています。最初のリストボックスでカテゴリを選択できるようにしたい場合、2番目のリストボックスが変更され、その特定のカテゴリのアイテムが表示されます。その非常に一般的な私はあなたが私がここで何を意味するのか理解できると確信しています。私はそれを見回していましたが、これをどうやって行うのか考えられませんでした。私は2つのリストボックスを作成しました。どんな助け?複数のリストボックスをリンクするC#

答えて

1

を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番目のリストボックスを塗りつぶす関数があります。
  2. 最初のリストボックスの変更のためのイベントを追加し、#1例(簡体字)の作業
1

で説明funcion呼び出す:私は2つのリストボックスlistbox1を持つWinフォームを作成し、

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の項目にコードでアクセスし、内容を設定する必要があります。あなたの方法では、リストボックス2内の項目を処理することはできません。なぜなら、それらはただちに作成されていて、コードで設定されていないからです。 amm申し訳ありませんが、私の英語はうまくいかず、説明するのが難しいですが、それはあなたにとって理にかなっていますか? – Yosi199

+0

@ user933977例を更新しました。これを行うさまざまな方法には、DataBindings、LINQなどがあります。扱うデータを示すためのコードを提供していないので、簡単な例を作成しようとしました。 – LarsTech

関連する問題