我有一个用TreeView文件填充的.rtf文件,我希望在单击树代码时将这些文件加载到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”类型的未处理异常 附加信息:对象引用未设置为对象的实例。
发布于 2015-09-08 17:54:23
使用
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获取所选节点的文本(文件名),然后对富文本框说从路径加载文件(不需要分配文件流)。
https://stackoverflow.com/questions/32463844
复制相似问题