如何禁用RichTextBox的富文本效果?

huangapple go评论120阅读模式
英文:

How i can disable the rich text effect of richtextbox?

问题

  1. 如何禁用`richtextbox`的富文本效果?我正在使用Windows FormsC#,.Net Framework 4.8并尝试制作一个记事本应用程序。但是富文本效果很糟糕。
  2. 示例:
  3. ![外观](https://i.stack.imgur.com/oTcPN.png)
  4. 那个语法高亮和图片。
  5. 我测试了以下代码(来自ChatGpt
  6. ```csharp
  7. richTextBox1.Rtf = "";
  1. if (Clipboard.ContainsText(TextDataFormat.Rtf))
  2. {
  3. richTextBox1.Rtf = Clipboard.GetText(TextDataFormat.Rtf);
  4. }
  5. else if (Clipboard.ContainsText(TextDataFormat.Text))
  6. {
  7. richTextBox1.Text = Clipboard.GetText(TextDataFormat.Text);
  8. }

第一个代码,它仍然可以显示图片,但没有语法高亮。我不想使用这个。第二个代码,我和第一个做了相同的操作,我不记得代码的输出。

  1. <details>
  2. <summary>英文:</summary>
  3. How I can disable the rich text effect of `richtextbox`? I am using Windows Forms, C#, .Net Framework 4.8 and I am trying to make a Notepad app. And richtext effect is so bad.
  4. Example:
  5. ![Look](https://i.stack.imgur.com/oTcPN.png)
  6. That syntax highlighting and images.
  7. I test these (codes from ChatGpt)

richTextBox1.Rtf = "";

if (Clipboard.ContainsText(TextDataFormat.Rtf))
{
richTextBox1.Rtf = Clipboard.GetText(TextDataFormat.Rtf);
}
else if (Clipboard.ContainsText(TextDataFormat.Text))
{
richTextBox1.Text = Clipboard.GetText(TextDataFormat.Text);
}

  1. 1st code, it still can show images but not syntax highlighting. and I do not want use this. 2nd code, I did same with 1st I dont remember that outputs of code.
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. ```plaintext
  6. &quot;[RTF](https://en.wikipedia.org/wiki/Rich_Text_Format)&quot; 代表“富文本格式” - 文本加上格式(如颜色和字体)和图像。
  7. 如果你只对纯文本版本感兴趣,那就不要费心获取RTF版本,只需使用剪贴板数据的纯文本版本。
  8. 所以将那些 `if` 替换为只有
  9. ```csharp
  10. richTextBox1.Text = Clipboard.GetText(TextDataFormat.Text);

这样你只会从剪贴板中获取纯文本版本,并用它来填充你的 richTextBox 控件。

  1. <details>
  2. <summary>英文:</summary>
  3. &quot;[RTF](https://en.wikipedia.org/wiki/Rich_Text_Format)&quot; stands for &quot;Rich Text Format&quot; - text PLUS formatting (like colors and fonts) and images.
  4. If all you are interested in is the plain text version, then don&#39;t bother getting the RFT version, just use the plain text version of the clipboard&#39;s data.
  5. So replace those `if`s with just

richTextBox1.Text = Clipboard.GetText(TextDataFormat.Text);

  1. That way you only get the plain-text version from the clipboard and use that to fill your richTextBox control.
  2. </details>

huangapple
  • 本文由 发表于 2023年7月14日 01:16:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定