C# WinForm TreeView用法总结
TreeView用法值得总结一下,今天用到了,一时忘了怎样执行选中节点事件。
url:http://greatverve.cnblogs.com/archive/2012/02/03/winform-treeview.html
参考:C# WinForm TreeView改变选中节点颜色,失去焦点时选中节点仍突显。
private void BindTreeView()
{
treeView1.LabelEdit = false;//不可编辑
//添加结点
TreeNode root = new TreeNode();
root.Text = "根节点";
//一级
TreeNode node1 = new TreeNode();
node1.Text = "1";
TreeNode node2 = new TreeNode();
node2.Text = "2";
//二级
TreeNode node11 = new TreeNode();
node11.Text = "11";
TreeNode node12 = new TreeNode();
node12.Text = "12";
TreeNode node21 = new TreeNode();
node21.Text = "21";
TreeNode node22 = new TreeNode();
node22.Text = "22";
//二级加入一级
node1.Nodes.Add(node11);
node1.Nodes.Add(node12);
node2.Nodes.Add(node21);
node2.Nodes.Add(node22);
//一级加入根
root.Nodes.Add(node1);
root.Nodes.Add(node2);
//
treeView1.Nodes.Add(root);
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeView1.SelectedNode != null)
{
MessageBox.Show(treeView1.SelectedNode.Text);
}
}
{
treeView1.LabelEdit = false;//不可编辑
//添加结点
TreeNode root = new TreeNode();
root.Text = "根节点";
//一级
TreeNode node1 = new TreeNode();
node1.Text = "1";
TreeNode node2 = new TreeNode();
node2.Text = "2";
//二级
TreeNode node11 = new TreeNode();
node11.Text = "11";
TreeNode node12 = new TreeNode();
node12.Text = "12";
TreeNode node21 = new TreeNode();
node21.Text = "21";
TreeNode node22 = new TreeNode();
node22.Text = "22";
//二级加入一级
node1.Nodes.Add(node11);
node1.Nodes.Add(node12);
node2.Nodes.Add(node21);
node2.Nodes.Add(node22);
//一级加入根
root.Nodes.Add(node1);
root.Nodes.Add(node2);
//
treeView1.Nodes.Add(root);
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeView1.SelectedNode != null)
{
MessageBox.Show(treeView1.SelectedNode.Text);
}
}
参考:C# WinForm TreeView改变选中节点颜色,失去焦点时选中节点仍突显。
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。