Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 570
user2740565 Avatar asked Oct 22 '25 05:10

user2740565


1 Answers

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);
}
like image 79
huMpty duMpty Avatar answered Oct 23 '25 19:10

huMpty duMpty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!