Floating FB sharing byReview Results

Floating FB sharing byReview Results

  • WPF Tutorial

    • # Read in Your Language

    WPF Controls

    Tutorial Home | Layouts | Containers | Button | CheckBox and CheckedListBox | ComboBox | DateTimePicker and MonthCalendar | DataGrid | Label and LinkLabel | ListBox | ListView | TextBox and MaskedTextBox | PictureBox and ProgressBar | TreeView | WebBrowser | Menus,Status and Toolbar | RadioButton | RichTextBox | ToolTip and Scrolling | Custom Controls

    RichTextBox control in WPF


    Namespace : System.Windows.Controls.RichTextBox

    RichTextBox may contain any combination of
    • Unicode text.
    • UIElement. Image, a nested Text, etc.
    • ContentElement. LineBreak, etc.
    • TextElement. Bold, Paragraph, etc – content that spans other content. TextElements like Bold (anything that derives from Inline) are essentially platforms for DependencyProperties applied to spanned content. TextElements like Paragraph (things that derive from BlockElement) may hold property values, but always provide structural information used by the layout engine.

    The RichTextBox operates on top of a Document object, which allows you to manipulate the text in far more powerful ways than what was exposed in Windows Forms.
    A flow document is designed to "reflow content" depending on window size, device resolution, and other environment variables. In addition, flow documents have a number of built in features including search, viewing modes that optimize readability, and the ability to change the size and appearance of fonts.

    Designer Code:

              <Paragraph>Text in paragraph one.</Paragraph>
              <Paragraph>Text in paragraph two.</Paragraph>

            <RichTextBox Margin="10,10,0,0" Name="RichTextBox1" 
            HorizontalAlignment="Left" VerticalAlignment="Top" Width="184" 
            Height="215" TextChanged="RichTextBox1_TextChanged">
                        <Bold>Text in Paragraph one</Bold>
                    <<Paragraph Foreground="Red">
                        Text in Paragraph Two.

    Code Behind:

            // Window1.xaml.cs
    private void RichTextBox1_TextChanged(object sender, TextChangedEventArgs e) { TextRange textRange = new TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd); // MessageBox.Show(textRange.Text); } //Adding text from code FlowDocument fldoc = new FlowDocument(); // Create a paragraph with text Paragraph para1 = new Paragraph(); para1.Inlines.Add(new Bold(new Run("sampletext"))); fldoc.Blocks.Add(para1); RichTextBox1.Document = fldoc;

    The RichTextBox operates on top of a Document object, which allows you to manipulate the text in far more powerful ways than what was exposed in Windows Forms.

    That said it’s more difficult to work with. To load in text from a file:

    Loading Documents in RichTextBox:

            RichTextBox rtb = new RichTextBox();
            string text = File.OpenText(@"c:\temp\sample.txt").ReadToEnd();
            rtb.Document.Blocks.Clear(); //if you want to clear before adding text
            rtb.Document.Blocks.Add(new Paragraph(new Run(text)));

            TextRange range;
            System.IO.FileStream fStream;
            if (System.IO.File.Exists(fileName))
                range = new TextRange(RichTextBox1.Document.ContentStart, 
                fStream = new System.IO.FileStream(fileName, 
                range.Load(fStream, System.Windows.DataFormats.Text );

    Spell checking functionality of RichTextBox :

    Supports only four languages(English, Spanish, German and French), have to specify the language.

             <RichTextBox SpellCheck.IsEnabled="True" Language="en-US" />

    UI controls in RichTextBox :


    Strikethrough text in RichTextBox :

             <Paragraph TextDecorations="Strikethrough">
                Text to Strikethrough.

    << Previous >> | << Next >>