Non functional buttons- Save Changes, Remove/ Missing Search button

+2 Kristina Parvanova · September 10, 2014
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace AddressBook
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<Contacts> contacts = new List<Contacts>();
        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            if (!Directory.Exists(path + "\\Address Book"))
            Directory.CreateDirectory(path + "\\Address Book");
            if (!File.Exists(path + "\\Address Book\\store.xml"))

            {
               XmlTextWriter xw = new XmlTextWriter(path + "\\Address Book\\store.xml", Encoding.UTF8);
               xw.WriteStartElement("Contacts");
               xw.WriteEndElement();
               xw.Close();
            }
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(path + "\\Address Book\\store.xml");
            foreach (XmlNode xNode in xDoc.SelectNodes("Contacts/Contacts"))
            {
                Contacts c = new Contacts();
                c.Name = xNode.SelectSingleNode("Name").InnerText;
                c.Email = xNode.SelectSingleNode("Email").InnerText;
                c.Phone = xNode.SelectSingleNode("Phone").InnerText;
                c.Address = xNode.SelectSingleNode("Address").InnerText;
           
                contacts.Add(c);
                listView1.Items.Add(c.Name);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Contacts c = new Contacts();
            c.Name = textBox1.Text;
            c.Email = textBox2.Text;
            c.Phone = textBox3.Text;
            c.Address = textBox4.Text;
            c.Photo = pictureBox1.Image;
            
           
            
            contacts.Add(c);
            listView1.Items.Add(c.Name);
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            
            
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = contacts[listView1.SelectedItems[0].Index].Name;
            textBox2.Text = contacts[listView1.SelectedItems[0].Index].Email;
            textBox3.Text = contacts[listView1.SelectedItems[0].Index].Phone;
            textBox4.Text = contacts[listView1.SelectedItems[0].Index].Address;
            pictureBox1.Image = contacts[listView1.SelectedItems[0].Index].Photo;
            listView1.SelectedItems[0].Text = textBox1.Text;
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Remove();
        }

        void Remove()
        {
            try
            {
                listView1.Items.Remove(listView1.SelectedItems[0]);
                contacts.RemoveAt(listView1.SelectedItems[0].Index);
            }
            catch { }
        

        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Remove();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pictureBox1.ImageLocation = ofd.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            contacts[listView1.SelectedItems[0].Index].Name = textBox1.Text;
            contacts[listView1.SelectedItems[0].Index].Email = textBox2.Text;
            contacts[listView1.SelectedItems[0].Index].Phone = textBox3.Text;
            contacts[listView1.SelectedItems[0].Index].Address = textBox4.Text;
            contacts[listView1.SelectedItems[0].Index].Photo = pictureBox1.Image;

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            XmlDocument xDoc = new XmlDocument();
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            xDoc.Load(path + "\\Address Book\\store.xml");
            XmlNode xNode = xDoc.SelectSingleNode("Contacts");
            xNode.RemoveAll();
            foreach (Contacts c in contacts)
            {
                XmlNode xTop = xDoc.CreateElement("Contacts");
                XmlNode xName = xDoc.CreateElement("Name");
                XmlNode xEmail = xDoc.CreateElement("Email");
                XmlNode xPhone = xDoc.CreateElement("Phone");
                XmlNode xAddress = xDoc.CreateElement("Address");
                XmlNode xPhoto = xDoc.CreateElement("Photo");
                xName.InnerText = c.Name;
                xEmail.InnerText = c.Email;
                xPhone.InnerText = c.Phone;
                xPhone.InnerText = c.Address;
              
                xTop.AppendChild(xName);
                xTop.AppendChild(xEmail);
                xTop.AppendChild(xPhone);
                xTop.AppendChild(xAddress);
                xTop.AppendChild(xPhoto);
                xDoc.DocumentElement.AppendChild(xTop);
            }
            xDoc.Save(path + "\\Address Book\\store.xml");
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
    class Contacts
    {
        public string Name
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public string Phone
        {
            get;
            set;
        }
        public string Address
        {
            get;
            set;
        }
        public Image Photo
        {
            get;
            set;
        }
    }
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Eugene Botma · September 12, 2014
Is there a question? Also, please use the code tags to paste code, it's much more readable then
0 Kristina Parvanova · September 13, 2014
Hi :) Thank You for Your answer, so...I wrote to Bucky that I have urgent need on my task on C#, but he's probably busy. I have a task on C# , the requirement are:
Create an address book:
The functionality:
1.       View contacts – You decide how to show them – List, Table, Grid and etc.
2.       Search for a contact
3.       Add / Modify / Remove contact.
4.       Store contacts - in XML File.
Contacts should have following information:
1.       Name
2.       E-Mail
3.       Phone
4.       Address
5.       Photo
this is my actual code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace AddressBook
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<Contacts> contacts = new List<Contacts>();
        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            if (!Directory.Exists(path + "\\Address Book"))
                Directory.CreateDirectory(path + "\\Address Book");
            if (!File.Exists(path + "\\Address Book\\store.xml"))
            {
                XmlTextWriter xw = new XmlTextWriter(path + "\\Address Book\\store.xml", Encoding.UTF8);
                xw.WriteStartElement("Contacts");
                xw.WriteEndElement();
                xw.Close();
            }
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(path + "\\Address Book\\store.xml");
            foreach (XmlNode xNode in xDoc.SelectNodes("Contacts/Contacts"))
            {
                Contacts c = new Contacts();
                c.Name = xNode.SelectSingleNode("Name").InnerText;
                c.Email = xNode.SelectSingleNode("Email").InnerText;
                c.Phone = xNode.SelectSingleNode("Phone").InnerText;
                c.Address = xNode.SelectSingleNode("Address").InnerText;


                contacts.Add(c);
                listView1.Items.Add(c.Name);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Contacts c = new Contacts();
            c.Name = textBox1.Text;
            c.Email = textBox2.Text;
            c.Phone = textBox3.Text;
            c.Address = textBox4.Text;
            c.Photo = pictureBox1.Image;


            contacts.Add(c);
            listView1.Items.Add(c.Name);
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            pictureBox1.Image = pictureBox1.Image;


        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = contacts[listView1.SelectedItems[0].Index].Name;
            textBox2.Text = contacts[listView1.SelectedItems[0].Index].Email;
            textBox3.Text = contacts[listView1.SelectedItems[0].Index].Phone;
            textBox4.Text = contacts[listView1.SelectedItems[0].Index].Address;
            pictureBox1.Image = contacts[listView1.SelectedItems[0].Index].Photo;
            listView1.SelectedItems[0].Text = textBox1.Text;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Remove();
        }

        void Remove()
        {
            try
            {
                listView1.Items.Remove(listView1.SelectedItems[0]);
                contacts.RemoveAt(listView1.SelectedItems[0].Index);
            }
            catch { }


        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Remove();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pictureBox1.ImageLocation = ofd.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            contacts[listView1.SelectedItems[0].Index].Name = textBox1.Text;
            contacts[listView1.SelectedItems[0].Index].Email = textBox2.Text;
            contacts[listView1.SelectedItems[0].Index].Phone = textBox3.Text;
            contacts[listView1.SelectedItems[0].Index].Address = textBox4.Text;
            contacts[listView1.SelectedItems[0].Index].Photo = pictureBox1.Image;

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            XmlDocument xDoc = new XmlDocument();
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            xDoc.Load(path + "\\Address Book\\store.xml");
            XmlNode xNode = xDoc.SelectSingleNode("Contacts");
            xNode.RemoveAll();
            foreach (Contacts c in contacts)
            {
                XmlNode xTop = xDoc.CreateElement("Contacts");
                XmlNode xName = xDoc.CreateElement("Name");
                XmlNode xEmail = xDoc.CreateElement("Email");
                XmlNode xPhone = xDoc.CreateElement("Phone");
                XmlNode xAddress = xDoc.CreateElement("Address");
                XmlNode xPhoto = xDoc.CreateElement("Photo");
                xName.InnerText = c.Name;
                xEmail.InnerText = c.Email;
                xPhone.InnerText = c.Phone;
                xPhone.InnerText = c.Address;

                xTop.AppendChild(xName);
                xTop.AppendChild(xEmail);
                xTop.AppendChild(xPhone);
                xTop.AppendChild(xAddress);
                xTop.AppendChild(xPhoto);
                xDoc.DocumentElement.AppendChild(xTop);
            }
            xDoc.Save(path + "\\Address Book\\store.xml");
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
       
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            
        }

    }
    }
    class Contacts
    {
        public string Name
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public string Phone
        {
            get;
            set;
        }
        public string Address
        {
            get;
            set;
        }
        public Image Photo
        {
            get;
            set;
        }
    }

Thank You :)
0 Kristina Parvanova · September 13, 2014
My problem is that my search,remove and save buttons doesn't work at all and I can't make them work :( This is my first program and it's about to fail :( :( :(
+1 Eugene Botma · September 13, 2014
What happens when you click the button? does it crash, give an error message, or just does nothing? explain exactly what happens
0 Kristina Parvanova · September 13, 2014
When I select name from the Contacts (ListView1) and then click the Save Changes or Remove button  in the first push doesn't work at all, in the second push gives exception. Save changes botton is supposed to save changes after clicking in contact list and editing some of the contacts./images/forum/upload/2014-09-13/9608a017b2943dbbe6f498f9e59dc56e.jpg
/images/forum/upload/2014-09-13/0927d8d915d3e980f9d2059d35f9d3df.jpg
0 Eugene Botma · September 15, 2014
Quick question, why use  listview instead of a listbox? It seems like you are displaying the items in a vertical manner any way. Listboxes are easier to use than a listviews in my opinion.
0 Eugene Botma · September 15, 2014
First of, you should name your controls. button1 means nothing. While lloking at the code, i see an event for button2_click. I don't know how to fix it because I don't know what button it is or what it is supposed to do. An important thing to learn right of the bat in programming is to use naming conventions. The best one (imo) is to use an abbreviation of the type of control and then a descriptive name of what it is supposed to do, for example:

A save button will have the name: btnSaveContact

The textboxes would be txtName, txtEmail, txtPhone, txtAddress etc.

This makes the code much more readable and understandable, and easier to work with in general.



button1 is the save button as far as I can tell from the code.
It works fine in fact. You just need to make sure that there is an item selected in the listview.

the remove button would work fine if you just swapped the two line inside the if statement. As it is now, you are removing the selected item from the list thus deselecting it, and then trying to use the (now deleted) selected item to remove the contact from the list.

I don't see any code for a search function in there, so of course it's not going to work.

Now where the actual problem lies with the save function is not the button, but the form1_formclosing event handler, where you write the data to the xml file.


XmlNode xTop = xDoc.CreateElement("Contacts");
XmlNode xName = xDoc.CreateElement("Name");
XmlNode xEmail = xDoc.CreateElement("Email");
XmlNode xPhone = xDoc.CreateElement("Phone");
XmlNode xAddress = xDoc.CreateElement("Address");
XmlNode xPhoto = xDoc.CreateElement("Photo");
xName.InnerText = c.Name;
xEmail.InnerText = c.Email;


xPhone.InnerText = c.Phone;

//here you are overwriting the phone number with the address in your xml file,
//and you never save the address anywhere
xPhone.InnerText = c.Address;

//you are also not saving the path to the photo in the xml file.


xTop.AppendChild(xName);
xTop.AppendChild(xEmail);
xTop.AppendChild(xPhone);
xTop.AppendChild(xAddress);
xTop.AppendChild(xPhoto);
xDoc.DocumentElement.AppendChild(xTop);
  • 1

C#

107,028 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators