编写此案例的目的是为了描述在普通的应用程序中如何运用DOM技术以及对上一篇文章《C#中使用XML——实现DOM》中所讲述的DOM的相关知识回顾一下,本案例将分析一个联系人应用程序,在这里将XML文档充当数据库来使用, 所有的联系人信息存储在XML文档中,同时,在程序中使用DOM对联系人文档进行查询、编辑、更新等操作。具体来说本案例将实现以下功能:
. 添加一个新的联系人
. 修改现有联系人
. 删除现有联系人
. 按姓氏查询联系人
. 按名字查询联系人
6. 将所有联系人导出到另一个XML文件
7. 将联系人从另一个XML文件导入
以下是程序运行效果图:
应用程序主窗体:
添加联系人窗体:
修改联系人窗体:
以下是用于测试程序的XML文件:
contact.xml 将该文件保存在项目目录下
<?xml version=".0" encoding="gb"?>
<ContactDetails>
<Contact>
<name>
<first>Steven</first>
<last>Perez</last>
</name>
<note>[email protected];system at http://www.details.net/token</note>
</Contact>
<Contact>
<name>
<first>Billoys</first>
<last>Perez</last>
</name>
<note>[email protected];system at http://www.Billoys.com/Billoys.htm</note>
</Contact>
<Contact>
<name>
<first>刘</first>
<last>罗锅</last>
</name>
<note>古代人</note>
</Contact>
</ContactDetails>
contact.xml 该文件用于实现导入联系人功能,将该文件随便保存在一个目录下然后将保存路径连同文件名拷贝到主窗体的“保存的路径”文本框中再单击“导入”按纽即可实现导入功能。
<?xml version=".0" encoding="gb"?>
<ContactDetails>
<Contact>
<name>
<first>Steven</first>
<last>Perez</last>
</name>
<note>[email protected];system at http://www.details.net/token</note>
</Contact>
<Contact>
<name>
<first>Billoys</first>
<last>Perez</last>
</name>
<note>[email protected];system at http://www.Billoys.com/Billoys.htm</note>
</Contact>
<Contact>
<name>
<first>刘</first>
<last>德华</last>
</name>
<note>香港著名艺人,工作勤恳同时不忘生活,出演电影00多部,演技已达登峰造极,刻画人物栩栩如生</note>
</Contact>
<Contact>
<name>
<first>扬</first>
<last>震</last>
</name>
<note>重案六组探员,为人胆大心细,沉着冷静,富有人情味,经历几次案件后更加成熟,在成长中不断磨练,是个真的汉子,正应验那句话:成就靠真本事</note>
</Contact>
<Contact>
<name>
<first>季</first>
<last>洁</last>
</name>
<note>重案六组探员,富有人情味,对扬震早已芳心默许,知道为什么吗?因为她天生就爱保护别人,当她看到扬震被别人用枪指着头吓的回不过神来时就对这个真实的男人产生了感觉,真可谓巾帼不让须眉</note>
</Contact>
</ContactDetails>
导出联系人时在“保存的路径”文本框中输入一个文件路径,程序将在该路径下创建一个XML文件,如果该文件存在于该路径上,程序将对该XML文件进行重写。
为实现以上所述所有功能,我专门编写了一个类来封装实现代码,该类代码如下:
namespace ContactApplication
{
using System;
using System.Xml;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;
/// <summary>
/// Contact 联系人
/// </summary>
public class Contact : IDisposable
{
private string xmlPath;
private XmlDocument xmlDoc;
private XmlNode selectNode;
private string firstName;
private string lastName;
private string note;
#region Contact 构造器
/// <summary>
/// 默认构造器
/// </summary>
public Contact()
{
this.xmlPath = "../../Contact.xml";
this.selectNode = null;
this.xmlDoc = new XmlDocument();
this.xmlDoc.Load(this.xmlPath);
this.firstName = string.Empty;
this.lastName = string.Empty;
this.note = string.Empty;
}
/// <summary>
/// 使用姓氏,名字,个人信息构造一个联系人对象
/// </summary>
/// <param name="firstName">姓氏</param>
/// <param name="lastName">名字</param>
/// <param name="note">个人信息</param>
public Contact(string firstName, string lastName, string note)
{
this.xmlPath = "../../Contact.xml";
this.selectNode = null;
this.xmlDoc = new XmlDocument();
this.xmlDoc.Load(this.xmlPath);
this.firstName = firstName;
this.lastName = lastName;
this.note = note;
}
#endregion
#region Contact 资源释放方法
/// <summary>
/// 清理该对象所有正在使用的资源
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 释放该对象的实例变量
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (!disposing)
return;
if (this.xmlPath != null)
this.xmlPath = null;
if (this.xmlDoc != null)
this.xmlDoc = null;
if (this.selectNode != null)
this.selectNode = null;
if (this.firstName != null)
this.firstName = null;
if (this.lastName != null)
this.lastName = null;
if (this.note != null)
this.note = null;
}
#endregion
#region Contact 属性
/// <summary>
/// 姓氏
/// </summary>
public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
}
}
/// <summary>
/// 名字
/// </summary>
public string LastName
{
get
{
return this.lastName;
}
set
{
this.lastName = value;
}
}
/// <summary>
/// 个人信息
/// </summary>
public string Note
{
get
{
return this.note;
}
set
{
this.note = value;
}
}
#endregion
#region Contact 功能函数
/// <summary>
/// 加载所有的联系人姓名
/// </summary>
/// <returns>联系人姓名列表</returns>
public ArrayList LoadContactName()
{
ArrayList nameList= new ArrayList();
XmlNodeList nameNodeList = xmlDoc.SelectNodes("//name");
foreach(XmlNode nameNode in nameNodeList)
{
XmlElement firstNameNode = (XmlElement)nameNode.FirstChild;
XmlText firstNameText = (XmlText)firstNameNode.FirstChild;
XmlElement lastNameNode = (XmlElement)nameNode.LastChild;
XmlText lastNameText = (XmlText)lastNameNode.FirstChild;
nameList.Add(lastNameText.Value + " " + firstNameText.Value);
}
return nameList;
}
/// <summary>
/// 获取note节点的文本值
/// </summary>
/// <returns>note节点的文本值</returns>
public string DisplayNote()
{
string note = string.Empty;
XmlElement selectName = (XmlElement)xmlDoc.SelectSingleNode(string.Format("//name[first='{0}' and last='{}']",this.firstName,this.lastName));
note = selectName.NextSibling.InnerText;
return note;
}
/// <summary>
/// 搜索联系人
/// </summary>
/// <remarks>
/// 根据传入的搜索类型(按姓或名)以及搜索值查询符合条件的联系人信息,并以对话框形式显示
/// </remarks>
/// <param name="searchType">搜索类型(first或last)</param>
/// <param name="searchValue">搜索值</param>
public void SearchContact(string searchType, string searchValue)
{
string contactInfo = string.Empty;
int i = 0;
XmlNodeList nameNodeList = xmlDoc.SelectNodes(string.Format("//name[{0}='{}']",searchType,searchValue));
if(searchType == "first")
{
MessageBox.Show(string.Format("符合{0}姓氏的联系人有{}个",searchValue,nameNodeList.Count),"搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information);
if (nameNodeList.Count == 0)
return;
foreach(XmlNode nameNode in nameNodeList)
{
i++;
contactInfo = nameNode.LastChild.InnerText + " " + nameNode.FirstChild.InnerText;
contactInfo = contactInfo + System.Environment.NewLine + "====================" + System.Environment.NewLine + nameNode.NextSibling.InnerText;
MessageBox.Show(string.Format("第{0}个联系人:{}{}{}",i,System.Environment.NewLine,System.Environment.NewLine,contactInfo),"搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
else if(searchType == "last")
{
MessageBox.Show(string.Format("符合{0}名字的联系人有{}个",searchValue,nameNodeList.Count),"搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information);
if (nameNodeList.Count == 0)
return;
foreach(XmlNode nameNode in nameNodeList)
{
i++;
contactInfo = nameNode.LastChild.InnerText + " " + nameNode.FirstChild.InnerText;
contactInfo = contactInfo + System.Environment.NewLine + "====================" + System.Environment.NewLine + nameNode.NextSibling.InnerText;
MessageBox.Show(string.Format("第{0}个联系人:{}{}{}",i,System.Environment.NewLine,System.Environment.NewLine,contactInfo),"搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("没有发现与您的搜索条件匹配的项,请检查您的操作是否正确!","搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
/// <summary>
/// 添加联系人
/// </summary>
public void AddContact()
{
XmlDocumentFragment xmlDocFrag = xmlDoc.CreateDocumentFragment();
XmlElement contactEle = xmlDoc.CreateElement("Contact");
XmlElement nameEle = xmlDoc.CreateElement("name");
XmlElement firstNameEle = xmlDoc.CreateElement("first");
firstNameEle.InnerText = this.firstName;
nameEle.AppendChild(firstNameEle);
XmlElement lastNameEle = xmlDoc.CreateElement("last");
lastNameEle.InnerText = this.lastName;
nameEle.AppendChild(lastNameEle);
XmlElement noteEle = xmlDoc.CreateElement("note");
noteEle.InnerText = this.note;
contactEle.AppendChild(nameEle);
contactEle.AppendChild(noteEle);
xmlDocFrag.AppendChild(contactEle);
XmlElement detailsEle = (XmlElement)xmlDoc.SelectSingleNode("/ContactDetails");
detailsEle.AppendChild(xmlDocFrag.FirstChild);
xmlDoc.Save(this.xmlPath);
}
/// <summary>
/// 修改联系人
/// </summary>
public void UpdateContact()
{
selectNode.FirstChild.InnerText = this.firstName;
selectNode.LastChild.InnerText = this.lastName;
selectNode.NextSibling.InnerText = this.note;
xmlDoc.Save(this.xmlPath);
}
/// <summary>
/// 删除联系人
/// </summary>
public void DeleteContact()
{
XmlElement contactEle = (XmlElement)this.selectNode.ParentNode;
XmlElement detailsEle = (XmlElement)contactEle.ParentNode;
detailsEle.RemoveChild(contactEle);
xmlDoc.Save(this.xmlPath);
}
/// <summary>
/// 根据列表框中选中的联系人在文档中选择一个对应的联系人
/// </summary>
public void SelectContact()
{
this.selectNode = xmlDoc.SelectSingleNode(string.Format("//name[first='{0}' and last='{}']",this.firstName,this.lastName));
}
/// <summary>
/// 导出联系人
/// </summary>
/// <param name="filePath">要导出的路径</param>
/// <returns>是否成功导出</returns>
public string ExportContacts(string filePath)
{
string isOk = "is not OK";
if (filePath != null)
{
XmlTextWriter xmlTxtWt = new XmlTextWriter(filePath,Encoding.UTF8);
xmlDoc.Save(xmlTxtWt);
xmlTxtWt.Close();
isOk = "is OK";
}
return isOk;
}
/// <summary>
/// 导入联系人
/// </summary>
/// <param name="filePath">要导入的路径</param>
public void ImportContacts(string filePath)
{
string impFirstName = string.Empty;
string impLastName = string.Empty;
XmlNode cNode;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNodeList contactNodeList = xmlDoc.SelectNodes("//Contact");
foreach(XmlNode contactNode in contactNodeList)
{
impFirstName = contactNode.FirstChild.FirstChild.ChildNodes[0].Value;
impLastName = contactNode.FirstChild.LastChild.ChildNodes[0].Value;
cNode = this.xmlDoc.SelectSingleNode(string.Format("//name[first='{0}' and last='{}']",impFirstName,impLastName));
if (cNode == null)
{
XmlNode importNode = xmlDoc.ImportNode(contactNode,true);
xmlDoc.SelectSingleNode("/ContactDetails").AppendChild(importNode);
}
}
xmlDoc.Save(this.xmlPath);
}
#endregion
}
}
程序主窗体代码如下:
namespace ContactApplication
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
/// <summary>
/// Form 的摘要说明。
/// </summary>
public class Form : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.RadioButton radioButton;
private System.Windows.Forms.RadioButton radioButton;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Label label;
private System.Windows.Forms.ListBox listBox;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Button button6;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.radioButton = new System.Windows.Forms.RadioButton();
this.radioButton = new System.Windows.Forms.RadioButton();
this.button = new System.Windows.Forms.Button();
this.label = new System.Windows.Forms.Label();
this.listBox = new System.Windows.Forms.ListBox();
this.textBox = new System.Windows.Forms.TextBox();
this.groupBox = new System.Windows.Forms.GroupBox();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.button = new System.Windows.Forms.Button();
this.button = new System.Windows.Forms.Button();
this.button = new System.Windows.Forms.Button();
this.button = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.groupBox.SuspendLayout();
this.SuspendLayout();
//
// label
//
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(8, 8);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(79, 7);
this.label.TabIndex = 0;
this.label.Text = "搜索联系人:";
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(8, 0);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(0, );
this.textBox.TabIndex = ;
this.textBox.Text = "";
//
// radioButton
//
this.radioButton.Location = new System.Drawing.Point(8, 7);
this.radioButton.Name = "radioButton";
this.radioButton.Size = new System.Drawing.Size(80, );
this.radioButton.TabIndex = ;
this.radioButton.Text = "按姓搜索";
//
// radioButton
//
this.radioButton.Location = new System.Drawing.Point(8, 0);
this.radioButton.Name = "radioButton";
this.radioButton.Size = new System.Drawing.Size(80, );
this.radioButton.TabIndex = ;
this.radioButton.Text = "按名搜索";
//
// button
//
this.button.Location = new System.Drawing.Point(8, 6);
this.button.Name = "button";
this.button.Size = new System.Drawing.Size(0, );
this.button.TabIndex = ;
this.button.Text = "查找联系人";
this.button.Click += new System.EventHandler(this.button_Click);
//
// label
//
this.label.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(, 8);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(79, 7);
this.label.TabIndex = ;
this.label.Text = "联系人列表:";
//
// listBox
//
this.listBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox.ItemHeight = ;
this.listBox.Location = new System.Drawing.Point(, 0);
this.listBox.Name = "listBox";
this.listBox.ScrollAlwaysVisible = true;
this.listBox.Size = new System.Drawing.Size(88, 6);
this.listBox.TabIndex = 6;
this.listBox.SelectedIndexChanged += new System.EventHandler(this.listBox_SelectedIndexChanged);
//
// textBox
//
this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox.Location = new System.Drawing.Point(, );
this.textBox.Multiline = true;
this.textBox.Name = "textBox";
this.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox.Size = new System.Drawing.Size(88, 8);
this.textBox.TabIndex = 7;
this.textBox.Text = "";
//
// groupBox
//
this.groupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox.Controls.Add(this.textBox);
this.groupBox.Controls.Add(this.label);
this.groupBox.Controls.Add(this.button);
this.groupBox.Controls.Add(this.button);
this.groupBox.Location = new System.Drawing.Point(0, 8);
this.groupBox.Name = "groupBox";
this.groupBox.Size = new System.Drawing.Size(0, 6);
this.groupBox.TabIndex = 8;
this.groupBox.TabStop = false;
this.groupBox.Text = "导出/导入联系人";
//
// textBox
//
this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.textBox.Location = new System.Drawing.Point(9, );
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(0, );
this.textBox.TabIndex = ;
this.textBox.Text = "";
//
// label
//
this.label.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.label.Location = new System.Drawing.Point(9, 6);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(7, 6);
this.label.TabIndex = ;
this.label.Text = "保存的路径";
//
// button
//
this.button.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.button.Location = new System.Drawing.Point(0, );
this.button.Name = "button";
this.button.TabIndex = ;
this.button.Text = "导出";
this.button.Click += new System.EventHandler(this.button_Click);
//
// button
//
this.button.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.button.Location = new System.Drawing.Point(6, );
this.button.Name = "button";
this.button.TabIndex = 0;
this.button.Text = "导入";
this.button.Click += new System.EventHandler(this.button_Click);
//
// button
//
this.button.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button.Location = new System.Drawing.Point(8, 8);
this.button.Name = "button";
this.button.Size = new System.Drawing.Size(96, );
this.button.TabIndex = 9;
this.button.Text = "添加联系人";
this.button.Click += new System.EventHandler(this.button_Click);
//
// button
//
this.button.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button.Location = new System.Drawing.Point(, 8);
this.button.Name = "button";
this.button.Size = new System.Drawing.Size(96, );
this.button.TabIndex = 0;
this.button.Text = "修改联系人";
this.button.Click += new System.EventHandler(this.button_Click);
//
// button6
//
this.button6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button6.Location = new System.Drawing.Point(6, 8);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(96, );
this.button6.TabIndex = ;
this.button6.Text = "删除联系人";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, );
this.ClientSize = new System.Drawing.Size(0, 7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button);
this.Controls.Add(this.button);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.textBox);
this.Controls.Add(this.listBox);
this.Controls.Add(this.label);
this.Controls.Add(this.button);
this.Controls.Add(this.radioButton);
this.Controls.Add(this.radioButton);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label);
this.Name = "Form";
this.Text = "联系人应用程序";
this.Load += new System.EventHandler(this.Form_Load);
this.groupBox.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form());
}
private void Form_Load(object sender, System.EventArgs e)
{
Contact contact = new Contact();
this.BindToListBox(contact);
}
private void BindToListBox(Contact contact)
{
this.listBox.Items.Clear();
ArrayList nameList;
try
{
nameList = contact.LoadContactName();
foreach(object obj in nameList)
{
this.listBox.Items.Add(obj.ToString());
}
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
contact.Dispose();
}
}
private void listBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
Contact contact = new Contact();
string itemText = this.listBox.GetItemText(listBox.SelectedItem);
string firstName = itemText.Split(" ".ToCharArray())[].Trim().ToString();
string lastName = itemText.Split(" ".ToCharArray())[0].Trim().ToString();
contact.FirstName = firstName;
contact.LastName = lastName;
try
{
this.textBox.Text = contact.DisplayNote();
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
contact.Dispose();
}
}
private void button_Click(object sender, System.EventArgs e)
{
string searchType = string.Empty;
string searchValue = string.Empty;
Contact contact = new Contact();
if (this.radioButton.Checked == true)
searchType = "first";
if (this.radioButton.Checked == true)
searchType = "last";
if (this.textBox.Text != "")
searchValue = this.textBox.Text;
try
{
contact.SearchContact(searchType,searchValue);
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
contact.Dispose();
}
}
private void button_Click(object sender, System.EventArgs e)
{
string firstName = string.Empty;
string lastName = string.Empty;
string note = string.Empty;
Form frm = new Form();
frm.ShowDialog();
if (frm.DialogResult == DialogResult.OK)
{
firstName = frm.Controls[0].Text;
lastName = frm.Controls[].Text;
note = frm.Controls[].Text;
Contact contact = new Contact(firstName,lastName,note);
try
{
contact.AddContact();
this.BindToListBox(contact);
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
private void button_Click(object sender, System.EventArgs e)
{
if (this.listBox.SelectedItem == null)
{
MessageBox.Show("要修改联系人信息请先在联系人列表中选中","修改联系人",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
return;
}
string firstName = string.Empty;
string lastName = string.Empty;
string note = string.Empty;
string itemText = string.Empty;
itemText = this.listBox.GetItemText(listBox.SelectedItem);
firstName = itemText.Split(" ".ToCharArray())[].Trim().ToString();
lastName = itemText.Split(" ".ToCharArray())[0].Trim().ToString();
Contact contact = new Contact();
contact.FirstName = firstName;
contact.LastName = lastName;
try
{
note = contact.DisplayNote();
contact.SelectContact();
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
Form frm = new Form();
frm.Controls[0].Text = firstName;
frm.Controls[].Text = lastName;
frm.Controls[].Text = note;
frm.ShowDialog();
if (frm.DialogResult == DialogResult.OK)
{
firstName = frm.Controls[0].Text;
lastName = frm.Controls[].Text;
note = frm.Controls[].Text;
contact.FirstName = firstName;
contact.LastName = lastName;
contact.Note = note;
try
{
contact.UpdateContact();
this.BindToListBox(contact);
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
private void button6_Click(object sender, System.EventArgs e)
{
if (this.listBox.SelectedItem == null)
{
MessageBox.Show("要删除联系人信息请先在联系人列表中选中","删除联系人",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
return;
}
string firstName = string.Empty;
string lastName = string.Empty;
string itemText = string.Empty;
itemText = this.listBox.GetItemText(listBox.SelectedItem);
firstName = itemText.Split(" ".ToCharArray())[].Trim().ToString();
lastName = itemText.Split(" ".ToCharArray())[0].Trim().ToString();
if (MessageBox.Show("是否确定删除联系人,删除后将不可恢复!","删除联系人",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)
{
Contact contact = new Contact();
contact.FirstName = firstName;
contact.LastName = lastName;
try
{
contact.SelectContact();
contact.DeleteContact();
this.BindToListBox(contact);
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
private void button_Click(object sender, System.EventArgs e)
{
string filePath = this.textBox.Text;
Contact contact = new Contact();
try
{
MessageBox.Show("Export " + contact.ExportContacts(filePath),"导出联系人",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
contact.Dispose();
}
}
private void button_Click(object sender, System.EventArgs e)
{
if (this.textBox.Text == "")
return;
string filePath = this.textBox.Text;
Contact contact = new Contact();
try
{
contact.ImportContacts(filePath);
this.BindToListBox(contact);
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}
添加联系人窗体代码如下:
namespace ContactApplication
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
/// <summary>
/// Form 的摘要说明。
/// </summary>
public class Form : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button;
private System.Windows.Forms.Button button;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button = new System.Windows.Forms.Button();
this.button = new System.Windows.Forms.Button();
this.textBox = new System.Windows.Forms.TextBox();
this.textBox = new System.Windows.Forms.TextBox();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button
//
this.button.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button.Location = new System.Drawing.Point(6, );
this.button.Name = "button";
this.button.TabIndex = 0;
this.button.Text = "确定";
//
// button
//
this.button.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button.Location = new System.Drawing.Point(96, );
this.button.Name = "button";
this.button.TabIndex = ;
this.button.Text = "取消";
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(6, 6);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(, );
this.textBox.TabIndex = ;
this.textBox.Text = "";
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(6, 6);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(, );
this.textBox.TabIndex = ;
this.textBox.Text = "";
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(6, 96);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(, );
this.textBox.TabIndex = ;
this.textBox.Text = "";
//
// label
//
this.label.Location = new System.Drawing.Point(6, 6);
this.label.Name = "label";
this.label.TabIndex = ;
this.label.Text = "姓氏:";
this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label
//
this.label.Location = new System.Drawing.Point(6, 6);
this.label.Name = "label";
this.label.TabIndex = 6;
this.label.Text = "名字:";
this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label
//
this.label.Location = new System.Drawing.Point(6, 96);
this.label.Name = "label";
this.label.TabIndex = 7;
this.label.Text = "个人信息:";
this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label
//
this.label.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label.Location = new System.Drawing.Point(0, 6);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(8, );
this.label.TabIndex = 8;
//
// Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, );
this.ClientSize = new System.Drawing.Size(78, 8);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.textBox);
this.Controls.Add(this.textBox);
this.Controls.Add(this.textBox);
this.Controls.Add(this.button);
this.Controls.Add(this.button);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "添加联系人";
this.Load += new System.EventHandler(this.Form_Load);
this.ResumeLayout(false);
this.Controls.SetChildIndex(this.textBox,0);
this.Controls.SetChildIndex(this.textBox,);
this.Controls.SetChildIndex(this.textBox,);
}
#endregion
private void Form_Load(object sender, System.EventArgs e)
{
}
}
}
修改联系人窗体如下:
namespace ContactApplication
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
/// <summary>
/// Form 的摘要说明。
/// </summary>
public class Form : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Button button;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.textBox = new System.Windows.Forms.TextBox();
this.textBox = new System.Windows.Forms.TextBox();
this.button = new System.Windows.Forms.Button();
this.button = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label
//
this.label.Location = new System.Drawing.Point(6, 6);
this.label.Name = "label";
this.label.TabIndex = 0;
this.label.Text = "姓氏:";
this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label
//
this.label.Location = new System.Drawing.Point(6, 6);
this.label.Name = "label";
this.label.TabIndex = ;
this.label.Text = "名字:";
this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label
//
this.label.Location = new System.Drawing.Point(6, 96);
this.label.Name = "label";
this.label.TabIndex = ;
this.label.Text = "个人信息:";
this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label
//
this.label.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label.Location = new System.Drawing.Point(0, 8);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(8, );
this.label.TabIndex = ;
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(6, 6);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(, );
this.textBox.TabIndex = ;
this.textBox.Text = "";
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(6, 6);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(, );
this.textBox.TabIndex = ;
this.textBox.Text = "";
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(6, 96);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(, );
this.textBox.TabIndex = 6;
this.textBox.Text = "";
//
// button
//
this.button.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button.Location = new System.Drawing.Point(6, );
this.button.Name = "button";
this.button.TabIndex = 7;
this.button.Text = "确定";
//
// button
//
this.button.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button.Location = new System.Drawing.Point(96, );
this.button.Name = "button";
this.button.TabIndex = 8;
this.button.Text = "取消";
//
// Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, );
this.ClientSize = new System.Drawing.Size(78, 7);
this.Controls.Add(this.button);
this.Controls.Add(this.button);
this.Controls.Add(this.textBox);
this.Controls.Add(this.textBox);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "修改联系人";
this.Load += new System.EventHandler(this.Form_Load);
this.ResumeLayout(false);
this.Controls.SetChildIndex(this.textBox,0);
this.Controls.SetChildIndex(this.textBox,);
this.Controls.SetChildIndex(this.textBox,);
}
#endregion
private void Form_Load(object sender, System.EventArgs e)
{
}
}
}








