I wanted to make an address book in C# and this is my first try with LINQ.
Generally it works O.K. but there is a strange problem:
When I search for an contact and I double click on the contact found in search instead of showing me the details for that contact it shows the details for the first contact in list or if I choose the second option from search it shows me the details for the second contact in list instead of showing me the details for the contact in search...(I use the same window for listing contacts and showing the search results)
Pictures and code:



Code for Search:
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
{
//If enter key was pressed then search for that key in database and populate the list.
if (e.KeyChar == (char)13)
{
string searhName = txtSearch.Text;
List<Contacts> searchedContactList = new List<Contacts>();
foreach (Contacts contact in XMLParse.ContactList)
{
if (contact.FName.IndexOf(searhName, StringComparison.CurrentCultureIgnoreCase) != -1|| contact.LName.IndexOf(searhName, StringComparison.CurrentCultureIgnoreCase) != -1)
searchedContactList.Add(contact);
}
populateData(searchedContactList);
}
}
Code for populatedata method:
private void populateData(List<Contacts> ContactList)
{
_dgAddressBook.BeginUpdate();
_dgAddressBook.Items.Clear();
foreach (Contacts cts in ContactList)
{
ListViewItem item = _dgAddressBook.Items.Add(cts.FName);
item.SubItems.Add(cts.LName);
item.SubItems.Add(cts.Phone1);
item.SubItems.Add(cts.Phone2);
item.SubItems.Add(cts.Address1);
item.SubItems.Add(cts.Email);
}
_dgAddressBook.EndUpdate();
}
Code for double click in list(search results window)
private void _dgAddressBook_DoubleClick(object sender, EventArgs e)
{
if (_dgAddressBook.SelectedItems.Count <= 0)
return;
int index = _dgAddressBook.SelectedItems[0].Index;
AddressDetail ad = new AddressDetail(XMLParse.ContactList, index);
ad.ShowDialog(); // details for contact form
populateData(XMLParse.ContactList);
}
Code for Contact list
public static List<Contacts> ContactList
{
get
{
if (_contactList == null)
_contactList = new List<Contacts>();
return _contactList;
}
}
So where I am wrong????
This post has been edited by chris99: 19 March 2012 - 12:34 AM

New Topic/Question
Reply




MultiQuote






|