Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to programmatically change text to underlined style in RichTextBox?

How to programmatically change text to underlined style in RichTextBox?

Scheduled Pinned Locked Moved C#
helptutorialquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SeeBees
    wrote on last edited by
    #1

    Can anybody help me in this? thank you in advance.

    L 1 Reply Last reply
    0
    • S SeeBees

      Can anybody help me in this? thank you in advance.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, this is a two-step process: 1. select the text that needs special handling; you can do that interactively, or programmatically; while it already is in place, or while adding. 2. choose a new font for the selected text (RichTextBox.SelectionFont Property) and use FontStyle.Underline for it. For all the details, read up on the RichTextBox and Font classes. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, this is a two-step process: 1. select the text that needs special handling; you can do that interactively, or programmatically; while it already is in place, or while adding. 2. choose a new font for the selected text (RichTextBox.SelectionFont Property) and use FontStyle.Underline for it. For all the details, read up on the RichTextBox and Font classes. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


        S Offline
        S Offline
        SeeBees
        wrote on last edited by
        #3

        Thanks. Well there's a problem. What if my selection includes multiple fonts? RichTextBox.SelectionFont Property returns null in that case. How can I underline my selection without changing the original fonts?

        L M 2 Replies Last reply
        0
        • S SeeBees

          Thanks. Well there's a problem. What if my selection includes multiple fonts? RichTextBox.SelectionFont Property returns null in that case. How can I underline my selection without changing the original fonts?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, if multiple styles are selected, the selection has to be split up into homogeneous parts; select and underline each part in turn; then restore the original selection. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


          S 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, if multiple styles are selected, the selection has to be split up into homogeneous parts; select and underline each part in turn; then restore the original selection. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


            S Offline
            S Offline
            SeeBees
            wrote on last edited by
            #5

            Thank you. :-D Your suggestion worked. I splitted my multi-font-selection into several homogeneous parts, selected each part and underlined them one after another. Finally I restored the original selection. However, as I try out the program, it flickers.

            L 1 Reply Last reply
            0
            • S SeeBees

              Thank you. :-D Your suggestion worked. I splitted my multi-font-selection into several homogeneous parts, selected each part and underlined them one after another. Finally I restored the original selection. However, as I try out the program, it flickers.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              SeeBees wrote:

              it flickers

              sure, all Controls basically have that problem, and the more data they hold and the more operations you perform on them, the more it shows. Here are some suggestions to reduce the flickering, you pick and choose: - optimize your code; tha faster it runs the less you will notice it; - try using SuspendLayout() and ResumeLayout(); I'm not sure how well it helps with an RTB; - difficult: get the Rtf property, operate on it (that's string operations now, not very elegant), and when done, store it again - alternative: have a second RTB, not visible, copy the Rtf from the first to the second, operate on the second RTB, then copy back the Rtf. This should work nicely. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


              S 1 Reply Last reply
              0
              • S SeeBees

                Thanks. Well there's a problem. What if my selection includes multiple fonts? RichTextBox.SelectionFont Property returns null in that case. How can I underline my selection without changing the original fonts?

                M Offline
                M Offline
                mav northwind
                wrote on last edited by
                #7

                Hi! I guess this article[^] could help...

                Regards, mav -- Black holes are the places where God divided by 0...

                S 1 Reply Last reply
                0
                • M mav northwind

                  Hi! I guess this article[^] could help...

                  Regards, mav -- Black holes are the places where God divided by 0...

                  S Offline
                  S Offline
                  SeeBees
                  wrote on last edited by
                  #8

                  This one is really helpful, thank you!

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    SeeBees wrote:

                    it flickers

                    sure, all Controls basically have that problem, and the more data they hold and the more operations you perform on them, the more it shows. Here are some suggestions to reduce the flickering, you pick and choose: - optimize your code; tha faster it runs the less you will notice it; - try using SuspendLayout() and ResumeLayout(); I'm not sure how well it helps with an RTB; - difficult: get the Rtf property, operate on it (that's string operations now, not very elegant), and when done, store it again - alternative: have a second RTB, not visible, copy the Rtf from the first to the second, operate on the second RTB, then copy back the Rtf. This should work nicely. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                    S Offline
                    S Offline
                    SeeBees
                    wrote on last edited by
                    #9

                    :) Thanks for all the suggestions you give!!!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups