The purpose of writing this case is to describe how to use DOM technology in a normal application and the relevant knowledge of the DOM described in the previous article "XML - implementing DOM" in the previous article, this case will analyze one Contact Application, use the XML document as a database, all contact information is stored in an XML document, while using DOM in the program to query, edit, update, etc. Specifically, this case will achieve the following features:
1. Add a new contact
2. Modify existing contacts
3. Delete an existing contact
4. Survey contact
5. Press the name query contact
6. Export all contacts to another XML file
7. Import contacts from another XML file
The following is the program running renderings:
Application main form:
Add Contact Form:
Modify the contact form:
The following is an XML file for test programs:
Contact.xml Save the file in the project directory
XML Version = "1.0" encoding = "GB2312"?>
name>
Contact>
name>
Contact>
name>
Contact>
ContactDetails>
Contact2.xml This file is used to implement the import contact function, save the file casually in a directory and then copy the save path together to the "saved path" text box of the file name to the main form, then click "Import" Press Import feature can be implemented in the new web.
XML Version = "1.0" encoding = "GB2312"?>
name>
Contact>
name>
name>
Contact>
name>
Contact>
name>
Contact>
ContactDetails>
When you export your contact, enter a file path in the Save Path text box, and the program will create an XML file in the path. If the file exists on the path, the program will rewrite the XML file.
In order to achieve all the features described above, I specifically prepare a class to encapsulate implementation code, which is as follows:
Namespace ContactApplication
{
Using system;
USING SYSTEM.XML;
Using system.text;
Using system.data;
Using system.windows.forms;
Using system.componentmodel;
Using system.collections;
///
///Undact contact
/// summary>
Public Class Contact: IDisposable
{
Private string xmlpath;
Private xmldocument Xmldoc;
Private XMLnode SelectNode;
Private string dimstname;
PRIVATE STRING LASTNAME;
PRIVATE STRING NOTE;
#Region Contact constructor
///
/// Default constructor
/// 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;
}
///
/// Construct a contact object using the last name, name, personal information
/// summary>
/// last name param>
/// Name param>
/// Personal information param>
Public Contact (String firstname, 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;
}
#ndregion
#Region Contact Resource Release Method
///
/// Clean all the resources being used by this object
/// summary>
Public void dispose ()
{
THIS.DISPOSE (TRUE);
Gc.suppressFinalize (this);
}
///
/// Release the instance variable of the object
/// summary>
/// 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;
}
#ndregion
#Region Contact property
///
/// surname
/// summary>
Public String Firstname
{
get
{
Return this.firstname;
}
set
{
THIS.FIRSTNAME = VALUE;
}
}
///
/// first name
/// summary>
Public String Lastname
{
get
{
Return this.lastname;
}
set
{
THIS.LASTNAME = Value;}
}
///
/// Personal information
/// summary>
Public String Note
{
get
{
Return this.note;
}
set
{
THIS.NOTE = VALUE;
}
}
#ndregion
#REGON Contact function function
///
/// Load all contact names
/// summary>
///
Public arraylist loading ()
{
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;
}
///
/// Get the text value of the Note node
/// summary>
///
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;
}
///
/// Search contact
/// summary>
///
/ / / According to the incoming search type (in list) and the search value query the qualified contact information, it is displayed in the form of a dialog.
/// remarks>
/// Search type (first or last) param>
/// Search value param>
Public void searchContact (String searchType, String Searchvalue)
{
String contactinfo = string.empty;
INT i = 0;
Xmlnodelist namenodelist = xmldoc.selectnodes ("// name [{0} = '{1}']", searchtype, searchvalue); if (searchtype == "first")
{
Messagebox.show (String.Format "contact with {1}", SearchValue, NameNodelist.count, "Search Contact", 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): {1} {2} {3} ", i, system.environment.newline, system.environment.newline, contactinfo," Search contact " , MessageBoxButtons.ok, MessageBoxicon.information;
}
}
Else IF (searchtype == "last")
{
Messagebox.show (String.Format "contact with {0} name has {1}", SearchValue, NameNodelist.count, "Search Contact", 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): {1} {2} {3} ", i, system.environment.newline, system.environment.newline, contactinfo," Search contact " , MessageBoxButtons.ok, MessageBoxicon.information;
}
Else
{
Messagebox.show ("No items that match your search criteria, check if your operation is correct!", Search contact, MessageBoxButtons.ok, MessageBoxicon.information;
}
}
///
/// add contact
/// summary>
Public void addcontact ()
{
XMLDocumentFragment Xmldocfrag = xmldoc.createdocumentfragment ();
Xmlelement ContactEle = XmLDoc.createElement ("Contact");
Xmlelement namele = xmldoc.createElement ("name");
Xmlelement firstnamele = xmldoc.createElement ("first");
Firstnamele.innertext = this.firstname;
NameEle.Appendchild (firstnamele);
Xmlelement lastnamele = Xmldoc.createElement ("Last");
Lastnamele.innertext = this.lastname;
NameEle.Appendchild (Lastnamele);
XmLelement Notele = XmLDoc.createElement ("Note");
Notele.innertext = this.note;
Contactele.Appendchild (namele);
Contactele.Appendchild (Notele);
XMLDocFrag.Appendchild (Contactele);
Xmlelement Detailsele = (XMLELEMENT) XmLDoc.selectsinglenode ("/ ContactDetails);
Detailsele.Appendchild (xmldocfrag.firstchil);
XMLDoc.save (this.xmlpath);
}
///
// / Modify the contact
/// summary>
Public void updatecontact ()
{
SelectNode.firstchild.innertext = this.firstname;
Selectnode.lastchild.innertext = this.lastname;
Selectnode.nextsibling.innertext = this.note;
XMLDoc.save (this.xmlpath);
}
///
/// Delete contact
/// summary>
Public void deletecontact ()
{
Xmlelement ContactEle = (Xmlelement) this.selectNode.ParentNode;
XMLELEMENT DETAILSELE = (XMLELEMENT) Contactele.ParentNode;
Detailsele.RemoveChild (Contactele);
XMLDoc.save (this.xmlpath);
}
///
/ / Select a corresponding contact in the document according to the contacts selected in the list box
/// summary>
Public void selectcontact ()
{
This.selectNode = xmldoc.selectsinglenode (String.Format ("// name [first = '{0}' and last = '{1}']", this.firstname, this.lastname));
}
///
/// Export contact
/// summary>
/// The path to export param>
///
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;
}
///
/// Import contact
/// summary>
/// The path to import 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 ("//undact");
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 ("/ contactdtails"). Appendchild (ImportNode);
}
XMLDoc.save (this.xmlpath);
}
#ndregion
}
}
The program main form code is as follows:
Namespace ContactApplication
{
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Using system.data;
///
/// Form1 summary description.
/// 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 button;
///
/// The required designer variable.
/// summary>
Private system.componentmodel.Container Components = NULL;
Public Form1 ()
{
//
// Windows Form Designer Support
//
InitializationComponent ();
//
// Todo: Add any constructor code after INITIALIZECOMPONENT call
//
}
///
/// Clean all the resources being used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// Designer supports the required method - Do not use the code editor to modify the // / this method.
/// summary>
Private vidinitiRizeComponent ()
{
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 ();
THISPBOX1.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 = "Search contacts:";
//
// 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 = "Search by" "
//
// 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 = "Search by name";
//
// 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 = "Find contacts";
This.Button1.click = new system.eventhandler (this.button1_click);
//
// label2
//
THISTEM.WINDOWS.FORMS.ANCHORSTYLES.TOP | System.Windows.Forms.Anchorstyles.Windows.Forms.Anchbook.windows.form |
| 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 = "Contact List:";
//
// ListBox1
//
THISTBOX1.Anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.top | System.Windows.Forms.Anchorstyles.Left)
| System.windows.Forms.Anchorstyles.right)))))))))))))))))))))))
THISTBOX1.ItemHeight = 12;
This.listbox1.location = new system.drawing.point (152, 40);
this.listbox1.name = "listbox1";
This.ListBox1.scrollal gatesvisible = true;
This.listbox1.size = new system.drawing.size (288, 64); this.listbox1.tabindex = 6;
This.ListBox1.Selected IndexChanged = New System.EventHandler (this.listbox1_selected);
//
// 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
//
THISTEM.WINDOWS.FORMS.ANCHORSTYLES.BOTTOM | System.Windows.Forms.Anchorstyles.Windows.Forms.Anchorstyles.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 = "Export / Import Contact";
//
// TextBox3
//
This.TextBox3.anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.left | system.windows.forms.AnchorseLes.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
//
THISTEM.WINDOWS.FORMS.ANCHORSTYLES (SYSTEM.WINDOWS.FORMS.ANCHORSTYLES.LINDOWS.FORMS.ANCHORSTYLES.LORS.WINDOWS.WINDOWS.FORS.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 = "Save the path";
//
// Button3
//
This.Button3.Anchor = system.windows.forms.Anchorstyles.Lorms.Anchorstyles.left;
This.Button3.Location = new system.drawing.point (104, 32);
This.button3.name = "button3";
this.button3.tabindex = 1;
This.Button3.Text = "Export";
This.Button3.click = new system.eventhandler (this.button3_click);
//
// Button2
//
This.Button2.Anchor = system.windows.forms.Anchorstyles.Lorms.Anchorstyles.left;
This.Button2.Location = new system.drawing.point (16, 32);
This.Button2.name = "button2";
this.button2.tabindex = 0;
This.Button2.Text = "Import";
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 = "Add Contact";
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 = "Modify Contact";
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 = "Delete Contact";
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 = "Contact Application";
This.Load = New System.EventHandler (this.form1_load);
THIS.GroupBox1.ResumeLayout (false); this.ResumeLayout (false);
}
#ndregion
///
/// The main entry point for the application.
/// 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.toT7tring (), "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.selectedited);
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.toT7tring (), "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 ();
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.toT7tring (), "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.toT7tring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);
}
}
}
Private void button5_click (Object Sender, System.Eventargs E)
{
IF (this.listbox1.selecteditem == NULL)
{
MessageBox.show ("To modify contact information, please select", "Modify Contact", MessageBoxButton, and 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.toT7tring (), "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.toT7tring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);
}
}
}
Private void button6_click (Object Sender, System.Eventargs E)
{
IF (this.listbox1.selecteditem == NULL)
{
Messagebox.show ("To delete contact information, please select", "Delete Contact", 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 ("Whether to determine whether to delete contacts, will not be recovered after delete!", "Delete Contact", 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), "Export Contact", MessageBoxButtons.ok, MessageBoxicon.AnTerisk;
}
Catch (Exception Exp)
{
Messagebox.show (Exp.toT7tring (), "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.toT7tring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);
}
}
}
}
Add a contact form code as follows:
Namespace ContactApplication
{
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
///
/// Form2's summary description.
/// 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;
///
/// The required designer variable.
/// summary>
Private system.componentmodel.Container Components = NULL; Public Form2 ()
{
//
// Windows Form Designer Support
//
InitializationComponent ();
//
// Todo: Add any constructor code after INITIALIZECOMPONENT call
//
}
///
/// Clean all the resources being used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
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 = "OK";
//
// 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 = "Cancel";
//
// 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 = "last name:";
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 = "Name:";
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 = "Personal Information:";
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 = "Add Contact";
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);
}
#ndregion
Private Void Form2_Load (Object Sender, System.EventArgs E)
{
}
}
}
Modify the contact form as follows:
Namespace ContactApplication
{
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
///
/// Form3 summary description.
/// 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;
///
/// The required designer variable.
/// summary>
Private system.componentmodel.Container Components = NULL;
Public Form3 ()
{
//
// Windows Form Designer Support
//
InitializationComponent ();
//
// Todo: Add any constructor code after INITIALIZECOMPONENT call
//
}
///
/// Clean all the resources being used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
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 = "last name:";
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 = "Name:";
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 = "Personal Information:";
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 = "OK";
//
// 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 = "Cancel";
//
// 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 = "Modify Contact";
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);
}
#ndregion
Private Void Form3_Load (Object Sender, System.Eventargs E)
{
}
}
}