Update ListBox

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I have a listbox to hold contact info...when I delete a contact I would like to update the listbox....but everything I have tried doesnt seem to work....i have a "deletecontact" method and a "loadcontactlist" method...

the delete method removes the contact from the xml file and the loadcontacts method reads a xml file and loads the data into the listbox....below is the delete method, I removing the listbox items work because if I comment out the loadcontacts method..all the contents of the listbox are removed...but they dont get removed when I uncomment the loadcontacts method...not sure why.

btw - when I use lstContacts.Items.Clear() I get an error saying cant use when ItemSource is in use!

 private void DeleteContact_Click(object sender, RoutedEventArgs e)        {
           
            if (lstContacts.SelectedIndex == -1)
            {
                return;
            }
            else
            {
                contact.DeleteNode(AppDomain.CurrentDomain.BaseDirectory + "Contacts.xml", lstContacts.SelectedIndex + 1, lstContacts);
                Thread.Sleep(500);
                lstContacts.ItemsSource = null;
              
                for (int i = 0; i < lstContacts.Items.Count; i++)
                {
                    int j = lstContacts.SelectedIndex;
                    lstContacts.Items.RemoveAt(j);
                    j++;
                }
               
           
                if (lstContacts.Items.Count == 0)
                {
                    Thread.Sleep(500);
                    contact.LoadContactList(AppDomain.CurrentDomain.BaseDirectory + "Contacts.xml", lstContacts);
                }
                
            }
        }


         public void LoadContactList(string path, System.Windows.Controls.ListBox lst)        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(path);
          
            XmlNodeList contactNode = xDoc.SelectNodes("/Contacts/Contact");


            foreach (XmlNode node in contactNode)
            {
                if (node["ContactName"] != null)
                {
                    string id = node["ID"].InnerText;
                    string name = node["ContactName"].InnerText;
                    string phone = node["Phone"].InnerText;
                    string address = node["Address"].InnerText;
                    contactList.Add(new Contact() { ID = id, ContactName = name, ContactPhone = phone, Address = address });
                    nameList.Add(ContactName);
                    contactIndex.Add(Convert.ToInt32(id));
                   
                }
                else
                lst.Items.Add("No Contacts");
            }
            lst.ItemsSource = contactList;
        
        }


any help would be appreciated

-InkedGFX
 
Back
Top Bottom