如何获取treeview控件选中的根节点和子结点的名字?
希望看了下面的图,你能够清除怎么做。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
//获取当前节点的名称
this.Text = treeView1.SelectedNode.Text;
List<string> subNodes=new List<string>();
//判断时候有子节点
if (treeView1.SelectedNode.Nodes.Count>0)
{
foreach (TreeNode subNode in treeView1.SelectedNode.Nodes)
{
subNodes.Add(subNode.Text);
}
}
//打印出所有的子节点
textBox1.Lines = subNodes.ToArray();
}
}
}