简体   繁体   English

从组合框获取所选对象

[英]Get selected Object from 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. 我有这样的Combobox充满了对象,并在从选择某个对象combobox ,我想显示文本Textbox ,但出于某种原因,我无法打通我的选择。

This is what is in my combobox : 这是我的combobox

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

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. 我敢肯定这真的很简单,我似乎无法弄清楚。

Use ComboBox.SelectedIndexChanged event 使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM