- 声明根节点
private TreeNode root1; private TreeNode root2; private TreeNode root3; private TreeNode root4; public Form1() { InitializeComponent(); Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { treeView1.Font = new Font(treeView1.Font.FontFamily, 14.5f); root1 = treeView1.Nodes.Add("FirstRoot"); root2 = treeView1.Nodes.Add("SecondRoot"); root3 = treeView1.Nodes.Add("ThirdRoot"); root4 = treeView1.Nodes.Add("FourRoot"); }
- 添加子节点
private void Form1_Load(object sender, EventArgs e) { treeView1.Font = new Font(treeView1.Font.FontFamily, 14.5f); root1 = treeView1.Nodes.Add("FirstRoot"); root2 = treeView1.Nodes.Add("SecondRoot"); root3 = treeView1.Nodes.Add("ThirdRoot"); root4 = treeView1.Nodes.Add("FourRoot"); Root1AddChild(); } private void Root1AddChild() { TreeNode root1Child_1=new TreeNode("FirstChild_1"); TreeNode root1Child_2 = new TreeNode("FirstChild_2"); TreeNode root1Child_3 = new TreeNode("FirstChild_3"); TreeNode root1Child_4 = new TreeNode("FirstChild_4"); root1.Nodes.Add(root1Child_1); root1.Nodes.Add(root1Child_2); root1.Nodes.Add(root1Child_3); root1.Nodes.Add(root1Child_4); }
- 子节点下再增加子节点
/// <summary> /// Root增加子节点 /// </summary> private void Root1AddChild() { TreeNode root1Child_1=new TreeNode("FirstChild_1"); TreeNode root1Child_2 = new TreeNode("FirstChild_2"); TreeNode root1Child_3 = new TreeNode("FirstChild_3"); TreeNode root1Child_4 = new TreeNode("FirstChild_4"); root1Child_1.ForeColor = Color.Blue; //节点字体颜色 root1Child_2.ForeColor = Color.Blue; root1Child_3.ForeColor = Color.Blue; root1Child_4.ForeColor = Color.Blue; root1.Nodes.Add(root1Child_1); root1.Nodes.Add(root1Child_2); root1.Nodes.Add(root1Child_3); root1.Nodes.Add(root1Child_4); FirstChildAddChild(root1Child_1); } /// <summary> /// 子节点下再增加 子节点 /// </summary> /// <param name="root1Child_1"></param> private void FirstChildAddChild(TreeNode root1Child_1) { TreeNode grandChild1 = new TreeNode("GrandChild_1"); TreeNode grandChild2 = new TreeNode("GrandChild_2"); TreeNode grandChild3 = new TreeNode("GrandChild_3"); TreeNode grandChild4 = new TreeNode("GrandChild_4"); grandChild1.ForeColor = Color.Red; //节点字体颜色 grandChild2.ForeColor = Color.Red; grandChild3.ForeColor = Color.Red; grandChild4.ForeColor = Color.Red; root1Child_1.Nodes.Add(grandChild1); root1Child_1.Nodes.Add(grandChild2); root1Child_1.Nodes.Add(grandChild3); root1Child_1.Nodes.Add(grandChild4); }
- 节点点击时间
/// <summary> /// 子节点下再增加 子节点 /// </summary> /// <param name="root1Child_1"></param> private void FirstChildAddChild(TreeNode root1Child_1) { TreeNode grandChild1 = new TreeNode("GrandChild_1"); TreeNode grandChild2 = new TreeNode("GrandChild_2"); TreeNode grandChild3 = new TreeNode("GrandChild_3"); TreeNode grandChild4 = new TreeNode("GrandChild_4"); grandChild1.Tag = "Tag可以存放数据,类型为Object"; grandChild1.ForeColor = Color.Red; //节点字体颜色 grandChild2.ForeColor = Color.Red; grandChild3.ForeColor = Color.Red; grandChild4.ForeColor = Color.Red; root1Child_1.Nodes.Add(grandChild1); root1Child_1.Nodes.Add(grandChild2); root1Child_1.Nodes.Add(grandChild3); root1Child_1.Nodes.Add(grandChild4); } private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { /* e.Node == 当前点击的节点 e.Node.Level ==当前点击节点深度(层) 从根节点0开始 =》ROOT=0 */ if (e.Node.Level==2) { if (e.Node.Tag!=null) { MessageBox.Show(e.Node.Tag.ToString()); } } }
- 节点== 右键上下文
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { /* e.Node == 当前点击的节点 e.Node.Level ==当前点击节点深度(层) 从根节点0开始 =》ROOT=0 */ if (e.Node.Level==2) { if (e.Node.Tag!=null) { MessageBox.Show(e.Node.Tag.ToString()); } } if (e.Button == MouseButtons.Right) { e.Node.ContextMenuStrip = contextMenuStrip1; } }
e.Button == MouseButtons.Right 判断为右键点击弹出上下文。
© 著作权归作者所有
举报
发表评论
0/200