ITPub博客

首页 > Linux操作系统 > Linux操作系统 > c#TreeView 控件的使用

c#TreeView 控件的使用

原创 Linux操作系统 作者:wangping_cool 时间:2012-10-31 16:40:11 0 删除 编辑

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 TreeView

{

public partial class Form1 : Form.

 {

public Form1()

 {

InitializeComponent();

TreeNode tnode = new TreeNode("山东");

tvNo1.Nodes.Add(tnode);

}

/// /// 展开子节点 /// /// ///

private void btnExpand_Click(object sender, EventArgs e)

{ tvNo1.SelectedNode.Expand(); } /

// /// 展开所有节点 /// /// ///

private void btnExpandAll_Click(object sender, EventArgs e)

{ tvNo1.SelectedNode = tvNo1.Nodes[0];

tvNo1.SelectedNode.ExpandAll(); }

/// /// 折叠所有节点 /// /// ///

private void btnCollaspeAll_Click(object sender, EventArgs e)

 { tvNo1.SelectedNode = tvNo1.Nodes[0]; tvNo1.SelectedNode.Collapse(); }

/// /// 实现增加子节点 /// /// ///

private void menuItem1_Click(object sender, EventArgs e)

 { AddNode(); }

/// /// 实现增加兄弟节点 /// /// ///

private void menuItem2_Click(object sender, EventArgs e)

{ AddParentNode(); }

/// /// 实现删除节点 /// /// ///

private void menuItem3_Click(object sender, EventArgs e)

{ DeleteNode(); }

/// /// 加入子节点 ///

private void AddNode()

{

if (tvNo1.SelectedNode == null)

{ MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); }

else

 {

 if (txtNodeName.Text != "")

{

TreeNode childNode;

childNode = new TreeNode(txtNodeName.Text);

tvNo1.SelectedNode.Nodes.Add(childNode);

 tvNo1.SelectedNode = childNode;

tvNo1.ExpandAll();

 }

else

 {

 MessageBox.Show("TextBox组建必须填入节点名称", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return;

} } }

/// /// 加入兄弟节点 ///

private void AddParentNode()

{

 if (tvNo1.SelectedNode == null)

{ MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }

else

{

if (txtNodeName.Text != "")

 { TreeNode parentNode = new TreeNode(txtNodeName.Text); tvNo1.SelectedNode.Parent.Nodes.Add(parentNode);

tvNo1.SelectedNode = parentNode; tvNo1.ExpandAll();

} else

{ MessageBox.Show("TextBox组建必须填入节点名称", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

return;

} }

TreeNode tNode = new TreeNode(txtNodeName.Text);

} /// /// 删除节点 ///

private void DeleteNode()

{

if (tvNo1.SelectedNode.Nodes.Count == 0)

{ tvNo1.SelectedNode.Remove(); }

else

 { MessageBox.Show("请先删除此节点中的子节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

return;

} }

/// /// 鼠标右击功能实现增加删除节点选项 /// /// ///

private void tvNo1_MouseDown(object sender, MouseEventArgs e)

{

if (e.Button == MouseButtons.Right)

 { contextMenu1.Show(this, new Point(e.X, e.Y));

 }

}

}

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28311102/viewspace-748027/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2012-10-26

  • 博文量
    8
  • 访问量
    17580