-
January 20th, 2010, 07:09 PM
#1
Display highlighted data from Listbox in Textboxes
I have an address book program completely done, except for one thing...
When a name is highlighted in the listbox, I need all of their information displayed in the related textboxes. All the contact information is stored in an array. I was playing with the code, and I know how to display just the information in the listbox, which is the first and last name, but I need ALL of the contact info displayed in the textboxes. I have included my code, and a screenshot of what should happen when a name is selected from the listbox.
Code:
public partial class frmAddressBook : Form
{
// Create Array to hold contact information
string[] Contacts = new string[20];
int i = 0;
public frmAddressBook()
{
InitializeComponent();
}
private void AddressBook_Load(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
// Close program
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Declare variables
string lastName = tbxLastName.Text;
string firstName = tbxFirstName.Text;
string street = tbxStreet.Text;
string city = tbxCity.Text;
string state = tbxState.Text;
string zip = tbxZip.Text;
// Add contact information for New contact to array
if (i < 20)
{
Contacts[i] = lastName + ", " + firstName;
i++;
}
else
{
MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
}
//Clear TextBoxes
tbxFirstName.Text = "";
tbxLastName.Text = "";
tbxStreet.Text = "";
tbxCity.Text = "";
tbxState.Text = "";
tbxZip.Text = "";
// Display list of contact names from array in the listbox
lstContacts.Items.Clear();
for (int j = 0; j < 20; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
// Delete selected contact
for (int k = 0; k < Contacts.Length; k++)
{
try
{
if (Contacts[k].Contains(lstContacts.SelectedItem.ToString()))
{
Contacts[k] = null;
}
}
catch
{
}
}
//Display Refreshed Records in ListBox
lstContacts.Items.Clear();
for (int j = 0; j < 10; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void lstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
tbxFirstName.Text = lstContacts.SelectedItem.ToString();
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|