从组合框获取所选对象 [英] Get selected Object from Combobox
本文介绍了从组合框获取所选对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 Combobox
填充了对象
,然后从 combobox
中选择了某个对象想要在文本框
中显示文本,但是由于某些原因,我无法通过选择来显示文本。
I have this Combobox
filled with objects
And Upon selecting a certain object from the combobox
I would like to show Text in a Textbox
, but for some reason I can't get my selection through.
这就是我的组合框
中的内容:
private void showBirds()
{
cboBirds.Items.Clear();
foreach (Bird b in Bird.ReadBirdCSV(txtFile.Text))
{
cboBirds.Items.Add(b);
}
}
它基本上显示了Objects Bird中鸟类的名字
It basically shows the names of birds from the 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.
}
我敢肯定这很简单,我可以
I'm sure it's really simple, I just can't seem to figure it out.
推荐答案
使用 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);
}
这篇关于从组合框获取所选对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文