c#中Treeview这个WINDOWS FROM控件的使用对接点的基本操作:加入新接点,加入兄弟接点,删除接点;
实现代码如下:
删除{treeView1.SelectedNode.Remove ( )} ;
接点添加:
private void AddChildNode ( )
{
if ( treeView1.SelectedNode == null )
{
MessageBox.Show ( "请选择一个节点" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
}
else
{
//创建一个节点对象,并初始化
try
{
ArrayList r=new ArrayList();
Form3 f=new Form3();
f.ShowDialog(this);
TreeNode tmp=new TreeNode(f.textBox1.Text);
r.Add(f.textBox1.Text.ToString());
r.Add(f.textBox2.Text.ToString());
r.Add(f.textBox3.Text.ToString());
r.Add(f.textBox4.Text.ToString());
r.Add(f.textBox5.Text.ToString());
r.Add(f.richTextBox1.Text.ToString());
tmp.Tag=r;
tmp.Text=f.textBox1.Text;
ArrayList t=(ArrayList)this.treeView1.SelectedNode.Tag;
if(System.Convert.ToDateTime(f.textBox2.Text.ToString())<=System.Convert.ToDateTime(t[1].ToString()))
{
MessageBox.Show ( "怎么可能!" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
}
else
{
treeView1.SelectedNode.Nodes.Add(tmp);
treeView1.SelectedNode = tmp;
treeView1.ExpandAll ( );
}
}
catch
{
MessageBox.Show ( "正确输入日期!" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
}
}
}
private void AddParent ( )
{
//首先判断是否选定组件中节点的位置
if ( treeView1.SelectedNode.Parent == null )
{
MessageBox.Show ( "请选择一个节点" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
}
else
{
try
{
ArrayList r=new ArrayList();
Form3 f=new Form3();
f.ShowDialog();
r.Add(f.textBox1.Text.ToString());
r.Add(f.textBox2.Text.ToString());
r.Add(f.textBox3.Text.ToString());
r.Add(f.textBox4.Text.ToString());
r.Add(f.textBox5.Text.ToString());
r.Add(f.richTextBox1.Text.ToString());
TreeNode tmp = new TreeNode (f.textBox1.Text);
tmp.Tag=r;
tmp.Text=f.textBox1.Text;
ArrayList t=(ArrayList)this.treeView1.SelectedNode.Parent.Tag;
if(System.Convert.ToDateTime(f.textBox2.Text)<=System.Convert.ToDateTime(t[1].ToString()))
{
MessageBox.Show ( "怎么可能!" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information );
}
else
{
treeView1.SelectedNode.Parent.Nodes.Add ( tmp ) ;
treeView1.ExpandAll();
}
}
catch
{
MessageBox.Show ( "正确输入日期!" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
}
//在TreeView组件中加入兄弟节点
}
}
遍历接点的算法:
public TreeNode FindNode(TreeNode root,string strValue )
{
if(root==null)
return null;
if(root.Text==strValue)
return root;
TreeNode r=null;
foreach(TreeNode node in root.Nodes)
{
r=FindNode(node,strValue);
if(r!=null)
break;
}
return r;
}
序列化实现对TREEVIEW的存储:
///class2 对TreeView进行序列化
///用于文件的存读;
///文件以二进制写入
public class TreeViewDataAccess
{
/// TreeViewData
[Serializable()]
public struct TreeViewData
{
public TreeNodeData[] Nodes;
/// 递归初始化TreeView数据
public TreeViewData(TreeView treeview)
{
Nodes = new TreeNodeData[treeview.Nodes.Count];
if (treeview.Nodes.Count == 0)
{
return;
}
for (int i = 0; i <= treeview.Nodes.Count - 1; i++)
{
Nodes = new TreeNodeData(treeview.Nodes);
}
}
/// 通过TreeViewData弹出TreeView
public void PopulateTree(TreeView treeview)
{
if (this.Nodes == null || this.Nodes.Length == 0)
{
return;
}
treeview.BeginUpdate();
for (int i = 0; i <= this.Nodes.Length - 1; i++)
{
treeview.Nodes.Add(this.Nodes.ToTreeNode());
}
treeview.EndUpdate();
}
}
/// TreeNodeData
[Serializable()]
public struct TreeNodeData
{
public string Text;
public int ImageIndex;
public int SelectedImageIndex;
public bool Checked;
public bool Expanded;
public object Tag;
public Color BackColor;
public TreeNodeData[] Nodes;
/// TreeNode构造函数
public TreeNodeData(TreeNode node)
{
this.Text = node.Text;
this.ImageIndex = node.ImageIndex;
this.SelectedImageIndex = node.SelectedImageIndex;
this.Checked = node.Checked;
this.BackColor=node.BackColor;
this.Expanded = node.IsExpanded;
this.Nodes = new TreeNodeData[node.Nodes.Count];
this.Tag =node.Tag;
if (node.Nodes.Count == 0)
{
return;
}
for (int i = 0; i <= node.Nodes.Count - 1; i++)
{
Nodes = new TreeNodeData(node.Nodes);
}
}
/// TreeNodeData返回TreeNode
public TreeNode ToTreeNode()
{
TreeNode ToTreeNode = new TreeNode(this.Text, this.ImageIndex, this.SelectedImageIndex);
ToTreeNode.Checked = this.Checked;
ToTreeNode.BackColor=this.BackColor;
ToTreeNode.Tag = this.Tag;
if (this.Expanded)
{
ToTreeNode.Expand();
}
if (this.Nodes == null && this.Nodes.Length == 0)
{
return null;
}
if(ToTreeNode != null && this.Nodes.Length == 0)
{
return ToTreeNode;
}
for (int i = 0; i <= this.Nodes.Length - 1; i++)
{
ToTreeNode.Nodes.Add(this.Nodes.ToTreeNode());
}
return ToTreeNode;
}
}
/// 加载TreeView
public static void LoadTreeViewData(TreeView treeView, string path)
{
try
{
BinaryFormatter ser = new BinaryFormatter();
Stream file = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read);
TreeViewData treeData = ((TreeViewData)(ser.Deserialize(file)));
treeData.PopulateTree(treeView);
file.Close();
}
catch
{}
}
/// 保存TreeView到文件
public static void SaveTreeViewData(TreeView treeView, string path)
{
try
{
BinaryFormatter ser = new BinaryFormatter();
Stream file = new FileStream(path,FileMode.Create);
ser.Serialize(file,new TreeViewData(treeView));
file.Close();
}
catch
{
}
}
}
}