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 can I format a text selection in bold AND italic?

How can I format a text selection in bold AND italic?

Scheduled Pinned Locked Moved C#
6 Posts 2 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.
  • U Offline
    U Offline
    User 11883281
    wrote on last edited by
    #1

    What the suject says... I can format the selection to bold and to italic. but when I want the text bold and italic the formation is set to regular automatically. Thats my code:

    newfont = FontStyle.Bold & FontStyle.Italic;
    richTextBox1.SelectionFont = new Font(currentfont.FontFamily, currentfont.Size, newfont);

    OriginalGriffO 1 Reply Last reply
    0
    • U User 11883281

      What the suject says... I can format the selection to bold and to italic. but when I want the text bold and italic the formation is set to regular automatically. Thats my code:

      newfont = FontStyle.Bold & FontStyle.Italic;
      richTextBox1.SelectionFont = new Font(currentfont.FontFamily, currentfont.Size, newfont);

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      & is AND - a binary operation that returns a 1 in each bit only if the corresponding bit in both operands is 1, and a zero otherwise. So if these are in Binary:

      0011 & 1010

      Will return 0010 because only the second bit has matching "1"s in the same position. The operation you want is OR : | which returns a 1 is either corresponding bit is 1.

      0011 | 1010

      Will return 1011. So try:

      newfont = FontStyle.Bold | FontStyle.Italic;

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      U 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        & is AND - a binary operation that returns a 1 in each bit only if the corresponding bit in both operands is 1, and a zero otherwise. So if these are in Binary:

        0011 & 1010

        Will return 0010 because only the second bit has matching "1"s in the same position. The operation you want is OR : | which returns a 1 is either corresponding bit is 1.

        0011 | 1010

        Will return 1011. So try:

        newfont = FontStyle.Bold | FontStyle.Italic;

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        U Offline
        U Offline
        User 11883281
        wrote on last edited by
        #3

        thanks a lot it works do you know how I can say that I will get the currentFontStyle without bold ? so if the selection is bold and italic and now i just want it in italic? i only want the code...

        OriginalGriffO 1 Reply Last reply
        0
        • U User 11883281

          thanks a lot it works do you know how I can say that I will get the currentFontStyle without bold ? so if the selection is bold and italic and now i just want it in italic? i only want the code...

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Use:

          existingStyle & ~FontStyle.Bold

          That turns the bit off.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          U 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Use:

            existingStyle & ~FontStyle.Bold

            That turns the bit off.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            U Offline
            U Offline
            User 11883281
            wrote on last edited by
            #5

            perfect thank you so much!

            OriginalGriffO 1 Reply Last reply
            0
            • U User 11883281

              perfect thank you so much!

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              You're welcome!

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              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