RichTextBox (WPF) 没有字符串属性“Text”

发布于 2024-07-23 11:12:33 字数 239 浏览 2 评论 0原文

我正在尝试设置/获取 RichTextBox 的文本,但当我想要获取 test.Text 时,Text 不在其属性列表中...

我在 C# (.net Framework 3.5 SP1) 中使用后面的代码

RichTextBox test = new RichTextBox();

不能有 < code>test.Text(?)

你知道它怎么可能吗?

I am trying to set/get the text of my RichTextBox, but Text is not among list of its properties when I want to get test.Text...

I am using code behind in C# (.net framework 3.5 SP1)

RichTextBox test = new RichTextBox();

cannot have test.Text(?)

Do you know how come it can be possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

上课铃就是安魂曲 2024-07-30 11:12:33

设置 RichTextBox 文本:

richTextBox1.Document.Blocks.Clear();
richTextBox1.Document.Blocks.Add(new Paragraph(new Run("Text")));

获取 RichTextBox 文本:

string richText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;

to set RichTextBox text:

richTextBox1.Document.Blocks.Clear();
richTextBox1.Document.Blocks.Add(new Paragraph(new Run("Text")));

to get RichTextBox text:

string richText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
一江春梦 2024-07-30 11:12:33

System.Windows.Forms< 中的 RichTextBox 之间存在混淆/a> 和 System.Windows.Control

我使用的是控件中的那个,就像我使用 WPF 一样。 在那里,没有 Text 属性,为了获取文本,我应该使用这一行:

string myText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text; 

谢谢

There was a confusion between RichTextBox in System.Windows.Forms and in System.Windows.Control

I am using the one in the Control as I am using WPF. In there, there is no Text property, and in order to get a text, I should have used this line:

string myText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text; 

thanks

楠木可依 2024-07-30 11:12:33

WPF RichTextBox 有一个 Document 属性,用于设置内容 a MSDN:

// Create a FlowDocument to contain content for the RichTextBox.
        FlowDocument myFlowDoc = new FlowDocument();

        // Add paragraphs to the FlowDocument.
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
        RichTextBox myRichTextBox = new RichTextBox();

        // Add initial content to the RichTextBox.
        myRichTextBox.Document = myFlowDoc;

您可以只使用 AppendText 方法,但如果这就是您所需要的后。

希望有帮助。

The WPF RichTextBox has a Document property for setting the content a la MSDN:

// Create a FlowDocument to contain content for the RichTextBox.
        FlowDocument myFlowDoc = new FlowDocument();

        // Add paragraphs to the FlowDocument.
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
        RichTextBox myRichTextBox = new RichTextBox();

        // Add initial content to the RichTextBox.
        myRichTextBox.Document = myFlowDoc;

You can just use the AppendText method though if that's all you're after.

Hope that helps.

断舍离 2024-07-30 11:12:33

使用两种扩展方法,这变得非常容易:

public static class Ext
{
    public static void SetText(this RichTextBox richTextBox, string text)
    {
        richTextBox.Document.Blocks.Clear();
        richTextBox.Document.Blocks.Add(new Paragraph(new Run(text)));
    }

    public static string GetText(this RichTextBox richTextBox)
    {
        return new TextRange(richTextBox.Document.ContentStart,
            richTextBox.Document.ContentEnd).Text;
    }
}

Using two extension methods, this becomes very easy:

public static class Ext
{
    public static void SetText(this RichTextBox richTextBox, string text)
    {
        richTextBox.Document.Blocks.Clear();
        richTextBox.Document.Blocks.Add(new Paragraph(new Run(text)));
    }

    public static string GetText(this RichTextBox richTextBox)
    {
        return new TextRange(richTextBox.Document.ContentStart,
            richTextBox.Document.ContentEnd).Text;
    }
}
染火枫林 2024-07-30 11:12:33

WPF RichTextBox 控件中没有 Text 属性。 这是获取所有文本的一种方法:

TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd);

string allText = range.Text;

There is no Text property in the WPF RichTextBox control. Here is one way to get all of the text out:

TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd);

string allText = range.Text;
不交电费瞎发啥光 2024-07-30 11:12:33
string GetString(RichTextBox rtb)
{
    var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    return textRange.Text;
}
string GetString(RichTextBox rtb)
{
    var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    return textRange.Text;
}
笑,眼淚并存 2024-07-30 11:12:33
RichTextBox rtf = new RichTextBox();
System.IO.MemoryStream stream = new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(yourText));

rtf.Selection.Load(stream, DataFormats.Rtf);

或者

rtf.Selection.Text = yourText;
RichTextBox rtf = new RichTextBox();
System.IO.MemoryStream stream = new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(yourText));

rtf.Selection.Load(stream, DataFormats.Rtf);

OR

rtf.Selection.Text = yourText;
森林迷了鹿 2024-07-30 11:12:33

只执行以下操作怎么样:

_richTextBox.SelectAll();
string myText = _richTextBox.Selection.Text;

How about just doing the following:

_richTextBox.SelectAll();
string myText = _richTextBox.Selection.Text;
〗斷ホ乔殘χμё〖 2024-07-30 11:12:33

“扩展 WPF 工具包”现在提供了带有 Text 属性的 Richtextbox。

您可以获取或设置不同格式的文本(XAML、RTF 和纯文本)。

以下是链接:扩展 WPF 工具包 RichTextBox

"Extended WPF Toolkit" now provides a richtextbox with the Text property.

You can get or set the text in different formats (XAML, RTF and plaintext).

Here is the link: Extended WPF Toolkit RichTextBox

请你别敷衍 2024-07-30 11:12:33

令我惊讶的是,RichtTextBox 没有返回与设置相同的值!

设置字符串 使用:

SelectAll()
RichTextBox.Selection.Text = "AA"

并返回:

SelectAll()
Return RichTextBox.Selection.Text

返回带回车的“AA”

还使用:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd)
Selection.Text = "AA"

并返回:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd)
Return Selection.Text

执行相同操作: 带回车的“AA”

RichTextBox 不返回设置的值
非常不正确的行为!!

通过以下方式解决(规避)这个问题:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd.GetPositionAtOffset(-1))
Return Selection.Text

To my big surprise the RichtTextBox does not return the same value as was set !

Setting a string With:

SelectAll()
RichTextBox.Selection.Text = "AA"

And returning with:

SelectAll()
Return RichTextBox.Selection.Text

Returns "AA" with carriage-return

Also using:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd)
Selection.Text = "AA"

And returning with:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd)
Return Selection.Text

Does the same: "AA" with carriage-return

The RichTextBox does not return the value as set
Very incorrect behaviour !!

Is solved (circumvented) this by:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd.GetPositionAtOffset(-1))
Return Selection.Text
青春有你 2024-07-30 11:12:33

据此,它确实有一个 Text 属性

http:// /msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members.aspx

如果您希望将文本分解为行,也可以尝试使用“Lines”属性。

According to this it does have a Text property

http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members.aspx

You can also try the "Lines" property if you want the text broken up as lines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文