編寫此案例的目的是為了描述在普通的應用程序中如何運用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)
{
}
}
}