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 判断为右键点击弹出上下文。