繁体   English   中英

如何在RichTextBox中加载.rtf文件?

[英]How to load a .rtf file in RichTextBox?

我有两种形式。 如果我单击第一个窗体上的按钮,我希望它自动加载第二个窗体,其中RichTextBox加载了.rtf文件。

我想请求帮助在RichTextBox中加载.rtf文件而不在表单中编码路径? 我尝试使用Directory.GetCurrentDirectory ,但我很难过,因为我不是那个经验丰富的程序员。

尝试这个:

public void LoadMyFile()
{
   // Create an OpenFileDialog to request a file to open.
   OpenFileDialog openFile1 = new OpenFileDialog();

   // Initialize the OpenFileDialog to look for RTF files.
   openFile1.DefaultExt = "*.rtf";
   openFile1.Filter = "RTF Files|*.rtf";

   // Determine whether the user selected a file from the OpenFileDialog. 
   if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
      openFile1.FileName.Length > 0) 
   {
      // Load the contents of the file into the RichTextBox.
      richTextBox1.LoadFile(openFile1.FileName);
   }
}

暂无
暂无

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

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