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

标签 c# 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);
   }
}

关于c# - 如何在 RichTextBox 中加载 .rtf 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20534564/

相关文章:

c# - String.IndexOf 函数为 TextBox.Select 提供了错误的位置编号

c# - 如何增加 richtextbox 中文本的字体大小

c# - WPF 如何让 RichTextBox 的宽度跟随父控件的宽度?

c# - 如何更改 Office 功能区的背景和前景

c# - 查找文本中完全匹配的所有关键字及其索引c#

c# - Entity Framework Core - 如何可编程性关闭自动增量?

c# - 在参数化查询中插入多个条目

c# - 哪个事件捕获对 RichTextBox 的每次编辑?

javascript - 单击编辑按钮时,stackoverflow 如何格式化文本区域?

c# - 使用无效键值 : "fields". 访问的 JArray 值应为数组位置索引