[英]Get selected Object from Combobox
我有這樣的Combobox
充滿了對象,並在從選擇某個對象combobox
,我想顯示文本Textbox
,但出於某種原因,我無法打通我的選擇。
這是我的combobox
:
private void showBirds()
{
cboBirds.Items.Clear();
foreach (Bird b in Bird.ReadBirdCSV(txtFile.Text))
{
cboBirds.Items.Add(b);
}
}
它基本上顯示了“對象鳥”中鳥的名稱。
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.
}
我敢肯定這真的很簡單,我似乎無法弄清楚。
使用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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.