我在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 :(得分:0)
您可以根据DataSource
事件处理程序中ListBox2
的选择设置ListBox1's
SelectedIndexChanged
,如下所示:
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 :(得分:0)
你可以在Combobox上执行此操作:
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;
}
答案 2 :(得分:-1)
您可以使用以下代码
将字符串项添加到组合框combobox.Items.Add(stringItem);