This works to search in the searchbox and display the found result in the ListBox:
private void txtSearch_TextChanged(object sender, System.EventArgs e)
{
// clear the results list
lstResults.Items.Clear();
// check to see if search box is empty
if (txtSearch.Text != "")
{
foreach (Contact contact in addressBk)
{
int len = txtSearch.Text.Length;
if (contact.LastName.Length >= len && string.Compare(contact.LastName.Substring(0, len), txtSearch.Text, true) == 0)
{
lstResults.Items.Add(contact.LastName);
}
}
}
}
But this part I'm working on doesn't:
private void displayAddressBookToolStripMenuItem_Click(object sender, EventArgs e)
{
if (lstResults.SelectedIndex != -1)
{
foreach (Contact contact in addressBk)
{
lstResults.Items = contact.LastName; // I know this is wrong but I've tried lstResults.Items.Add(contact.LastName); with no errors but no results either
}
}
}
// Looks like I fixed it with the below. Note the parts I commented out.
private void displayAddressBookToolStripMenuItem_Click(object sender, EventArgs e)
{
// string lname;
//if (lstResults.SelectedIndex != -1)
// {
lstResults.Items.Clear();
foreach (Contact contact in addressBk)
{
// lstResults.Items.Add(contact.LastName);
// lname = contact.LastName;
lstResults.Items.Add(contact.LastName); // Can't figure out how to put both first and last name in listbox on the same row
}
// }
}