Mitch A
Mitch A

Reputation: 23

Visual C#: How do I attach a click event to an item in listbox?

enter image description here

See GUI Design above

I am creating a program where the user enters contacts by name, address, phone, which are stored in parallel arrays. The program then stores all of the entered contacts into a listbox. I then want the user to be able to click on any name and have the complete contact info for that person displayed in an adjacent textbox. My question is how to i create an event where everytime a new item in the listbox is clicked on, their info will be displayed.

Enter button which stores info into arrays and adds name to contact list:

private void button1_Click(object sender, EventArgs e)
    {
        first[mindex] = txtFirst.Text;
        last[mindex] = txtLast.Text;
        email[mindex] = txtEmail.Text;
        address[mindex] = txtAddress.Text;
        cell[mindex] = txtCell.Text;

        lstContacts.Items.Add(first[mindex] + " " + last[mindex]);

        mindex++;

        txtLast.Text = "";
        txtFirst.Text = "";
        txtEmail.Text = "";
        txtAddress.Text = "";
        txtCell.Text = "";
        txtLast.Focus();

    }

This is what i want to be executed each time a contact name is clicked on:

private void DisplayContact()
    {   
        int dispIndex;
        dispIndex = lstContacts.SelectedIndex;

        txtOutput.Text = "Name: " + "\t\t" + first[dispIndex] + last[dispIndex] + Environment.NewLine +
                         "Address: " + "\t\t" + address[dispIndex] + Environment.NewLine +
                         "Cell: " + "\t\t" + cell[dispIndex] + Environment.NewLine +
                         "Email: " + "\t\t" + email[dispIndex];
    }

Just don't know how to connect these things. Any help appreciated

Upvotes: 1

Views: 4996

Answers (3)

mindOfAi
mindOfAi

Reputation: 4602

You can use the SelectionChanged event for this. Add the SelectionChanged to your listbox

private void listBox_SelectionChanged(object sender, EventArgs e)
{
        int dispIndex;
        dispIndex = lstContacts.SelectedIndex;

        txtOutput.Text = "Name: " + "\t\t" + first[dispIndex] + last[dispIndex] + Environment.NewLine +
                         "Address: " + "\t\t" + address[dispIndex] + Environment.NewLine +
                         "Cell: " + "\t\t" + cell[dispIndex] + Environment.NewLine +
                         "Email: " + "\t\t" + email[dispIndex];
}

Hope it helps!

Upvotes: 1

Dan Orlovsky
Dan Orlovsky

Reputation: 1095

You want the ListBox to subscribe to the Click event - and from there you can call your DisplayContact method.

From your Design View, select the ListBox you would like to add this functionality to. In the properties window, click the lightning-bolt icon to open the events tab.

Events Tab

From here, scroll until you find the Click Action. Double Click the name (of the event, in this case: "Click") and Visual Studio will automatically subscribe this control to the click event and create a method.

Click Event

In the Form's .cs file, you'll find the generated method, which follows a format you're probably familiar with. But here is where you call your DisplayContact method:

    private void listBox1_Click(object sender, EventArgs e)
    {
        DisplayContact();
    }

You can do this for any event you can think of - but simply adding a method to the Form's code is not enough to make this a success. Visual Studio automatically generates the code that tells your program the ListBox is waiting for such an event, and that happens in the form's Designer file:

Designer code

^^ That's from the FormName.Designer.cs file, in the InitializeComponent method.

Hope this helps.

Upvotes: 1

Vijunav Vastivch
Vijunav Vastivch

Reputation: 4191

On SelectedIndexChanged Properties:

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     var i = listBox1.Items[listBox1.SelectedIndex].ToString();
     MessageBox.Show(i.ToString());

 }

Upvotes: 2

Related Questions