using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication8
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void AddChildNode()
{
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (textBox1.Text =="")
{
TreeNode tmp = new TreeNode(textBox1.Text);
treeView1.SelectedNode.Nodes.Add(tmp);
treeView1.ExpandAll();
}
else
{
MessageBox.Show("TEXTBOX组件必须填入节点名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return;
}
}
private void AddParent()
{
//首先判断是否选定组件中节点的位置
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (textBox1.Text != "")
{
//创建一个节点对象,并初始化
TreeNode tmp;
tmp = new TreeNode(textBox1.Text);
//在TreeView组件中加入兄弟节点
treeView1.SelectedNode.Parent.Nodes.Add(tmp);
treeView1.ExpandAll();
}
else
{
MessageBox.Show("TextBox组件必须填入节点名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
TreeNode tnode = new TreeNode(textBox1.Text);
}
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
contextMenuStrip1.Show(this, new Point(e.X, e.Y));
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
treeView1.SelectedNode.Expand();
}
private void button4_Click(object sender, System.EventArgs e)
{
//定位根节点
treeView1.SelectedNode = treeView1.Nodes[0];
//展开组件中的所有节点
treeView1.SelectedNode.ExpandAll();
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
AddChildNode();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
AddParent();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
//判断选定的节点是否存在下一级节点
if (treeView1.SelectedNode.Nodes.Count == 0)
//删除节点
treeView1.SelectedNode.Remove();
else
MessageBox.Show("请先删除此节点中的子节点!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button5_Click(object sender, System.EventArgs e)
{
//定位根节点
treeView1.SelectedNode = treeView1.Nodes[0];
//折叠组件中所有节点
treeView1.SelectedNode.Collapse();
}
}
}