TreeView tv = new TreeView()
tv.NodeStyle 其实TreeView有NodeStyle属性的,你可以设置
//contextMenuStrip1与treeView1关联,在节点上单击右键就给该节点添加子节点,在空白地方单击右键,就添加根节点using System
using System.Collections.Generic
using System.ComponentModel
using System.Data
using System.Drawing
using System.Text
using System.Windows.Forms
namespace WindowsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent()
this.treeView1.LabelEdit = true
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.treeView1.SelectedNode = e.Node
flag = true
contextMenuStrip1.Show(MousePosition)
}
}
bool flag = false
private void 添加节点ToolStripMenuItem_Click(object sender, EventArgs e)
{
TreeNode tn = new TreeNode()
if (flag)
{
this.treeView1.SelectedNode.Nodes.Add(tn)
this.treeView1.SelectedNode = tn
tn.BeginEdit()
flag = false
}
else
{
this.treeView1.Nodes.Add(tn)
this.treeView1.SelectedNode = tn
tn.BeginEdit()
flag = false
}
}
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
e.Node.Text = e.Label
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (!flag)
{
this.treeView1.SelectedNode = null
}
}
bool flag1 = false
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
flag1 = true
}
private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
if (!flag1)
{
flag = false
}
else
{
flag1 = false
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)