从填充有 .rtf 文件的 TreeView 打开 RichTextBox 中的 .rtf 文件
Open a .rtf file in a RichTextBox from a TreeView populated with .rtf files
我有一个填充了 .rtf 文件的 TreeView,我想在单击树节点时将这些文件加载到 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);
}
这里是我点击树节点后出现的错误:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe
Additional information: Object reference not set to an instance of an object.
使用
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode tn=e.Node;
string loc = currentLocation + "\" + tn.Text+ ".rtf";
richTextBox1.LoadFile(loc);
}
首先将选中的(无法从树视图中获取选中的节点)节点转换为 TreeNode,然后使用命令 tn.Text 获取选中节点的文本(文件名),之后你说到富文本框以从路径加载文件(您不需要分配文件流)。
我有一个填充了 .rtf 文件的 TreeView,我想在单击树节点时将这些文件加载到 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);
}
这里是我点击树节点后出现的错误:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe
Additional information: Object reference not set to an instance of an object.
使用
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode tn=e.Node;
string loc = currentLocation + "\" + tn.Text+ ".rtf";
richTextBox1.LoadFile(loc);
}
首先将选中的(无法从树视图中获取选中的节点)节点转换为 TreeNode,然后使用命令 tn.Text 获取选中节点的文本(文件名),之后你说到富文本框以从路径加载文件(您不需要分配文件流)。