use*_*565 2 c# combobox object
我有这个Combobox充满了对象并且在从 中选择某个对象后,combobox我想在 a 中显示文本Textbox,但由于某种原因,我无法通过我的选择。
这是我的combobox:
private void showBirds()
{
cboBirds.Items.Clear();
foreach (Bird b in Bird.ReadBirdCSV(txtFile.Text))
{
cboBirds.Items.Add(b);
}
}
Run Code Online (Sandbox Code Playgroud)
它基本上显示了 Objects Bird 中鸟类的名称。
private void cboBirds_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//WHAT DO I WRITE HERE TO GET txbGender TO SHOW THE GENDER?
foreach (Bird b in cboBirds.Items)
{
Console.WriteLine(b.Gender +" - " + b.Name +" - " + b.Risk + " - " +b.Reference);
}
//^This shows all info on every bird.
}
Run Code Online (Sandbox Code Playgroud)
我敢肯定这真的很简单,我只是似乎无法弄清楚。
使用ComboBox.SelectedIndexChanged事件
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(ComboBox1.SelectedItem==null) return;
var b= (Bird) ComboBox1.SelectedItem;
if(b!=null)
Console.WriteLine(b.Gender +" - " + b.Name +" - " + b.Risk + " - " +b.Reference);
}
Run Code Online (Sandbox Code Playgroud)