Reputation: 23
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
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
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.
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.
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:
^^ That's from the FormName.Designer.cs file, in the InitializeComponent method.
Hope this helps.
Upvotes: 1
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