4

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.

Chris
  • 8,527
  • 10
  • 34
  • 51
user2740565
  • 67
  • 1
  • 1
  • 3
  • I don't understand the `Console.WriteLine(...` part, if you have a TextBox you have to set `Text` properties of it. Maybe i missing something? – Alessandro D'Andria Sep 02 '13 at 17:17
  • You are completely right, I just put it there to point out that there are in fact objects in my cbo, not just strings – user2740565 Sep 02 '13 at 17:21
  • possible duplicate http://stackoverflow.com/questions/3237885/combobox-selectedindexchanged-event-how-to-get-the-previously-selected-index – konkked Mar 25 '16 at 13:41

1 Answers1

8

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);
}
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99