tags:

views:

949

answers:

7

C# Which Event should I use to display data in a textbox when I select an item in a listbox?

I want to select an item in a list box (winforms) and then a textbox near by show some data related to that item but I don't know which event to use. I'll need to be able to click down the list and watch the textbox text update with each click.

Thanks

A: 

I think this will help you out.

Jason Punyon
A: 

Sorry, don't know the exact name of the event off the top of my head, but is something like SelectedItemChanged that you are looking for.

Rob Prouse
+4  A: 

SelectedIndexChanged

gkrogers
+2  A: 

You will want to handle either SelectedIndexChanged or SelectedValueChanged.

(Note that the SelectedValueChanged MSDN article has an example that sounds like exactly what you are doing.)

Andrew Hare
A: 

Is the SelectedIndexChanged event not working for you?

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    relatedTextbox.Text = listBox1.SelectedItem.ToString();
}
Bob
A: 

SelectedIndexChanged was what I was looking for, thanks to all.

+1  A: 

Assuming you have a form with a TextBox and a ListBox.

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        textBox1.Text = listBox1.SelectedItem.ToString();
    }
}
Gerrie Schenck