繁体   English   中英

从填充有.rtf文件的TreeView中打开RichTextBox中的.rtf文件

[英]Open a .rtf file in a RichTextBox from a TreeView populated with .rtf files

我有一个填充有.rtf文件的TreeView,并且我想在单击treenode时将文件加载到RichTextBox中。

这是代码:

private string currentLocation = Directory.GetCurrentDirectory() + "\\Notes";
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    string loc = currentLocation + "\\" + treeView1.SelectedNode.Text+ ".rtf";
    FileStream fs = new FileStream(loc, FileMode.Open, FileAccess.Read);
    richTextBox1.LoadFile(fs, RichTextBoxStreamType.RichText);
}

这是我单击树节点后发生的错误:

WindowsFormsApplication1.exe中发生了类型为'System.NullReferenceException'的未处理异常

附加信息:对象引用未设置为对象的实例。

采用

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    TreeNode tn=e.Node;
    string loc = currentLocation + "\\" + tn.Text+ ".rtf";
    richTextBox1.LoadFile(loc);
}

首先将选定的(无法从treeview中获得选定的节点)节点转换为TreeNode,然后使用命令tn.Text获得选定的节点的文本(文件名),然后对富文本框说以加载文件从路径开始(您无需分配文件流)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM