以下是我尝试的内容
namespace USCS
{
public partial class Form5 : Form
{
List<String> CheckedNodes = new List<String>();
public Form5()
{
InitializeComponent();
InitializeTreeView();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
{
if (e.Node.Checked)
{
CheckedNodes.Add(e.Node.FullPath.ToString());
}
else
{
CheckedNodes.Remove(e.Node.FullPath.ToString());
}
}
private void treeView1_AfterSelect_2(object sender, TreeViewEventArgs e)
{
if (e.Node.Checked)
{
CheckedNodes.Add(e.Node.FullPath.ToString());
}
else
{
CheckedNodes.Remove(e.Node.FullPath.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
Behavior desc_behav = new Behavior();
setBehaviorData(desc_behav);
// Form5.ActiveForm.Hide();
// var myForm = new Form5();
// myForm.Show();
}
private void InitializeTreeView()
{
treeView1.BeginUpdate();
treeView1.Nodes.Add("Parent");
treeView1.Nodes[0].Nodes.Add("Child 1");
treeView1.Nodes[0].Nodes.Add("Child 2");
treeView1.Nodes[0].Nodes[1].Nodes.Add("Grandchild");
treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("Great Grandchild");
treeView1.EndUpdate();
}
private void setBehaviorData(Behavior desc_behav)
{
var message = CheckedNodes[0];
MessageBox.Show(message.ToString());
}
private void Traverse(TreeNodeCollection nodes)
{
String f;
foreach (TreeNode node in nodes)
{
f = node.Text;
Traverse(node.Nodes);
}
}
}
}
正如你所看到的,我正试图获得;文件路径";树视图中所选用户框的。我只是不断收到错误消息;索引超出范围";这毫无意义,因为它正在索引0处拉取,或者当我尝试直接调用列表时,我只得到一条System.checkednodes[System]消息。
实际上只是希望它返回一个名称路径的字符串。
是否要在树视图中获取所有选中的节点?如果是这样,您可以尝试使用递归并订阅treeView_AfterCheck
。
这里有一个简单的演示,你可以参考。
private void Form1_Load(object sender, EventArgs e)
{
treeView1.CheckBoxes = true;
}
List<String> CheckedNodes = new List<String>();
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
CheckedNodes = GetCheckedNodes(treeView1.Nodes);
foreach (var i in CheckedNodes)
{
Console.WriteLine(i);
}
}
public List<string> GetCheckedNodes(TreeNodeCollection nodes)
{
List<string> nodeList = new List<string>();
if (nodes == null)
{
return nodeList;
}
foreach (TreeNode childNode in nodes)
{
if (childNode.Checked)
{
nodeList.Add(childNode.FullPath);
}
nodeList.AddRange(GetCheckedNodes(childNode.Nodes));
}
return nodeList;
}
您可以使用这样的东西:
private void button1_Click(object sender, EventArgs e)
{
List<string> checkedNodes = new List<string>(CheckedNodes(treeView1));
foreach(string path in checkedNodes)
{
Console.WriteLine(path);
}
}
private IEnumerable<String> CheckedNodes(TreeView tv)
{
foreach (string path in CheckedNodes(tv.Nodes))
{
yield return path;
}
}
private IEnumerable<String> CheckedNodes(TreeNodeCollection tnCol)
{
foreach (TreeNode tn in tnCol)
{
if (tn.Checked)
{
yield return tn.FullPath;
}
if (tn.Nodes.Count > 0)
{
foreach (String path in CheckedNodes(tn.Nodes))
{
yield return path;
}
}
}
}