PDA

View Full Version : Lưu trạng thái cho ứng dụng sử dụng XML



vanlan
30-04-2006, 17:02
Có rất nhiều cách để lưu trạng thái của ứng dụng nhưng cách phổ biển và dể dùng nhất là XML. Bài viết này sẻ hướng dẩn cho bạn làm thế nào để sử dụng XML để lưu lại trạng thái của ứng dụng
Với ứng dụng này bạn có thể kéo giản, dời vị trí, thêm item, sau đó bạn thoát khỏi ứng dụng và vào lại các trạng thại vẩn còn dử nguyên.


screenhot:
http://img146.imageshack.us/img146/768/untitled27zt.gif
data.xml lưu vào thư mục Bin\Debug của ứng dụng


<?xml version="1.0" encoding="utf-8"?>
<data>
<width>273</width>
<height>324</height>
<top>128</top>
<left>356</left>
<items>
<item>a</item>
<item>b</item>
<item>c</item>
</items>
</data>

code Form1.cs


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;


namespace XMLApplication
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//khởi tạo đối tượng XmlDoc
XmlDocument xmlDoc = new XmlDocument();

private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any 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
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Wind ows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(8, 224);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(112, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windo ws.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(128, 224);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Thêm";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Win dows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox1.Location = new System.Drawing.Point(8, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(280, 212);
this.listBox1.TabIndex = 2;
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windo ws.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(216, 224);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "Xóa";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(296, 253);
this.Controls.Add(this.button2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form 1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}

private void button2_Click(object sender, System.EventArgs e)
{
if(listBox1.SelectedIndex != -1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
//load dử liều
xmlDoc.Load("data.xml");

//gán dử liệu
this.Top = int.Parse(xmlDoc.DocumentElement["top"].InnerText);
this.Left = int.Parse(xmlDoc.DocumentElement["left"].InnerText);
this.Width = int.Parse(xmlDoc.DocumentElement["width"].InnerText);
this.Height = int.Parse(xmlDoc.DocumentElement["height"].InnerText);

for(int i=0; i<xmlDoc.DocumentElement["items"].ChildNodes.Count;i++)
{
listBox1.Items.Add(xmlDoc.DocumentElement["items"].ChildNodes[i].InnerText);
}
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//gán dử liệu
xmlDoc.DocumentElement["top"].InnerText = this.Top.ToString();
xmlDoc.DocumentElement["left"].InnerText = this.Left.ToString();
xmlDoc.DocumentElement["width"].InnerText = this.Width.ToString();
xmlDoc.DocumentElement["height"].InnerText = this.Height.ToString();
xmlDoc.DocumentElement["items"].RemoveAll();

for(int i=0; i<listBox1.Items.Count;i++)
{
XmlElement xmlElement = xmlDoc.CreateElement("item");
xmlElement.InnerText = listBox1.Items[i].ToString();
xmlDoc.DocumentElement["items"].AppendChild(xmlElement);
}

// lưu dử liệu
xmlDoc.Save("data.xml");
}

}
}


download code :
http://www.4shared.com/file/1446808/c62d5613/xmlapplication.html

nguoivodanh1003
01-05-2006, 23:25
Cám ơn bạn vanlan, cái này tui biết rồi nhưng tui thích cách chia sẽ nhưng vần đề mình thấy hay với người khác, có thể nó sẽ giúp những người khác rất nhiều. Cám ơn.