现在的位置: 首页 > 综合 > 正文

自定义TreeNode属性

2012年10月20日 ⁄ 综合 ⁄ 共 2814字 ⁄ 字号 评论关闭

一、目的:自定义TreeNode属性,让控件可以存储更多属性。有利于开发
二、方法:

public class GroupTreeNode : TreeNode
    {
        private IPAddress ip;
        private HostStatus status;
        public HostStatus Status
        {
            set { status = value; }
            get { return status; }
        }
        public IPAddress IP
        {
            set { ip = value; }
            get { return ip; }
        }

        public GroupTreeNode()
            : base()
        {

        }
        public GroupTreeNode(string text, int imageIndex, int selectedImageIndex, IPAddress address)
            : base(text, imageIndex, selectedImageIndex)
        {
            this.ip = address;
        }
    }

三、应用

Code

四、其他
循环访问 TreeView 控件的所有节点

private void PrintRecursive(TreeNode treeNode)
{
// Print the node.
System.Diagnostics.Debug.WriteLine(treeNode.Text);
MessageBox.Show(treeNode.Text);
// Print each node recursively.
foreach (TreeNode tn in treeNode.Nodes)
{
PrintRecursive(tn);
}
}
// Call the procedure using the TreeView.
private void CallRecursive(TreeView treeView)
{
// Print each node recursively.
TreeNodeCollection nodes = treeView.Nodes;
foreach (TreeNode n in nodes)
{
PrintRecursive(n);
}
}

抱歉!评论已关闭.