国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > XML > 正文

C#中使用XML——基于DOM的案例分析

2024-09-05 20:55:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
編寫此案例的目的是為了描述在普通的應(yīng)用程序中如何運(yùn)用dom技術(shù)以及對(duì)上一篇文章《c#中使用xml——實(shí)現(xiàn)dom》中所講述的dom的相關(guān)知識(shí)回顧一下,本案例將分析一個(gè)聯(lián)系人應(yīng)用程序,在這里將xml文檔充當(dāng)數(shù)據(jù)庫(kù)來(lái)使用, 所有的聯(lián)系人信息存儲(chǔ)在xml文檔中,同時(shí),在程序中使用dom對(duì)聯(lián)系人文檔進(jìn)行查詢、編輯、更新等操作。具體來(lái)說(shuō)本案例將實(shí)現(xiàn)以下功能:

1. 添加一個(gè)新的聯(lián)系人

2. 修改現(xiàn)有聯(lián)系人

3. 刪除現(xiàn)有聯(lián)系人

4. 按姓氏查詢聯(lián)系人

5. 按名字查詢聯(lián)系人

6. 將所有聯(lián)系人導(dǎo)出到另一個(gè)xml文件

7. 將聯(lián)系人從另一個(gè)xml文件導(dǎo)入

以下是程序運(yùn)行效果圖:

應(yīng)用程序主窗體:




添加聯(lián)系人窗體:




修改聯(lián)系人窗體:



以下是用于測(cè)試程序的xml文件:

contact.xml 將該文件保存在項(xiàng)目目錄下

<?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 該文件用于實(shí)現(xiàn)導(dǎo)入聯(lián)系人功能,將該文件隨便保存在一個(gè)目錄下然后將保存路徑連同文件名拷貝到主窗體的“保存的路徑”文本框中再單擊“導(dǎo)入”按紐即可實(shí)現(xiàn)導(dǎo)入功能。

<?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>香港著名藝人,工作勤懇同時(shí)不忘生活,出演電影100多部,演技已達(dá)登峰造極,刻畫人物栩栩如生</note>

</contact>

<contact>

<name>

<first>揚(yáng)</first>

<last>震</last>

</name>

<note>重案六組探員,為人膽大心細(xì),沉著冷靜,富有人情味,經(jīng)歷幾次案件后更加成熟,在成長(zhǎng)中不斷磨練,是個(gè)真的漢子,正應(yīng)驗(yàn)?zāi)蔷湓挘撼删涂空姹臼?lt;/note>

</contact>

<contact>

<name>

<first>季</first>

<last>潔</last>

</name>

<note>重案六組探員,富有人情味,對(duì)揚(yáng)震早已芳心默許,知道為什么嗎?因?yàn)樗焐蛺?ài)保護(hù)別人,當(dāng)她看到揚(yáng)震被別人用槍指著頭嚇的回不過(guò)神來(lái)時(shí)就對(duì)這個(gè)真實(shí)的男人產(chǎn)生了感覺(jué),真可謂巾幗不讓須眉</note>

</contact>

</contactdetails>



導(dǎo)出聯(lián)系人時(shí)在“保存的路徑”文本框中輸入一個(gè)文件路徑,程序?qū)⒃谠撀窂较聞?chuàng)建一個(gè)xml文件,如果該文件存在于該路徑上,程序?qū)?duì)該xml文件進(jìn)行重寫。



為實(shí)現(xiàn)以上所述所有功能,我專門編寫了一個(gè)類來(lái)封裝實(shí)現(xiàn)代碼,該類代碼如下:

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 聯(lián)系人

/// </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 構(gòu)造器



/// <summary>

/// 默認(rèn)構(gòu)造器

/// </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>

/// 使用姓氏,名字,個(gè)人信息構(gòu)造一個(gè)聯(lián)系人對(duì)象

/// </summary>

/// <param name="firstname">姓氏</param>

/// <param name="lastname">名字</param>

/// <param name="note">個(gè)人信息</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>

/// 清理該對(duì)象所有正在使用的資源

/// </summary>

public void dispose()

{

this.dispose(true);

gc.suppressfinalize(this);

}



/// <summary>

/// 釋放該對(duì)象的實(shí)例變量

/// </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>

/// 個(gè)人信息

/// </summary>

public string note

{

get

{

return this.note;

}

set

{

this.note = value;

}

}



#endregion



#region contact 功能函數(shù)



/// <summary>

/// 加載所有的聯(lián)系人姓名

/// </summary>

/// <returns>聯(lián)系人姓名列表</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節(jié)點(diǎn)的文本值

/// </summary>

/// <returns>note節(jié)點(diǎn)的文本值</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>

/// 搜索聯(lián)系人

/// </summary>

/// <remarks>

/// 根據(jù)傳入的搜索類型(按姓或名)以及搜索值查詢符合條件的聯(lián)系人信息,并以對(duì)話框形式顯示

/// </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}姓氏的聯(lián)系人有{1}個(gè)",searchvalue,namenodelist.count),"搜索聯(lián)系人",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}個(gè)聯(lián)系人:{1}{2}{3}",i,system.environment.newline,system.environment.newline,contactinfo),"搜索聯(lián)系人",messageboxbuttons.ok,messageboxicon.information);

}

}

else if(searchtype == "last")

{

messagebox.show(string.format("符合{0}名字的聯(lián)系人有{1}個(gè)",searchvalue,namenodelist.count),"搜索聯(lián)系人",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}個(gè)聯(lián)系人:{1}{2}{3}",i,system.environment.newline,system.environment.newline,contactinfo),"搜索聯(lián)系人",messageboxbuttons.ok,messageboxicon.information);

}

}

else

{

messagebox.show("沒(méi)有發(fā)現(xiàn)與您的搜索條件匹配的項(xiàng),請(qǐng)檢查您的操作是否正確!","搜索聯(lián)系人",messageboxbuttons.ok,messageboxicon.information);

}

}



/// <summary>

/// 添加聯(lián)系人

/// </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>

/// 修改聯(lián)系人

/// </summary>

public void updatecontact()

{

selectnode.firstchild.innertext = this.firstname;

selectnode.lastchild.innertext = this.lastname;

selectnode.nextsibling.innertext = this.note;



xmldoc.save(this.xmlpath);

}



/// <summary>

/// 刪除聯(lián)系人

/// </summary>

public void deletecontact()

{

xmlelement contactele = (xmlelement)this.selectnode.parentnode;

xmlelement detailsele = (xmlelement)contactele.parentnode;



detailsele.removechild(contactele);

xmldoc.save(this.xmlpath);

}



/// <summary>

/// 根據(jù)列表框中選中的聯(lián)系人在文檔中選擇一個(gè)對(duì)應(yīng)的聯(lián)系人

/// </summary>

public void selectcontact()

{

this.selectnode = xmldoc.selectsinglenode(string.format("//name[first='{0}' and last='{1}']",this.firstname,this.lastname));

}



/// <summary>

/// 導(dǎo)出聯(lián)系人

/// </summary>

/// <param name="filepath">要導(dǎo)出的路徑</param>

/// <returns>是否成功導(dǎo)出</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>

/// 導(dǎo)入聯(lián)系人

/// </summary>

/// <param name="filepath">要導(dǎo)入的路徑</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 的摘要說(shuō)明。

/// </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>

/// 必需的設(shè)計(jì)器變量。

/// </summary>

private system.componentmodel.container components = null;



public form1()

{

//

// windows 窗體設(shè)計(jì)器支持所必需的

//

initializecomponent();



//

// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼

//

}



/// <summary>

/// 清理所有正在使用的資源。

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}



#region windows 窗體設(shè)計(jì)器生成的代碼

/// <summary>

/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內(nèi)容。

/// </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 = "搜索聯(lián)系人:";

//

// 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 = "查找聯(lián)系人";

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 = "聯(lián)系人列表:";

//

// 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 = "導(dǎo)出/導(dǎo)入聯(lián)系人";

//

// 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 = "導(dǎo)出";

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 = "導(dǎo)入";

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 = "添加聯(lián)系人";

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 = "修改聯(lián)系人";

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 = "刪除聯(lián)系人";

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 = "聯(lián)系人應(yīng)用程序";

this.load += new system.eventhandler(this.form1_load);

this.groupbox1.resumelayout(false);

this.resumelayout(false);



}

#endregion



/// <summary>

/// 應(yīng)用程序的主入口點(diǎn)。

/// </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("要修改聯(lián)系人信息請(qǐng)先在聯(lián)系人列表中選中","修改聯(lián)系人",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("要?jiǎng)h除聯(lián)系人信息請(qǐng)先在聯(lián)系人列表中選中","刪除聯(lián)系人",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("是否確定刪除聯(lián)系人,刪除后將不可恢復(fù)!","刪除聯(lián)系人",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),"導(dǎo)出聯(lián)系人",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);

}

}

}

}

添加聯(lián)系人窗體代碼如下:

namespace contactapplication

{

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;



/// <summary>

/// form2 的摘要說(shuō)明。

/// </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>

/// 必需的設(shè)計(jì)器變量。

/// </summary>

private system.componentmodel.container components = null;



public form2()

{

//

// windows 窗體設(shè)計(jì)器支持所必需的

//

initializecomponent();



//

// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼

//

}



/// <summary>

/// 清理所有正在使用的資源。

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}



#region windows 窗體設(shè)計(jì)器生成的代碼

/// <summary>

/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內(nèi)容。

/// </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 = "個(gè)人信息:";

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 = "添加聯(lián)系人";

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)

{



}

}

}

修改聯(lián)系人窗體如下:

namespace contactapplication

{

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;



/// <summary>

/// form3 的摘要說(shuō)明。

/// </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>

/// 必需的設(shè)計(jì)器變量。

/// </summary>

private system.componentmodel.container components = null;



public form3()

{

//

// windows 窗體設(shè)計(jì)器支持所必需的

//

initializecomponent();



//

// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼

//

}



/// <summary>

/// 清理所有正在使用的資源。

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}



#region windows 窗體設(shè)計(jì)器生成的代碼

/// <summary>

/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內(nèi)容。

/// </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 = "個(gè)人信息:";

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 = "修改聯(lián)系人";

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)

{



}

}

}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桃园县| 青神县| 丹阳市| 北宁市| 荃湾区| 饶阳县| 固安县| 汉寿县| 伊川县| 黄平县| 通渭县| 东兰县| 芦山县| 大同县| 绥阳县| 饶河县| 黄石市| 金阳县| 秦皇岛市| 十堰市| 伊金霍洛旗| 汨罗市| 榆林市| 四子王旗| 潜江市| 平乐县| 云梦县| 东源县| 温州市| 济宁市| 怀仁县| 古浪县| 嘉禾县| 丰城市| 弥渡县| 文水县| 潜山县| 莆田市| 凌海市| 浑源县| 洱源县|