using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ExTreeView
{
public partial class Form1 : Form
{
ContextMenu contextMenu1;
public Form1()
{
contextMenu1=new System.Windows.Forms.ContextMenu();
InitializeComponent();
InitializeTreeView();
this.contextMenu1.Popup += new System.EventHandler(MyPopupEventHandler);
this.treeView1.ContextMenu = this.contextMenu1;
}
private void MyPopupEventHandler(System.Object sender, System.EventArgs e)
{
// Define the MenuItem objects to display for the TextBox.
MenuItem menuItem1 = new MenuItem("AddBrother", new System.EventHandler(button6_Click));
// Define the MenuItem object to display for the PictureBox.
MenuItem menuItem3 = new MenuItem("AddChild", new System.EventHandler(button4_Click));
MenuItem menuItem4 = new MenuItem("Delete", new System.EventHandler(button5_Click));
// Clear all previously added MenuItems.
contextMenu1.MenuItems.Clear();
// if (contextMenu1.SourceControl == textBox1)
// {
// Add MenuItems to display for the TextBox.
contextMenu1.MenuItems.Add(menuItem1);
contextMenu1.MenuItems.Add(menuItem3);
// }
// else if (contextMenu1.SourceControl == pictureBox1)
// {
// Add the MenuItem to display for the PictureBox.
contextMenu1.MenuItems.Add(menuItem4);
// }
}
// Populates a TreeView control with example nodes.
private void InitializeTreeView()
{
treeView1.BeginUpdate();
treeView1.Nodes.Add("往来客户");
treeView1.Nodes[0].Nodes.Add("01Child 1");
treeView1.Nodes[0].Nodes.Add("02Child 2");
treeView1.Nodes[0].Nodes.Add("03Child 3");
treeView1.Nodes[0].Nodes.Add("04Child 4");
treeView1.ExpandAll();
treeView1.EndUpdate();
}
private void AddChildNode()
{
string lableStart = treeView1.SelectedNode.Text.Substring(0, 2);
//首先判断是否选定组件中的位置
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (textBox3.Text == null)
{ }
TreeNode tmp = new TreeNode(lableStart);
treeView1.SelectedNode.Nodes.Add(tmp);
// treeView1.SelectedNode = tmp;
// TreeNode mySelectedNode = treeView1.SelectedNode;
/* if ( tmp.Parent != null)
{
treeView1.SelectedNode = tmp;
treeView1.LabelEdit = true;
if (!tmp.IsEditing)
{
tmp.BeginEdit();
}
}
else
{
MessageBox.Show("No tree node selected or selected node is a root node.\n" +
"Editing of root nodes is not allowed.", "Invalid selection");
}
if (textBox1.Text != "")
{
//创建一个节点对象,并初始化
TreeNode tmp;
tmp = new TreeNode(textBox1.Text);
//在TreeView组件中加入子节点
treeView1.SelectedNode = tmp;
treeView1.ExpandAll();
}
else
{
MessageBox.Show("TextBox组件必须填入节点名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}*/
}
}
private void AddBrothers()
{
//首先判断是否选定组件中节点的位置
if (treeView1.SelectedNode == null || treeView1.SelectedNode.Parent==null)
{
MessageBox.Show("请选择一个节点或者根结点不能添加兄弟节点!!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//创建一个节点对象,并初始化
TreeNode tmp = new TreeNode(" ");
//在TreeView组件中加入兄弟节点
treeView1.SelectedNode.Parent.Nodes.Add(tmp);
treeView1.SelectedNode = tmp;
treeView1.LabelEdit = true;
if (!tmp.IsEditing)
{
tmp.BeginEdit();
}
}
}
private void button4_Click(object sender, EventArgs e)
{
AddChildNode();
}
private void button5_Click(object sender, EventArgs e)
{
//判断选定的节点是否存在下一级节点
if (treeView1.SelectedNode.Nodes.Count == 0)
//删除节点
treeView1.SelectedNode.Remove();
else
MessageBox.Show("请先删除此节点中的子节点!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button6_Click(object sender, EventArgs e)
{
AddBrothers();
}
private void button7_Click(object sender, EventArgs e)
{
TreeNode tem = (TreeNode)sender;
}
/* Get the tree node under the mouse pointer and
save it in the mySelectedNode variable.
private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
}
*/
private void treeView1_AfterLabelEdit_1(object sender, NodeLabelEditEventArgs e)
{
if (e.Label != null)
{
if (e.Label.Length > 0)
{
if (e.Label.IndexOfAny(new char[] { '@', '.', ',', '!' }) == -1)
{
//if(e.Label.StartsWith(textBox2.Text.Trim()))
// Stop editing without canceling the label change.
e.Node.EndEdit(false);
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\n" +
"The invalid characters are: '@','.', ',', '!'",
"Node Label Edit");
e.Node.BeginEdit();
}
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
"Node Label Edit");
e.Node.BeginEdit();
}
this.treeView1.LabelEdit = false;
}
}
private void treeView1_DoubleClick(object sender, EventArgs e)
{
TreeNode mySelectedNode = treeView1.SelectedNode;
if (mySelectedNode != null && mySelectedNode.Parent != null)
{
treeView1.SelectedNode = mySelectedNode;
treeView1.LabelEdit = true;
if (!mySelectedNode.IsEditing)
{
mySelectedNode.BeginEdit();
}
}
else
{
MessageBox.Show("No tree node selected or selected node is a root node.\n" +
"Editing of root nodes is not allowed.", "Invalid selection");
}
}
private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
{
TreeNode mySelectedNode = this.treeView1.SelectedNode;
textBox1.Text = mySelectedNode.Text;
textBox2.Text = textBox1.Parent.Text.Substring(0, 2);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ExTreeView
{
public partial class Form1 : Form
{
ContextMenu contextMenu1;
string lableStart;
public Form1()
{
contextMenu1=new System.Windows.Forms.ContextMenu();
InitializeComponent();
InitializeTreeView();
this.contextMenu1.Popup += new System.EventHandler(MyPopupEventHandler);
this.treeView1.ContextMenu = this.contextMenu1;
}
private void MyPopupEventHandler(System.Object sender, System.EventArgs e)
{
// Define the MenuItem object to display for the PictureBox.
MenuItem menuItem1 = new MenuItem("AddChild", new System.EventHandler(button4_Click));
MenuItem menuItem2 = new MenuItem("Delete", new System.EventHandler(button5_Click));
MenuItem menuItem3 = new MenuItem("Edit", new System.EventHandler(button6_Click));
// Clear all previously added MenuItems.[必须]
contextMenu1.MenuItems.Clear();
//如果需要根据空间来选择contextMenu的MenuItems,
//可以用if (contextMenu1.SourceControl == textBox1)
contextMenu1.MenuItems.Add(menuItem1);
contextMenu1.MenuItems.Add(menuItem2);
contextMenu1.MenuItems.Add(menuItem3);
}
// Populates a TreeView control with example nodes.
private void InitializeTreeView()
{
treeView1.BeginUpdate();
treeView1.Nodes.Add("科目");
treeView1.Nodes[0].Nodes.Add("01Child 1");
treeView1.Nodes[0].Nodes.Add("02Child 2");
treeView1.Nodes[0].Nodes.Add("03Child 3");
treeView1.Nodes[0].Nodes.Add("04Child 4");
treeView1.Nodes[0].Nodes.Add("05Child 5");
treeView1.ExpandAll();
treeView1.EndUpdate();
}
/// <summary>
/// 增加子节点
/// </summary>
private void AddChildNode()
{
//寻找那个以0X开头且在非第0层下面加子节点
if (textBox2.Text != "")
{
lableStart = textBox2.Text.Substring(0,2);
foreach (TreeNode tn in treeView1.Nodes[0].Nodes)
{
if (tn.Text.StartsWith(lableStart))
{
treeView1.SelectedNode = tn;
}
}
}
//首先判断是否选定组件中的位置
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (textBox3.Text =="")
{
TreeNode tmp = new TreeNode(lableStart);
treeView1.SelectedNode.Nodes.Add(tmp);
if (tmp.Parent != null)
{
treeView1.SelectedNode = tmp;
treeView1.LabelEdit = true;
if (!tmp.IsEditing)
{
tmp.BeginEdit();
}
}
else
{
MessageBox.Show("No tree node selected or selected node is a root node.\n" +
"Editing of root nodes is not allowed.", "Invalid selection");
}
}
else
{
if (textBox3.Text.StartsWith(lableStart))
{
TreeNode tmp = new TreeNode(textBox3.Text);
treeView1.SelectedNode.Nodes.Add(tmp);
}
else
{
MessageBox.Show("要以父节点的编号开始命名子节点","the Name");
}
}
}
}
private void AddBrothers()
{
//首先判断是否选定组件中节点的位置
if (treeView1.SelectedNode == null || treeView1.SelectedNode.Parent==null)
{
MessageBox.Show("请选择一个节点或者根结点不能添加兄弟节点!!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//创建一个节点对象,并初始化
TreeNode tmp = new TreeNode(" ");
//在TreeView组件中加入兄弟节点
treeView1.SelectedNode.Parent.Nodes.Add(tmp);
treeView1.SelectedNode = tmp;
treeView1.LabelEdit = true;
if (!tmp.IsEditing)
{
tmp.BeginEdit();
}
}
}
private void button4_Click(object sender, EventArgs e)
{
AddChildNode();
treeView1.SelectedNode.Expand();
textBox3.Text = "";
}
private void button5_Click(object sender, EventArgs e)
{
//判断选定的节点是否存在下一级节点
if (treeView1.SelectedNode.Nodes.Count == 0)
//删除节点
treeView1.SelectedNode.Remove();
else
MessageBox.Show("请先删除此节点中的子节点!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button6_Click(object sender, EventArgs e)
{
AddBrothers();
}
private void button7_Click(object sender, EventArgs e)
{
TreeNode tem = (TreeNode)sender;
}
/* Get the tree node under the mouse pointer and
save it in the mySelectedNode variable.
private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
}
*/
private void treeView1_AfterLabelEdit_1(object sender, NodeLabelEditEventArgs e)
{
// lableStart = treeView1.SelectedNode.Text.Substring(0, 2);
if (e.Label != null)
{
if (e.Label.Length > 0)
{
if (e.Label.IndexOfAny(new char[] { '@', '.', ',', '!' }) == -1 )
{
// Stop editing without canceling the label change.
e.Node.EndEdit(false);
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\n" +
"The invalid characters are: '@','.', ',', '!'",
"Node Label Edit");
e.Node.BeginEdit();
}
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
"Node Label Edit");
e.Node.BeginEdit();
}
this.treeView1.LabelEdit = false;
}
}
private void treeView1_DoubleClick(object sender, EventArgs e)
{
TreeNode mySelectedNode = treeView1.SelectedNode;
if (mySelectedNode != null && mySelectedNode.Parent != null)
{
treeView1.SelectedNode = mySelectedNode;
treeView1.LabelEdit = true;
if (!mySelectedNode.IsEditing)
{
mySelectedNode.BeginEdit();
}
}
else
{
MessageBox.Show("No tree node selected or selected node is a root node.\n" +
"Editing of root nodes is not allowed.", "Invalid selection");
}
}
private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
TreeNode mySelectedNode = treeView1.SelectedNode;
mySelectedNode.Text = textBox4.Text;
mySelectedNode.EndEdit(false);
}
private void checkBox9_CheckedChanged(object sender, EventArgs e)
{
//
CheckBox checkBox = (CheckBox)sender;
if (checkBox.Checked)
{
textBox4.Enabled = true;
TreeNode mySelectedNode = treeView1.SelectedNode;
if (mySelectedNode != null)
{
lableStart = treeView1.SelectedNode.Text.Substring(0, 2);
if (textBox4.Text.StartsWith(lableStart))
{
treeView1.LabelEdit = true;
mySelectedNode.BeginEdit();
}
}
else
{
MessageBox.Show("No tree node selected or selected node is a root node.\n" +
"Editing of root nodes is not allowed.", "Invalid selection");
}
}
else
{
textBox4.Enabled = false;
treeView1.LabelEdit = false;
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode mySelectedNode = this.treeView1.SelectedNode;
if (mySelectedNode.IsEditing)
{
textBox1.Text = mySelectedNode.Parent.Text;
textBox2.Text = mySelectedNode.Parent.Text;
textBox4.Text = mySelectedNode.Parent.Text;
textBox4.Enabled = false;
}
else
{
textBox1.Text = mySelectedNode.Text;
textBox2.Text = mySelectedNode.Text;
textBox4.Text = mySelectedNode.Text;
textBox4.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}