1.设置所选节点,如选中第二个节点
function SetSelNode()
{
TreeView1.selectedNodeIndex="1";
}
2.得到所选节点的Text,ID或NodeData
function GetAttribute()
{
alert(TreeView1.getTreeNode(TreeView1.selectedNodeIndex).getAttribute("Text");
}
替换Text为ID或NodeData,可分别得到所选节点的ID或NodeData
3.修改节点属性,如修改第一个节点的Text
function ModifyNode()
{
var node=TreeView1.getTreeNode("0");
node.setAttribute("Text","hgknight");
}
4.得到点击节点
function TreeView1.onclick()
{
alert(TreeView1.getTreeNode(TreeView1.clickedNodeIndex)。getAttribute("Text");
}
5.添加节点
function AddNode()
{
var node=TreeView1.createTreeNode();
node.setAttribute("Text","hgknight");
TreeView1.add(node);
}
6.js遍历所有节点
var AllRootNode=new Array();
AllRootNode=TreeView1.getChildren();
AlertNode(AllRootNode);
function AlertNode(NodeArray)
{
if(parseInt(NodeArray.length)==0)
return;
else
{
for(i=0;i
var cNode;
cNode=NodeArray[i];
alert(cNode.getAttribute("Text");
if(parseInt(cNode.getChildren().length)!=0)
AlertNode(cNode.getChildren());
}
}
}
当前选中项:TreeView.SelectedNode
增加顶级节点:TreeView.Nodes.Add("Key", "Text")
增加同级节点:TreeView.SelectedNode.Parent.Nodes.Add("Key", "Text")
增加子节点:TreeView.SelectedNode.Nodes.Add("Key", "Text")
全部展开:TreeView.ExpandAll()
全部收拢:TreeView.CollapseAll()
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[i] = new TreeNodeData(treeview.Nodes[i]);
}
}
/// 通过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[i].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[i] = new TreeNodeData(node.Nodes[i]);
}
}
/// 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[i].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
{
}
}
}
}