编写此案例的目的是为了描述在普通的应用程序中如何运用dom技术以及对上一篇文章《c#中使用xml——实现dom》中所讲述的dom的相关知识回顾一下,本案例将分析一个联系人应用程序,在这里将xml文档充当数据库来使用, 所有的联系人信息存储在xml文档中,同时,在程序中使用dom对联系人文档进行查询、编辑、更新等操作。具体来说本案例将实现以下功能:
1. 添加一个新的联系人
2. 修改现有联系人
3. 删除现有联系人
4. 按姓氏查询联系人
5. 按名字查询联系人
6. 将所有联系人导出到另一个xml文件
7. 将联系人从另一个xml文件导入
以下是程序运行效果图:
应用程序主窗体:
添加联系人窗体:
修改联系人窗体:
以下是用于测试程序的xml文件:
contact.xml 将该文件保存在项目目录下
<?xml version="1.0" encoding="gb2312"?>
<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>
contact2.xml 该文件用于实现导入联系人功能,将该文件随便保存在一个目录下然后将保存路径连同文件名拷贝到主窗体的“保存的路径”文本框中再单击“导入”按纽即可实现导入功能。
<?xml version="1.0" encoding="gb2312"?>
<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>香港著名艺人,工作勤恳同时不忘生活,出演电影100多部,演技已达登峰造极,刻画人物栩栩如生</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='{1}']",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}='{1}']",searchtype,searchvalue));
if(searchtype == "first")
{
messagebox.show(string.format("符合{0}姓氏的联系人有{1}个",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}个联系人:{1}{2}{3}",i,system.environment.newline,system.environment.newline,contactinfo),"搜索联系人",messageboxbuttons.ok,messageboxicon.information);
}
}
else if(searchtype == "last")
{
messagebox.show(string.format("符合{0}名字的联系人有{1}个",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}个联系人:{1}{2}{3}",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='{1}']",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 xmldoc2 = new xmldocument();
xmldoc2.load(filepath);
xmlnodelist contactnodelist = xmldoc2.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='{1}']",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>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.label label1;
private system.windows.forms.textbox textbox1;
private system.windows.forms.radiobutton radiobutton1;
private system.windows.forms.radiobutton radiobutton2;
private system.windows.forms.button button1;
private system.windows.forms.label label2;
private system.windows.forms.listbox listbox1;
private system.windows.forms.textbox textbox2;
private system.windows.forms.groupbox groupbox1;
private system.windows.forms.button button2;
private system.windows.forms.button button3;
private system.windows.forms.label label3;
private system.windows.forms.textbox textbox3;
private system.windows.forms.button button4;
private system.windows.forms.button button5;
private system.windows.forms.button button6;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// 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.label1 = new system.windows.forms.label();
this.textbox1 = new system.windows.forms.textbox();
this.radiobutton1 = new system.windows.forms.radiobutton();
this.radiobutton2 = new system.windows.forms.radiobutton();
this.button1 = new system.windows.forms.button();
this.label2 = new system.windows.forms.label();
this.listbox1 = new system.windows.forms.listbox();
this.textbox2 = new system.windows.forms.textbox();
this.groupbox1 = new system.windows.forms.groupbox();
this.textbox3 = new system.windows.forms.textbox();
this.label3 = new system.windows.forms.label();
this.button3 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.button4 = new system.windows.forms.button();
this.button5 = new system.windows.forms.button();
this.button6 = new system.windows.forms.button();
this.groupbox1.suspendlayout();
this.suspendlayout();
//
// label1
//
this.label1.autosize = true;
this.label1.location = new system.drawing.point(8, 8);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(79, 17);
this.label1.tabindex = 0;
this.label1.text = "搜索联系人:";
//
// textbox1
//
this.textbox1.location = new system.drawing.point(8, 40);
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(104, 21);
this.textbox1.tabindex = 1;
this.textbox1.text = "";
//
// radiobutton1
//
this.radiobutton1.location = new system.drawing.point(8, 72);
this.radiobutton1.name = "radiobutton1";
this.radiobutton1.size = new system.drawing.size(80, 24);
this.radiobutton1.tabindex = 2;
this.radiobutton1.text = "按姓搜索";
//
// radiobutton2
//
this.radiobutton2.location = new system.drawing.point(8, 104);
this.radiobutton2.name = "radiobutton2";
this.radiobutton2.size = new system.drawing.size(80, 24);
this.radiobutton2.tabindex = 3;
this.radiobutton2.text = "按名搜索";
//
// button1
//
this.button1.location = new system.drawing.point(8, 136);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(104, 23);
this.button1.tabindex = 4;
this.button1.text = "查找联系人";
this.button1.click += new system.eventhandler(this.button1_click);
//
// label2
//
this.label2.anchor = ((system.windows.forms.anchorstyles)(((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.left)
| system.windows.forms.anchorstyles.right)));
this.label2.autosize = true;
this.label2.location = new system.drawing.point(152, 8);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(79, 17);
this.label2.tabindex = 5;
this.label2.text = "联系人列表:";
//
// listbox1
//
this.listbox1.anchor = ((system.windows.forms.anchorstyles)(((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.left)
| system.windows.forms.anchorstyles.right)));
this.listbox1.itemheight = 12;
this.listbox1.location = new system.drawing.point(152, 40);
this.listbox1.name = "listbox1";
this.listbox1.scrollalwaysvisible = true;
this.listbox1.size = new system.drawing.size(288, 64);
this.listbox1.tabindex = 6;
this.listbox1.selectedindexchanged += new system.eventhandler(this.listbox1_selectedindexchanged);
//
// textbox2
//
this.textbox2.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.textbox2.location = new system.drawing.point(152, 112);
this.textbox2.multiline = true;
this.textbox2.name = "textbox2";
this.textbox2.scrollbars = system.windows.forms.scrollbars.both;
this.textbox2.size = new system.drawing.size(288, 128);
this.textbox2.tabindex = 7;
this.textbox2.text = "";
//
// groupbox1
//
this.groupbox1.anchor = ((system.windows.forms.anchorstyles)(((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)
| system.windows.forms.anchorstyles.right)));
this.groupbox1.controls.add(this.textbox3);
this.groupbox1.controls.add(this.label3);
this.groupbox1.controls.add(this.button3);
this.groupbox1.controls.add(this.button2);
this.groupbox1.location = new system.drawing.point(0, 248);
this.groupbox1.name = "groupbox1";
this.groupbox1.size = new system.drawing.size(440, 64);
this.groupbox1.tabindex = 8;
this.groupbox1.tabstop = false;
this.groupbox1.text = "导出/导入联系人";
//
// textbox3
//
this.textbox3.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.left | system.windows.forms.anchorstyles.right)));
this.textbox3.location = new system.drawing.point(192, 32);
this.textbox3.name = "textbox3";
this.textbox3.size = new system.drawing.size(240, 21);
this.textbox3.tabindex = 3;
this.textbox3.text = "";
//
// label3
//
this.label3.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.left | system.windows.forms.anchorstyles.right)));
this.label3.location = new system.drawing.point(192, 16);
this.label3.name = "label3";
this.label3.size = new system.drawing.size(72, 16);
this.label3.tabindex = 2;
this.label3.text = "保存的路径";
//
// button3
//
this.button3.anchor = system.windows.forms.anchorstyles.left;
this.button3.location = new system.drawing.point(104, 32);
this.button3.name = "button3";
this.button3.tabindex = 1;
this.button3.text = "导出";
this.button3.click += new system.eventhandler(this.button3_click);
//
// button2
//
this.button2.anchor = system.windows.forms.anchorstyles.left;
this.button2.location = new system.drawing.point(16, 32);
this.button2.name = "button2";
this.button2.tabindex = 0;
this.button2.text = "导入";
this.button2.click += new system.eventhandler(this.button2_click);
//
// button4
//
this.button4.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
this.button4.location = new system.drawing.point(128, 328);
this.button4.name = "button4";
this.button4.size = new system.drawing.size(96, 23);
this.button4.tabindex = 9;
this.button4.text = "添加联系人";
this.button4.click += new system.eventhandler(this.button4_click);
//
// button5
//
this.button5.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
this.button5.dialogresult = system.windows.forms.dialogresult.cancel;
this.button5.location = new system.drawing.point(232, 328);
this.button5.name = "button5";
this.button5.size = new system.drawing.size(96, 23);
this.button5.tabindex = 10;
this.button5.text = "修改联系人";
this.button5.click += new system.eventhandler(this.button5_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(336, 328);
this.button6.name = "button6";
this.button6.size = new system.drawing.size(96, 23);
this.button6.tabindex = 11;
this.button6.text = "删除联系人";
this.button6.click += new system.eventhandler(this.button6_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(440, 373);
this.controls.add(this.button6);
this.controls.add(this.button5);
this.controls.add(this.button4);
this.controls.add(this.groupbox1);
this.controls.add(this.textbox2);
this.controls.add(this.listbox1);
this.controls.add(this.label2);
this.controls.add(this.button1);
this.controls.add(this.radiobutton2);
this.controls.add(this.radiobutton1);
this.controls.add(this.textbox1);
this.controls.add(this.label1);
this.name = "form1";
this.text = "联系人应用程序";
this.load += new system.eventhandler(this.form1_load);
this.groupbox1.resumelayout(false);
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void form1_load(object sender, system.eventargs e)
{
contact contact = new contact();
this.bindtolistbox(contact);
}
private void bindtolistbox(contact contact)
{
this.listbox1.items.clear();
arraylist namelist;
try
{
namelist = contact.loadcontactname();
foreach(object obj in namelist)
{
this.listbox1.items.add(obj.tostring());
}
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
contact.dispose();
}
}
private void listbox1_selectedindexchanged(object sender, system.eventargs e)
{
contact contact = new contact();
string itemtext = this.listbox1.getitemtext(listbox1.selecteditem);
string firstname = itemtext.split(" ".tochararray())[1].trim().tostring();
string lastname = itemtext.split(" ".tochararray())[0].trim().tostring();
contact.firstname = firstname;
contact.lastname = lastname;
try
{
this.textbox2.text = contact.displaynote();
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
contact.dispose();
}
}
private void button1_click(object sender, system.eventargs e)
{
string searchtype = string.empty;
string searchvalue = string.empty;
contact contact = new contact();
if (this.radiobutton1.checked == true)
searchtype = "first";
if (this.radiobutton2.checked == true)
searchtype = "last";
if (this.textbox1.text != "")
searchvalue = this.textbox1.text;
try
{
contact.searchcontact(searchtype,searchvalue);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
contact.dispose();
}
}
private void button4_click(object sender, system.eventargs e)
{
string firstname = string.empty;
string lastname = string.empty;
string note = string.empty;
form2 frm2 = new form2();
frm2.showdialog();
if (frm2.dialogresult == dialogresult.ok)
{
firstname = frm2.controls[0].text;
lastname = frm2.controls[1].text;
note = frm2.controls[2].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 button5_click(object sender, system.eventargs e)
{
if (this.listbox1.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.listbox1.getitemtext(listbox1.selecteditem);
firstname = itemtext.split(" ".tochararray())[1].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);
}
form3 frm3 = new form3();
frm3.controls[0].text = firstname;
frm3.controls[1].text = lastname;
frm3.controls[2].text = note;
frm3.showdialog();
if (frm3.dialogresult == dialogresult.ok)
{
firstname = frm3.controls[0].text;
lastname = frm3.controls[1].text;
note = frm3.controls[2].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.listbox1.selecteditem == null)
{
messagebox.show("要删除联系人信息请先在联系人列表中选中","删除联系人",messageboxbuttons.ok,messageboxicon.asterisk);
return;
}
string firstname = string.empty;
string lastname = string.empty;
string itemtext = string.empty;
itemtext = this.listbox1.getitemtext(listbox1.selecteditem);
firstname = itemtext.split(" ".tochararray())[1].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 button3_click(object sender, system.eventargs e)
{
string filepath = this.textbox3.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 button2_click(object sender, system.eventargs e)
{
if (this.textbox3.text == "")
return;
string filepath = this.textbox3.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>
/// form2 的摘要说明。
/// </summary>
public class form2 : system.windows.forms.form
{
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.textbox textbox1;
private system.windows.forms.textbox textbox2;
private system.windows.forms.textbox textbox3;
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.label label3;
private system.windows.forms.label label4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form2()
{
//
// 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.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.textbox1 = new system.windows.forms.textbox();
this.textbox2 = new system.windows.forms.textbox();
this.textbox3 = new system.windows.forms.textbox();
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.label3 = new system.windows.forms.label();
this.label4 = new system.windows.forms.label();
this.suspendlayout();
//
// button1
//
this.button1.dialogresult = system.windows.forms.dialogresult.ok;
this.button1.location = new system.drawing.point(216, 152);
this.button1.name = "button1";
this.button1.tabindex = 0;
this.button1.text = "确定";
//
// button2
//
this.button2.dialogresult = system.windows.forms.dialogresult.cancel;
this.button2.location = new system.drawing.point(296, 152);
this.button2.name = "button2";
this.button2.tabindex = 1;
this.button2.text = "取消";
//
// textbox1
//
this.textbox1.location = new system.drawing.point(136, 16);
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(232, 21);
this.textbox1.tabindex = 2;
this.textbox1.text = "";
//
// textbox2
//
this.textbox2.location = new system.drawing.point(136, 56);
this.textbox2.name = "textbox2";
this.textbox2.size = new system.drawing.size(232, 21);
this.textbox2.tabindex = 3;
this.textbox2.text = "";
//
// textbox3
//
this.textbox3.location = new system.drawing.point(136, 96);
this.textbox3.name = "textbox3";
this.textbox3.size = new system.drawing.size(232, 21);
this.textbox3.tabindex = 4;
this.textbox3.text = "";
//
// label1
//
this.label1.location = new system.drawing.point(16, 16);
this.label1.name = "label1";
this.label1.tabindex = 5;
this.label1.text = "姓氏:";
this.label1.textalign = system.drawing.contentalignment.middlecenter;
//
// label2
//
this.label2.location = new system.drawing.point(16, 56);
this.label2.name = "label2";
this.label2.tabindex = 6;
this.label2.text = "名字:";
this.label2.textalign = system.drawing.contentalignment.middlecenter;
//
// label3
//
this.label3.location = new system.drawing.point(16, 96);
this.label3.name = "label3";
this.label3.tabindex = 7;
this.label3.text = "个人信息:";
this.label3.textalign = system.drawing.contentalignment.middlecenter;
//
// label4
//
this.label4.borderstyle = system.windows.forms.borderstyle.fixedsingle;
this.label4.location = new system.drawing.point(0, 136);
this.label4.name = "label4";
this.label4.size = new system.drawing.size(384, 1);
this.label4.tabindex = 8;
//
// form2
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(378, 183);
this.controls.add(this.label4);
this.controls.add(this.label3);
this.controls.add(this.label2);
this.controls.add(this.label1);
this.controls.add(this.textbox3);
this.controls.add(this.textbox2);
this.controls.add(this.textbox1);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.formborderstyle = system.windows.forms.formborderstyle.fixeddialog;
this.maximizebox = false;
this.name = "form2";
this.startposition = system.windows.forms.formstartposition.centerscreen;
this.text = "添加联系人";
this.load += new system.eventhandler(this.form2_load);
this.resumelayout(false);
this.controls.setchildindex(this.textbox1,0);
this.controls.setchildindex(this.textbox2,1);
this.controls.setchildindex(this.textbox3,2);
}
#endregion
private void form2_load(object sender, system.eventargs e)
{
}
}
}
修改联系人窗体如下:
namespace contactapplication
{
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
/// <summary>
/// form3 的摘要说明。
/// </summary>
public class form3 : system.windows.forms.form
{
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.label label3;
private system.windows.forms.label label4;
private system.windows.forms.textbox textbox1;
private system.windows.forms.textbox textbox2;
private system.windows.forms.textbox textbox3;
private system.windows.forms.button button1;
private system.windows.forms.button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form3()
{
//
// 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.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.label3 = new system.windows.forms.label();
this.label4 = new system.windows.forms.label();
this.textbox1 = new system.windows.forms.textbox();
this.textbox2 = new system.windows.forms.textbox();
this.textbox3 = new system.windows.forms.textbox();
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.suspendlayout();
//
// label1
//
this.label1.location = new system.drawing.point(16, 16);
this.label1.name = "label1";
this.label1.tabindex = 0;
this.label1.text = "姓氏:";
this.label1.textalign = system.drawing.contentalignment.middlecenter;
//
// label2
//
this.label2.location = new system.drawing.point(16, 56);
this.label2.name = "label2";
this.label2.tabindex = 1;
this.label2.text = "名字:";
this.label2.textalign = system.drawing.contentalignment.middlecenter;
//
// label3
//
this.label3.location = new system.drawing.point(16, 96);
this.label3.name = "label3";
this.label3.tabindex = 2;
this.label3.text = "个人信息:";
this.label3.textalign = system.drawing.contentalignment.middlecenter;
//
// label4
//
this.label4.borderstyle = system.windows.forms.borderstyle.fixedsingle;
this.label4.location = new system.drawing.point(0, 128);
this.label4.name = "label4";
this.label4.size = new system.drawing.size(384, 1);
this.label4.tabindex = 3;
//
// textbox1
//
this.textbox1.location = new system.drawing.point(136, 16);
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(232, 21);
this.textbox1.tabindex = 4;
this.textbox1.text = "";
//
// textbox2
//
this.textbox2.location = new system.drawing.point(136, 56);
this.textbox2.name = "textbox2";
this.textbox2.size = new system.drawing.size(232, 21);
this.textbox2.tabindex = 5;
this.textbox2.text = "";
//
// textbox3
//
this.textbox3.location = new system.drawing.point(136, 96);
this.textbox3.name = "textbox3";
this.textbox3.size = new system.drawing.size(232, 21);
this.textbox3.tabindex = 6;
this.textbox3.text = "";
//
// button1
//
this.button1.dialogresult = system.windows.forms.dialogresult.ok;
this.button1.location = new system.drawing.point(216, 144);
this.button1.name = "button1";
this.button1.tabindex = 7;
this.button1.text = "确定";
//
// button2
//
this.button2.dialogresult = system.windows.forms.dialogresult.cancel;
this.button2.location = new system.drawing.point(296, 144);
this.button2.name = "button2";
this.button2.tabindex = 8;
this.button2.text = "取消";
//
// form3
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(378, 175);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.controls.add(this.textbox3);
this.controls.add(this.textbox2);
this.controls.add(this.textbox1);
this.controls.add(this.label4);
this.controls.add(this.label3);
this.controls.add(this.label2);
this.controls.add(this.label1);
this.formborderstyle = system.windows.forms.formborderstyle.fixeddialog;
this.name = "form3";
this.startposition = system.windows.forms.formstartposition.centerscreen;
this.text = "修改联系人";
this.load += new system.eventhandler(this.form3_load);
this.resumelayout(false);
this.controls.setchildindex(this.textbox1,0);
this.controls.setchildindex(this.textbox2,1);
this.controls.setchildindex(this.textbox3,2);
}
#endregion
private void form3_load(object sender, system.eventargs e)
{
}
}
}