thực hành c#
Bài Thực Hành C#
------- TreeView--------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace TestTreeView
{
public partial class TestTreeView2 : Form
{
public TestTreeView2()
{
InitializeComponent();
}
private void TestTreeView2_Load(object sender, EventArgs e)
{
cbNode.SelectedIndex = 0;
if (!File.Exists("Test.xml"))
{
FileStream fs;
fs = new FileStream("Test.xml", FileMode.Create);
StreamWriter sWriter = new StreamWriter(fs, Encoding.UTF8);
sWriter.WriteLine("<?xml version='1.0' encoding='utf-8'?><TreeView></TreeView>");
sWriter.Flush();
fs.Close();
}
else
{
tvNode.Nodes.Clear();
string path = Application.StartupPath + "\\Test.xml";
SaveLoadFile serializer = new SaveLoadFile();
serializer.LoadFile(tvNode, path);
if (tvNode.Nodes.Count > 0)
{
btXoa.Enabled = true;
}
}
}
private void tvNode_DoubleClick(object sender, EventArgs e)
{
tvNode.SelectedNode.BeginEdit();
}
private void btThem_Click(object sender, EventArgs e)
{
try
{
if (tbNhap.Text != "")
{
if (cbNode.SelectedIndex == 0)
{
TreeView treeNode = new TreeView();
treeNode.Text = tbNhap.Text;
tvNode.Nodes.Add(treeNode.Text);
btXoa.Enabled = true;
}
else
{
TreeView treeNode = new TreeView();
treeNode.Text = tbNhap.Text;
if (tvNode.Nodes.Count == 0)
MessageBox.Show("Chưa có node cha, hãy thêm node cha trước");
else if (tvNode.SelectedNode == null)
MessageBox.Show("Bạn chưa chọn node cha cần thêm");
else
{
tvNode.SelectedNode.Nodes.Add(treeNode.Text);
tvNode.SelectedNode.ExpandAll();
tbNhap.Text = "";
}
}
}
tbNhap.Text = "";
Save();
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}
private void btXoa_Click(object sender, EventArgs e)
{
tvNode.SelectedNode.Remove();
if (tvNode.Nodes.Count == 0)
{
btXoa.Enabled = false;
}
Save();
}
private void tbNhap_TextChanged(object sender, EventArgs e)
{
if (tbNhap.Text != "") { btThem.Enabled = true; } else btThem.Enabled = false;
}
private void Save()
{
try
{
string path = Application.StartupPath + "\\Test.xml";
SaveLoadFile serializer = new SaveLoadFile();
serializer.SaveFile(tvNode, path);
//MessageBox.Show("Save thành công");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
------SaveLoadFile---------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;
namespace TestTreeView
{
class SaveLoadFile
{
private const string XmlNodeTag = "node";
// Xml attributes for node e.g. <node></node>
private const string XmlNodeTextAtt = "text";
//private const string XmlNodeTagAtt = "tag";
//private const string XmlNodeImageIndexAtt = "imageindex";
public SaveLoadFile()
{
}
public void SaveFile(System.Windows.Forms.TreeView treeView, string fileName)
{
XmlTextWriter textWriter = new XmlTextWriter(fileName, System.Text.Encoding.UTF8);
// writing the xml declaration tag
textWriter.WriteStartDocument();
//textWriter.WriteRaw("
");
// writing the main tag that encloses all node tags
textWriter.WriteStartElement("TreeView");
// save the nodes, recursive method
SaveNodes(treeView.Nodes, textWriter);
textWriter.WriteEndElement();
textWriter.Close();
}
public void SaveNodes(TreeNodeCollection nodesCollection, XmlTextWriter textWriter)
{
for(int i = 0; i < nodesCollection.Count; i++)
{
TreeNode node = nodesCollection[i];
textWriter.WriteStartElement(XmlNodeTag);
textWriter.WriteAttributeString(XmlNodeTextAtt, node.Text);
//textWriter.WriteAttributeString(XmlNodeImageIndexAtt, node.ImageIndex.ToString());
//if(node.Tag != null)
// textWriter.WriteAttributeString(XmlNodeTagAtt, node.Tag.ToString());
// add other node properties to serialize here
if (node.Nodes.Count > 0)
{
SaveNodes(node.Nodes, textWriter);
}
textWriter.WriteEndElement();
}
}
public void LoadFile(System.Windows.Forms.TreeView treeView, string fileName)
{
XmlTextReader reader = null;
try
{
// disabling re-drawing of treeview till all nodes are added
treeView.BeginUpdate();
reader = new XmlTextReader(fileName);
TreeNode parentNode = null;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == XmlNodeTag)
{
TreeNode newNode = new TreeNode();
bool isEmptyElement = reader.IsEmptyElement;
// loading node attributes
int attributeCount = reader.AttributeCount;
if (attributeCount > 0)
{
for (int i = 0; i < attributeCount; i++)
{
reader.MoveToAttribute(i);
SetAttributeValue(newNode, reader.Name, reader.Value);
}
}
// add new node to Parent Node or TreeView
if (parentNode != null)
parentNode.Nodes.Add(newNode);
else
treeView.Nodes.Add(newNode);
// making current node 'ParentNode' if its not empty
if (!isEmptyElement)
{
parentNode = newNode;
}
}
}
// moving up to in TreeView if end tag is encountered
else if (reader.NodeType == XmlNodeType.EndElement)
{
if (reader.Name == XmlNodeTag)
{
parentNode = parentNode.Parent;
}
}
else if (reader.NodeType == XmlNodeType.XmlDeclaration)
{ //Ignore Xml Declaration
}
else if (reader.NodeType == XmlNodeType.None)
{
return;
}
else if (reader.NodeType == XmlNodeType.Text)
{
parentNode.Nodes.Add(reader.Value);
}
}
}
finally
{
// enabling redrawing of treeview after all nodes are added
treeView.EndUpdate();
reader.Close();
}
}
private void SetAttributeValue(TreeNode node, string propertyName, string value)
{
if (propertyName == XmlNodeTextAtt)
{
node.Text = value;
}
}
}
}
Bạn đang đọc truyện trên: Truyen247.Pro