我正在尝试设置/获取RichTextBox的文本,但是当我想要获取test.Text时,Text不在其属性列表中...
我正在使用C#(.net framework 3.5 SP1)的代码后端。
RichTextBox test = new RichTextBox();
无法使用 test.Text(?)
你知道可能的原因吗?
我正在尝试设置/获取RichTextBox的文本,但是当我想要获取test.Text时,Text不在其属性列表中...
我正在使用C#(.net framework 3.5 SP1)的代码后端。
RichTextBox test = new RichTextBox();
无法使用 test.Text(?)
你知道可能的原因吗?
设置 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;
在 System.Windows.Forms 和 System.Windows.Control 中,RichTextBox 之间存在混淆。
由于我正在使用 WPF,因此我使用的是 Control 中的 RichTextBox。在那里,没有 Text 属性,为了获取文本,我应该使用这行代码:
string myText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text;
谢谢
WPF 的 RichTextBox 有一个名为 Document
的属性,用于设置内容,类似于 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
方法就可以实现你想要的功能。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;
}
}
WPF RichTextBox控件中没有Text
属性。以下是一种获取所有文本的方法:
TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd);
string allText = range.Text;
string GetString(RichTextBox rtb)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.Text;
}
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.SelectAll();
string myText = _richTextBox.Selection.Text;
rtxb_input.SelectAll();
txb_InputLength.Text = rtxb_input.Selection.Text.Length.ToString();
- Marty_in_a_BoxRichtTextBox
返回的值与设置的值不同!使用以下代码设置字符串: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
Paragraph()
有一个Paragraph(Inline)
的重载至少从 .NET 3.5 开始(而且Run(string)
也是有效的 - 它甚至在示例中)。 - Dragomok