編寫此案例的目的是為了描述在普通的應用程序中如何運用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;
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)
{
<br> 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;
{
> 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 first
Name = 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; <br>
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()
{
//
ew 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); r>
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.Wi
ndows.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)
{
}
}
}