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 set SelectionFont when it returns null?

How to set SelectionFont when it returns null?

Scheduled Pinned Locked Moved C#
questionhelptutorial
4 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.
  • R Offline
    R Offline
    rvp717y
    wrote on last edited by
    #1

    Hi there, I've got a standard RTF control and am coding a basic text formatter. In the event the user selects a section of text which has multiple fonts contained in it, SelectionFont will return null - that's documented and accepted behaviour. However - how do I now set the font for the selected text to the new font in that case? All the examples I've found use variations on the theme of: richTextBox1.SelectionFont = new Font( currentFont.FontFamily, currentFont.Size, newFontStyle); which clearly won't work if richTextBox1.SelectionFont == null. Please help! Kind regards, John.

    L 1 Reply Last reply
    0
    • R rvp717y

      Hi there, I've got a standard RTF control and am coding a basic text formatter. In the event the user selects a section of text which has multiple fonts contained in it, SelectionFont will return null - that's documented and accepted behaviour. However - how do I now set the font for the selected text to the new font in that case? All the examples I've found use variations on the theme of: richTextBox1.SelectionFont = new Font( currentFont.FontFamily, currentFont.Size, newFontStyle); which clearly won't work if richTextBox1.SelectionFont == null. Please help! Kind regards, John.

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

      Hi, you can set it to any font you like: rtb.SelectionFont=new Font("Arial", 24, FontStyle.Bold); if you want it to resemble the original font(s), you might: - remember the selection start and length - apply a new selection, inside the original one (e.g. the first char) so you can investigate what font that was using - maybe iterate the previous step for different selection parts - restore the original selection - now apply a new font, based on what you discovered :)

      Luc Pattyn [My Articles] [Forum Guidelines]

      R M 2 Replies Last reply
      0
      • L Luc Pattyn

        Hi, you can set it to any font you like: rtb.SelectionFont=new Font("Arial", 24, FontStyle.Bold); if you want it to resemble the original font(s), you might: - remember the selection start and length - apply a new selection, inside the original one (e.g. the first char) so you can investigate what font that was using - maybe iterate the previous step for different selection parts - restore the original selection - now apply a new font, based on what you discovered :)

        Luc Pattyn [My Articles] [Forum Guidelines]

        R Offline
        R Offline
        rvp717y
        wrote on last edited by
        #3

        Thanks Luc, I was really hoping that something so complex wasn't required! Kind regards, John.

        1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, you can set it to any font you like: rtb.SelectionFont=new Font("Arial", 24, FontStyle.Bold); if you want it to resemble the original font(s), you might: - remember the selection start and length - apply a new selection, inside the original one (e.g. the first char) so you can investigate what font that was using - maybe iterate the previous step for different selection parts - restore the original selection - now apply a new font, based on what you discovered :)

          Luc Pattyn [My Articles] [Forum Guidelines]

          M Offline
          M Offline
          Muammar
          wrote on last edited by
          #4

          That was clever, I've always admired your logic Luc!


          Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

          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