簡體   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