2017-02-12 4 views
0

私はC#Winformのコンボボックスを持っていますが、このリストからの文字列名変数を入力したいのです。リストコードはここにあります。C#winform ComboBox

あなたは次のように SelectedIndexChangedイベントハンドラ内 ListBox1'sからの選択に基づいて ListBox2DataSourceを設定することができ
class Animals 
{ 
    public string averageMass { get; set; } 
    public string lifeSpan { get; set; } 
    public string whereToFind { get; set; } 
    public string name { get; set; } 
    public string animalImage { get; set; } 
} 
class Mammals:Animals 
{ 
    public static List<Mammals> MammalList = new List<Mammals>(); 
    public string hairColour { get; set; } 
} 
+0

グーグルを使用してコンボボックスに文字列項目を追加することができ、あなたは、あなたのプレゼンテーション層でのビジネスロジックを持ってビジネスロジック層に移動し、バインドコントロールを使用してコンボボックスをバインドまたはSelectedValue Changeイベントを使用してコントロールのデータソースを設定することによって実現します –

答えて

0

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedItem == "Mammals") //You can also do index e.g. comboBox1.SelectedIndex == 0 
     { 
      comboBox2.DataSource = mammalList; 
     } 
     else 
     { 
      comboBox2.DataSource = reptileList; 
     } 
    } 

それとも、できもこれを行う:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     comboBox2.DataSource = fncGetSpecies(comboBox1.SelectedIndex); 
    } 

    private string[] fncGetSpecies(int intIndex) 
    { 
     //This will return if selected item is 0 which is Mammals or 1 if the selected item is Reptiles. 
     return intIndex == 0 ? mammalList : reptileList; 
    } 
0

:あなたはコンボボックスにこれを行うことができ

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if(listBox1.SelectedIndex==0)//Which is Mammals list 
     { 
      listBox2.DataSource = reptileList; 
     } 
     else//Which is Reptiles list 
     { 
      listBox2.DataSource = mammalList; 
     } 
    } 
-1

あなたが「dropdownlists Winフォームをカスケード接続」次のコード

combobox.Items.Add(stringItem); 
+0

非常に曖昧な答えです。 – gplumb

+0

@gplumbこれはデータをコンボボックスに追加する一般的な質問です。ここで何をお勧めしますか? –