112 c# wpf richtextbox wpf-controls
我正在尝试设置/获取我的RichTextBox的文本,但当我想获得test.Text时,Text不在其属性列表中...
我在C#中使用代码(.net framework 3.5 SP1)
RichTextBox test = new RichTextBox();
Run Code Online (Sandbox Code Playgroud)
不能有 test.Text(?)
你知道怎么可能吗?
sma*_*871 119
要设置 RichTextBox的文字:
richTextBox1.Document.Blocks.Clear();
richTextBox1.Document.Blocks.Add(new Paragraph(new Run("Text")));
Run Code Online (Sandbox Code Playgroud)
要获取 RichTextBox的文字:
string richText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
Run Code Online (Sandbox Code Playgroud)
小智 64
有RichTextBox中之间的混淆System.Windows.Forms的和System.Windows.Control
我正在使用控件中的那个,因为我正在使用WPF.在那里,没有Text属性,为了获得文本,我应该使用这一行:
string myText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text;
Run Code Online (Sandbox Code Playgroud)
谢谢
Eig*_*ite 38
在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;
Run Code Online (Sandbox Code Playgroud)
你可以使用这个AppendText方法,如果这就是你所追求的全部.
希望有所帮助.
小智 13
string GetString(RichTextBox rtb)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.Text;
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*ckx 11
TextWPF RichTextBox控件中没有属性.这是获取所有文本的一种方法:
TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd);
string allText = range.Text;
Run Code Online (Sandbox Code Playgroud)
Smi*_*ver 11
使用两种扩展方法,这变得非常简单:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
如何做到以下几点:
_richTextBox.SelectAll();
string myText = _richTextBox.Selection.Text;
Run Code Online (Sandbox Code Playgroud)
RichTextBox rtf = new RichTextBox();
System.IO.MemoryStream stream = new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(yourText));
rtf.Selection.Load(stream, DataFormats.Rtf);
Run Code Online (Sandbox Code Playgroud)
要么
rtf.Selection.Text = yourText;
Run Code Online (Sandbox Code Playgroud)
Jai*_*cia -12
据此它确实有一个 Text 属性
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members.aspx
如果您希望文本分成行,您也可以尝试“行”属性。
| 归档时间: |
|
| 查看次数: |
177029 次 |
| 最近记录: |