Comment obtenir du RTF à partir de RichTextBox
-
03-07-2019 - |
Question
Comment obtenir le texte RTF d'un RichTextBox
? J'essaie d'obtenir cela, mais la propriété n'existe pas.
RichTextBox rtb = new RichTextBox();
string s = rtb.Rtf;
La solution
Pour obtenir le XAML réel créé par l'utilisateur à l'intérieur de la RichTextBox:
TextRange tr = new TextRange(myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
tr.Save(ms, DataFormats.Xaml);
string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());
EDIT: je n'ai pas de code devant moi pour tester, mais une instance du type TextRange
a une méthode Save
(pour diffuser) qui prend une paramètre DataFormats
, qui peut être DataFormats.Rtf
Autres conseils
Il existe 2 classes RichTextBox, une du framework winforms et une du framework WPF:
System.Windows.Controls.RichTextBox wpfBox;
System.Windows.Forms.RichTextBox winformsBox;
Seul Winforms RichTextBox possède une propriété Rtf, l'autre possède une propriété Document contenant un FlowDocument.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow