Tips for Using RichTextBox in WPF (translation and Arrangement)

Source: Internet
Author: User

Here: http://blogs.msdn.com/jfoscoding/archive/2006/01/14/512825.aspx

Here, only the knowledge points are sorted out:
1. Obtain the selected content:
(1) Use the RichTextBox. Document. Selection attribute
(2) Access text in the "blocks" of the RichTextBox. Document. Blocks attribute

2. add content to RichTextBox in XAML:
<RichTextBox isspellcheckenabled = "true">
<Flowdocument>
<Paragraph>
<! -- Add your content here -->
This is a RichTextBox. I can <bold> bold </bold>, <italic> italicize </italic>, </Paragraph>
</Flowdocument>
</RichTextBox>

3. Shorten the segment spacing, similar to <br>, instead of <p>
You can use style to define the segment Spacing:
<RichTextBox>
<RichTextBox. Resources>
<Style targettype = "{X: Type paragraph}">
<Setter property = "margin" value = "0"/>
</Style>
</RichTextBox. Resources>
<Flowdocument>
<Paragraph>
This is my first paragraph... see how there is...
</Paragraph>
<Paragraph>
A No space anymore between it and the second paragraph?
</Paragraph>
</Flowdocument>
</RichTextBox>

4. Read the plain text file from the file and put it into RichTextBox or directly put the text into RichTextBox:Private void loadtextfile (RichTextBox, string filename)
{
RichTextBox. Document. blocks. Clear ();
Using (streamreader = file. opentext (filename )){
Paragraph paragraph = new paragraph ();
Paragraph. Text = streamreader. readtoend ();
RichTextBox. Document. blocks. Add (paragraph );
}
}

Private void loadtext (RichTextBox, string txtcontent)
{
RichTextBox. Document. blocks. Clear ();
Paragraph paragraph = new paragraph ();
Paragraph. Text = txtcontent;
RichTextBox. Document. blocks. Add (paragraph );
}5. Get the content of the specified RichTextBox:
Private string gettext (RichTextBox)
{
Textrange = new textrange (RichTextBox. Document. contentstart, RichTextBox. Document. contentend );
Return textrange. text;
}6. Place the Rich Text Format In the RichTextBox:Private Static void loadrtf (string RTF, RichTextBox)
{
If (string. isnullorempty (RTF )){
Throw new argumentnullexception ();
}
Textrange = new textrange (RichTextBox. Document. contentstart, RichTextBox. Document. contentend );
Using (memorystream rtfmemorystream = new memorystream ()){
Using (streamwriter rtfstreamwriter = new streamwriter (rtfmemorystream )){
Rtfstreamwriter. Write (RTF );
Rtfstreamwriter. Flush ();
Rtfmemorystream. Seek (0, seekorigin. Begin); // load the memorystream into textrange ranging from start to end of RichTextBox.
Textrange. Load (rtfmemorystream, dataformats. rtf );
}
}
}7. Load the content in the file as the content of RichTextBox
Private Static void LoadFile (string filename, RichTextBox)
{
If (string. isnullorempty (filename )){
Throw new argumentnullexception ();
}
If (! File. exists (filename )){
Throw new filenotfoundexception ();
}
Using (filestream stream = file. openread (filename )){
Textrange documenttextrange = new textrange (RichTextBox. Document. contentstart, RichTextBox. Document. contentend );
String dataformat = dataformats. text;
String ext = system. Io. Path. getextension (filename );
If (string. Compare (EXT, ". XAML", true) = 0 ){
Dataformat = dataformats. XAML;
}
Else if (string. Compare (EXT, ". rtf", true) = 0 ){
Dataformat = dataformats. rtf;
}
Documenttextrange. Load (stream, dataformat );
}
}8. Save the content of RichTextBox as a file:
Private Static void SaveFile (string filename, RichTextBox)
{
If (string. isnullorempty (filename )){
Throw new argumentnullexception ();
}
Using (filestream stream = file. openwrite (filename )){
Textrange documenttextrange = new textrange (RichTextBox. Document. contentstart, RichTextBox. Document. contentend );
String dataformat = dataformats. text;
String ext = system. Io. Path. getextension (filename );
If (string. Compare (EXT, ". XAML", true) = 0 ){
Dataformat = dataformats. XAML;
}
Else if (string. Compare (EXT, ". rtf", true) = 0 ){
Dataformat = dataformats. rtf;
}
Documenttextrange. Save (stream, dataformat );
}
}9. Make a simple Editor:
<! -- Window1.xaml -->
<Dockpanel>
<Menu dockpanel. Dock = "TOP">
<Menuitem header = "_ file">
<Menuitem header = "_ open file" Click = "onopenfile"/>
<Menuitem header = "_ save" Click = "onsavefile"/>
<Separator/>
<Menuitem header = "e_xit" Click = "onexit"/>
</Menuitem>
</Menu>
<RichTextBox name = "richtextbox1"> </RichTextBox>
</Dockpanel>
// Window1.xaml. CS
Private void onexit (Object sender, eventargs e ){
This. Close ();
}
Private void onopenfile (Object sender, eventargs e ){
Microsoft. win32.openfiledialog ofd = new Microsoft. win32.openfiledialog ();
Ofd. Filter = "text files (*. txt; *. XAML; *. rtf) | *. txt; *. XAML; *. rtf ";
Ofd. multiselect = false;
If (OFD. showdialog () = true ){
LoadFile (OFD. safefilename, richtextbox1 );
}}
Private void onsavefile (Object sender, eventargs e ){
Microsoft. win32.savefiledialog SFD = new Microsoft. win32.savefiledialog ();
SFD. Filter = "text files (*. txt; *. XAML; *. rtf) | *. txt; *. XAML; *. rtf ";
If (SFD. showdialog () = true ){
SaveFile (SFD. safefilename, richtextbox1 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.