Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6384553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:47:24+00:00 2026-05-25T02:47:24+00:00

In a WPF .NET 4.0 RichTextBox with the following text: Apple Cheese Orange Pear

  • 0

In a WPF .NET 4.0 RichTextBox with the following text:

Apple

Cheese

Orange

Pear

Chicken

How would I programmatically with C#, (not with XAML markup), bold all lines that start with the character “C”?

More generally, how do you get a reference to a given line of text from a RichTextBox and then apply some formatting to it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:47:24+00:00Added an answer on May 25, 2026 at 2:47 am

    Well that was trickier than I expected but I think the code below does it:

            foreach (var paragraph in richTextBox1.Document.Blocks)
            {
                var text = new TextRange(paragraph.ContentStart,
                               paragraph.ContentEnd).Text;
    
                paragraph.FontWeight = text.StartsWith("C") ?
                               FontWeights.Bold : FontWeights.Normal;
            }
    

    Basically, the RichTextBox holds its content in a FlowDocument (accessed through the Document property), which in turn has a collection of Blocks containing each Paragraph. Actually, each item in the Blocks collection can be anything derived from the abstract class Block…but I’m assuming if you only ever add simple text to your RichTextBox then they’ll always just be Paragraphs. See here for a better explanation!

    The trickiest part is that to get the text out of the paragraph you need to use the TextRange class…but the good news is that, once we have the text, the Paragraph has simple properties on it for setting the font weight, etc!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF .NET 4 C# RichTextBox and I'm wanting to replace certain
I have the following code that adds a background worker into a VB.net WPF
I have quastion about DataGrid in WPF .NET 4. Here is XAML code with
We are currently looking to create a text-editor in WPF (.NET 4.0) which will
Effect 's do not seem to work on the WebBrowser control in WPF (.NET
In our WPF (.NET 4.5) application we would like to unify the look of
So In WPF I can declare a Rich Text Box like so <RichTextBox Text={Binding
I have a new VB.Net WPF application. The MainWindow.xaml contains nothing more than a
Using the excellent Microsoft Ribbon for WPF for .NET 4 (not the .NET 4.5
Problem is described and demonstrated on the following links: Paul Stovell WPF: Blurry Text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.