将文本转换为RichTextFormat [英] Converting Text to RichTextFormat

查看:328
本文介绍了将文本转换为RichTextFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个解决方案,而不是一个问题。



我发现在任何地方都没有提到这个,所以最终提出了我自己的解决方案。令人讨厌的是,richtextbox不允许在其richtextbox.RTF属性中使用纯文本。因此,即使将richtextbox.RTF中的值放入另一个框中,然后将这些内容放回richtextbox.RTF也不起作用。



这是转换代码(有点解决方法)将纯文本转换为富文本格式:



 私有 函数 ConvertTextToRTF( ByVal  RTFtext 作为 字符串作为 字符串 
Dim data_object As DataObject
data_object.SetData(DataFormats.Rtf,RTFtext)
返回 data_object.GetData(DataFormats.Rtf)
结束 功能





使用如下:

  Dim  text < span class =code-keyword> as   string  =   Hello World 
richtextbox.RTF = ConvertTextToRTF(text)

解决方案

好的,我不明白。为什么使用 RTF 属性作为纯文本?为什么不使用 Text 属性?



 richTextBox1.Text =  纯文本; 


你可能需要了解richtext和纯文本之间的一些区别。

富文本。



富文本仅使用8位格式允许它只使用unicode字符。然而,例如逃逸序列。 \u会告诉转换器该怎么做。



要将纯文本转换为RTF,您需要以纯文本形式包含approrpriate rtf标签,以使它们在rtf中工作。



请参见此处 [ ^ ]进行更详细的分析。


我试过发布的解决方案通过'shaneapp',但我从来没有让它工作,我甚至直接复制代码,但当我尝试保存文件时,它会给我一个无效的文件格式错误。 
经过实验,我偶然发现了这个非常简单的解决方案:

< pre>昏暗的文字As String =Scott
'通过做SelectedText - 它将它转换为RichTextFormat ...
''(因为你将能够看到你是否打开文件...
''生成如下 - 它包含rtf格式代码。
RichTextBox1.SelectedText = text
RichTextBox1.SaveFile(C:\ Test \ test1.rtf)


This is a solution, not a question.

I've found no mention of this anywhere, so eventually came up with my own solution. Annoyingly enough, the richtextbox does not allow plain text in its richtextbox.RTF property. So even placing the value from richtextbox.RTF into another box, then placing those contents back into the richtextbox.RTF does not work.

Here is the conversion code (somewhat of a workaround) that converts plain text into a richtext format:

Private Function ConvertTextToRTF(ByVal RTFtext As String) As String
      Dim data_object As New DataObject
      data_object.SetData(DataFormats.Rtf, RTFtext)
      Return data_object.GetData(DataFormats.Rtf)
End Function



Use as follows:

Dim text as string = "Hello World"
richtextbox.RTF = ConvertTextToRTF(text)

解决方案

Okay, I don't get it. Why use the RTF property for plain text? Why not use the Text property?

richTextBox1.Text = "Plain text";


You might need to understand some of the differences between richtext and plain text.
Rich text.

Rich text uses only an 8 bit format which allows it to use only unicode characters. However escape sequences for e.g. \u will tell the converter what to do.

To convert plain text to RTF, you will need to include the approrpriate rtf tags in plain text to make them work in rtf.

See here[^] for a more detailed analysis.


I tried the solution posted by 'shaneapp', but I never got it to work, I even Copied the Code directly, but when I tried to Save the File, it would give me an "Invalid file format" error.
After experimenting, I stumbled across this very easy solution that works:

<pre>   Dim text As String = "Scott"
        ' By doing SelectedText - it converts it to RichTextFormat ...
        '' (As you will be able to see if you Open the file ...
        '' generated below - it contains the rtf format codes.
        RichTextBox1.SelectedText = text
        RichTextBox1.SaveFile("C:\Test\test1.rtf")


这篇关于将文本转换为RichTextFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆