简体   繁体   中英

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.

This is what is in my 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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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