AddressList

原创 2007年09月23日 12:26:00

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 = "";


        }

 

 


    }

运行后界面如下:

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

C#AddressList_csharp.rar

  • 2011-10-17 11:04
  • 338KB
  • 下载

AddressList

  • 2008-10-26 14:11
  • 10KB
  • 下载

AddressList_csharp

  • 2011-11-30 12:17
  • 377KB
  • 下载

AddressList_csharp3.

  • 2009-11-26 12:57
  • 338KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)