AddressList

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace AddressList
{
    public partial class Form1 : Form
    {
        public XmlDocument xmlDoc = new XmlDocument();
        private string xpath_filter ="@name[parent::contacts or parent::contact]";
        string path = Application.StartupPath+"//contacts.xml";
        string recentNodeText;
        string recentGroup;
        public Form1()
        {
            InitializeComponent();
        }
        private void populateTreeControl(System.Xml.XmlNode document, System.Windows.Forms.TreeNodeCollection nodes)  //加載TreeView使用的函數
        {
            foreach (System.Xml.XmlNode node in document.ChildNodes)
            {
                System.Xml.XmlNode expr = node.SelectSingleNode(xpath_filter);
                if (expr != null)
                {
                    TreeNode new_child = new TreeNode(expr.Value);
                    nodes.Add(new_child);
                    populateTreeControl(node, new_child.Nodes);
                }
            }

        }
        private void initialize()     //初始化界面,加載TreeView
        {
            treeView1.ImageList = this.imageList1;
            System.Xml.XmlDocument document = new System.Xml.XmlDataDocument();
            document.Load(path);
            XmlNode root =document.SelectSingleNode("addressbook");
            XmlNodeList rootlist=root.ChildNodes;
            this.combGroup.Items.Clear();
            foreach(XmlNode xmln in  rootlist)
            {
                XmlElement xmlE = (XmlElement)xmln;
                combGroup.Items.Add(xmlE.GetAttribute("name"));
            }
            populateTreeControl(document.DocumentElement, treeView1.Nodes);
 
        }
        private void allClear()       //清空所有顯示的個人信息
        {
            this.txtAddress.Text = "";
            this.txtage.Text = "";
            this.dateTimePicker1.Format = DateTimePickerFormat.Custom;  //這兩行代碼實現 dateTimePicker控件的清空
            this.dateTimePicker1.CustomFormat = "  ";        
            this.txtEmail.Text = "";
            this.combGroup.Text = "";
            this.txtname.Text = "";
 
        }
        private void Form1_Load(object sender, EventArgs e)
        {

            this.initialize();

        }

        private void butRefresh_Click(object sender, EventArgs e)  //刷新聯繫人信息
        {
            this.allClear();
        }

        private void butAdd_Click(object sender, EventArgs e)     //添加聯繫人
        {
            try
            {
                if (txtAddress.Text != "" & txtage.Text != "" & this.dateTimePicker1.Text != "" & txtEmail.Text != "" & txtname.Text != "")
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(this.txtEmail.Text, @"^([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$"))  //正則表達式判斷是否符合郵件地址格式
                    {
                        bool addNewGroug = true;
                        bool exist = false;
                        this.xmlDoc.Load(path);
                        XmlNode root = xmlDoc.SelectSingleNode("addressbook");
                        XmlElement xe1 = xmlDoc.CreateElement("contact");
                        XmlElement xesub1 = xmlDoc.CreateElement("age");
                        xesub1.InnerText = this.txtage.Text;
                        XmlElement xesub2 = xmlDoc.CreateElement("birthday");
                        xesub2.InnerText = this.dateTimePicker1.Text;
                        XmlElement xesub3 = xmlDoc.CreateElement("address");
                        xesub3.InnerText = this.txtAddress.Text;
                        XmlElement xesub4 = xmlDoc.CreateElement("email");
                        xesub4.InnerText = this.txtEmail.Text;

                        XmlNodeList nodeList = root.ChildNodes;
                        foreach (XmlNode xmlNode in nodeList)
                        {
                            XmlElement xmlEle = (XmlElement)xmlNode;
                            if (xmlEle.GetAttribute("name") == this.combGroup.Text)
                            {
                                XmlNodeList xmlNodli = xmlEle.ChildNodes;
                                foreach (XmlNode xmlnod in xmlNodli)
                                {
                                    XmlElement xmlElemen = (XmlElement)xmlnod;
                                    XmlNodeList Nodeli = xmlElemen.ChildNodes;

                                    if (xmlElemen.GetAttribute("name") == this.txtname.Text)
                                    {
                                        MessageBox.Show("通訊錄中以存在此人信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        exist = true;
                                        addNewGroug = false;
                                        break;
                                    }

                                }
                                if (exist == false)
                                {
                                    addNewGroug = false;
                                    xe1.SetAttribute("name", this.txtname.Text);
                                    xe1.AppendChild(xesub1);
                                    xe1.AppendChild(xesub2);
                                    xe1.AppendChild(xesub3);
                                    xe1.AppendChild(xesub4);
                                    xmlEle.AppendChild(xe1);

                                    xmlDoc.Save(path);
                                    treeView1.Nodes.Clear();
                                    initialize();
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }

                        if (addNewGroug == true)
                        {
                            if (MessageBox.Show("是否確定添加新分組", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                            {
                                XmlElement newgroug = xmlDoc.CreateElement("contacts");
                                newgroug.SetAttribute("name", this.combGroup.Text);
                                xe1.SetAttribute("name", this.txtname.Text);
                                xe1.AppendChild(xesub1);
                                xe1.AppendChild(xesub2);
                                xe1.AppendChild(xesub3);
                                xe1.AppendChild(xesub4);
                                newgroug.AppendChild(xe1);
                                root.AppendChild(newgroug);

                                xmlDoc.Save(path);
                                treeView1.Nodes.Clear();
                                initialize();
                            }
                            else
                            {

                            }

                        }
                    }
                    else
                    {
                        MessageBox.Show("您輸入的email地址不合法!","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("您輸入的信息不完整!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("添加記錄時發生錯誤:" + error.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)     //當點擊TreeView時調用的函數
        {
           
            this.dateTimePicker1.Format = DateTimePickerFormat.Long;
            this.dateTimePicker1.CustomFormat = "";
            recentNodeText = e.Node.Text;
            //this.recentGroup = this.combGroup.Text;
            this.xmlDoc.Load(path);
            XmlNode xn = xmlDoc.SelectSingleNode("addressbook");
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xns in xnl)   //xns是 分組類型
            {
                XmlElement xe = (XmlElement)xns;
                if (xe.GetAttribute("name") == e.Node.Text)
                {
                    e.Node.Expand();
                    this.allClear();
                }
                else
                {
                    XmlNodeList contactlist = xe.ChildNodes;
                    foreach (XmlNode xmlnode in contactlist)
                    {
                        XmlElement contacteEle = (XmlElement)xmlnode;
                        if (contacteEle.GetAttribute("name") == e.Node.Text)
                        {
                            XmlNodeList xnls = contacteEle.ChildNodes;
                            XmlElement parent = (XmlElement)contacteEle.ParentNode;
                            this.combGroup.Text = parent.GetAttribute("name");
                            this.txtname.Text = contacteEle.GetAttribute("name");
                            this.txtage.Text = xnls[0].InnerText;
                            this.dateTimePicker1.Value = Convert.ToDateTime(xnls[1].InnerText);
                            this.txtAddress.Text = xnls[2].InnerText;
                            this.txtEmail.Text = xnls[3].InnerText;
                            break;
                        }
                    }
                }
            }


        }

        private void butDel_Click(object sender, EventArgs e)      //刪除聯繫人
        {
            try
            {
               
                xmlDoc.Load(path);
                XmlNodeList xnl = xmlDoc.SelectSingleNode("addressbook").ChildNodes;

                foreach (XmlNode xn in xnl)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("name") == recentNodeText)
                    {
                        if (MessageBox.Show("是否刪除該組的所有記錄", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                        {
                            xe.ParentNode.RemoveChild(xe);
                            xmlDoc.Save(path);
                            treeView1.Nodes.Clear();
                            initialize();
                        }
                        break;
                    }
                    else
                    {
                        XmlNodeList contactlist = xe.ChildNodes;
                        foreach (XmlNode xmlnode in contactlist)
                        {
                            XmlElement contacteEle = (XmlElement)xmlnode;
                            if (contacteEle.GetAttribute("name") == recentNodeText)
                            {
                                if (MessageBox.Show("是否刪除該記錄", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                                {
                                    contacteEle.ParentNode.RemoveChild(contacteEle);
                                    xmlDoc.Save(path);
                                    treeView1.Nodes.Clear();
                                    initialize();
                                }
                                break;
                            }
                        }

                    }

 


                }
            }
            catch (Exception error)
            {
                MessageBox.Show("刪除時出錯:" + error.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void butAlter_Click(object sender, EventArgs e)       //修改聯繫人信息
        {
            if (this.txtname.Text == this.recentNodeText)
            {
                if(this.combGroup.Text==recentGroup)
                {
                    xmlDoc.Load(path);
                    XmlNode xmlNode = xmlDoc.SelectSingleNode("addressbook");
                    XmlNodeList xmlNoLi = xmlNode.ChildNodes;
                    foreach (XmlNode xmlNod in xmlNoLi)
                    {
                       XmlElement xmlEle = (XmlElement)xmlNod;
                       if (xmlEle.GetAttribute("name") == this.combGroup.Text)
                       {

                          XmlNodeList xmlNdLi = xmlEle.ChildNodes;
                          foreach (XmlNode xmlNo in xmlNdLi)
                          {
                            XmlElement xmlElem = (XmlElement)xmlNo;
                            if (xmlElem.GetAttribute("name") == this.txtname.Text)
                            {
                                XmlNodeList XmlNolist = xmlElem.ChildNodes;
                                XmlNolist[0].InnerText = this.txtage.Text;
                                XmlNolist[1].InnerText = this.dateTimePicker1.Text;
                                XmlNolist[2].InnerText = this.txtAddress.Text;
                                XmlNolist[3].InnerText = this.txtEmail.Text;
                                xmlDoc.Save(path);
                                this.treeView1.Nodes.Clear();
                                this.initialize();
                                MessageBox.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                break;
                            }
                          }
                          break;
                        }
                    }                 
                }
                else
                {
                    MessageBox.Show("不能修改分組!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("不能修改聯繫人的姓名!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

            }
        }

        private void txtage_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' && e.KeyChar != '.' && e.KeyChar != 8 || e.KeyChar > '9' && e.KeyChar != '.' && e.KeyChar != 8)//實現禁止修改年齡的事件
            {

                e.Handled = true;//是不處理的意思//e.KeyChar   =   8是退格鍵。

            }
        }

        private void dateTimePicker1_CloseUp(object sender, EventArgs e) 
        {
            this.dateTimePicker1.Format = DateTimePickerFormat.Long;
            this.dateTimePicker1.CustomFormat = "";


        }

 

 


    }

運行後界面如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章