C sharp中的Treeview节点 [英] Treeview node in C sharp

查看:114
本文介绍了C sharp中的Treeview节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在richtextbox中显示文本文件内容。项目启动时,它运行项目没有任何错误。我还发现当我尝试将brekpoint插入treeView1_AfterSelect时,控件不会去那里

I am unable to display the text file contents in richtextbox. When the project is started it runs the project without any error. I also found when I tried inserting brekpoint into treeView1_AfterSelect, the control doesn''t go there

public Form1()
{
    InitializeComponent();

    DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer")
    if (directoryInfo.Exists)
    {
        treeView1.AfterSelect += treeView1_AfterSelect;
        BuildTree(directoryInfo, treeView1.Nodes);
    }
}

private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe)
{
    TreeNode curNode = addInMe.Add(directoryInfo.Name);

    foreach (FileInfo file in directoryInfo.GetFiles())
    {
        curNode.Nodes.Add(file.FullName, file.Name);
    }
    foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
    {
        BuildTree(subdir, curNode.Nodes);
    }
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    if(e.Node.Name.EndsWith("txt"))
    {
        this.richTextBox1.Clear();
        StreamReader reader = new StreamReader(e.Node.Name);
        this.richTextBox1.Text = reader.ReadToEnd();
        reader.Close();
    }
}

推荐答案

检查以确保为TreeView1连接了AfterSelect事件。在treeView1的属性中,选择事件图标以显示TreeView的事件。查看AfterSelect元素,如果未设置,请将其链接到您的方法。
Check to make sure the AfterSelect event is wired up for treeView1. In the properties for treeView1, select the Events icon to display the events for the TreeView. Look in the AfterSelect element and if it''s not set, link it to your method there.


这篇关于C sharp中的Treeview节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆