2

我有这个充满了对象,并从我想在 a 中显示 Text 中Combobox选择某个对象后,但由于某种原因,我无法通过我的选择。comboboxTextbox

这就是我的combobox

 private void showBirds()
    {
        cboBirds.Items.Clear();
        foreach (Bird b in Bird.ReadBirdCSV(txtFile.Text))
        {
            cboBirds.Items.Add(b);
        }
    }

它基本上显示了来自 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.

    }

我敢肯定这真的很简单,我似乎无法弄清楚。

4

1 回答 1

6

使用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);
}
于 2013-09-02T17:09:12.747 回答