2017-02-12 61 views
0

我有一个C#Winform上的组合框,我想从这个列表中填充字符串名称变量,没有别的。这是清单代码。C#winform组合框

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

谷歌“级联dropdownlists WinForm的”,你的业务逻辑在表示层,将其移动到业务逻辑层和使用绑定控件绑定的组合框或通过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

您可以根据选择从ListBox1'sSelectedIndexChanged事件处理程序如下设置的ListBox2DataSource

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

可以使用添加字符串项组合框下面的代码

combobox.Items.Add(stringItem); 
+0

难以置信的模糊答案。 – gplumb

+0

@gplumb这是一个向combobox添加数据的通用问题。你还会在这里建议什么? –