Font change ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hariharan S

    #1

    Font change ...

    Hi Guys,

    Have an issue with the Font object.

    I have a text box in which I can enter text and I have included a small
    button which upon selection, should enable me to change the newly entered
    text into bold. But it is not what is happening. All the previous
    information entered in the regular font too gets changed to bold. The code
    on the button click event handler is as follows ...

    Note: A Font object is created as a Font object cannot be assigned values.

    public void btnBold_Click(o bject sender, System.EventArg s e)
    {
    Font oldFont = Font;
    FontStyle newStyle = Font.Style ^ FontStyle.Bold;
    Font = new Font(oldFont, newStyle);
    oldFont.Dispose ();
    }

    This is a code copied from Mickey Williams' Visual C# .NET text book (see
    page 470). What I require is only the text that is going to be entered in
    the text box after the selection of the btnBold should appear as 'bold' and
    the earlier text already present in the textbox. The textbox used is
    'RichTextBox' as the regular text box does not provide these facilities.

    Example: a RichTextBox below ...
    ---------------------------------------------------------------------------
    | This is just to demonstrate what I expect. Let us say, I want the text
    |
    | to be bold from this point
    |
    ---------------------------------------------------------------------------

    The moment, I type any character after the ' point ' in the above
    RichTextBox after selecting the 'bold' option, all the characters from the '
    This ' too gets changed to bold. That is something, I do not want.

    Any help on this would be appreciated. Awaiting response at the earliest and
    would be grateful.

    p.s. Do not want to use the FontDialog to change my Font.
    ---

    Regards,
    Hariharan S



  • Morten Wennevik

    #2
    Re: Font change ...

    Are you using a richtexbox yourself?
    If you use a regular textbox the font applies to all text.

    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

    Comment

    • Noah Coad [MCP & MVP]

      #3
      Re: Font change ...

      Hey Hariharan,

      You would use the RichTextBox's "SelectionF ont" property to accomplish your
      task. Say your RichTextBox was named "rtf", then use something like this
      code:

      private void btnBold_Click(o bject sender, System.EventArg s e)
      {
      rtf.SelectionFo nt = new Font(rtf.Font, FontStyle.Bold) ;
      }

      Good luck!

      -Noah Coad
      Microsoft MVP & MCP


      "Hariharan S" <freespirit@tel evital.com> wrote in message
      news:exSlHocnDH A.1960@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Hi Guys,
      >
      > Have an issue with the Font object.
      >
      > I have a text box in which I can enter text and I have included a small
      > button which upon selection, should enable me to change the newly entered
      > text into bold. But it is not what is happening. All the previous
      > information entered in the regular font too gets changed to bold. The code
      > on the button click event handler is as follows ...
      >
      > Note: A Font object is created as a Font object cannot be assigned values.
      >
      > public void btnBold_Click(o bject sender, System.EventArg s e)
      > {
      > Font oldFont = Font;
      > FontStyle newStyle = Font.Style ^ FontStyle.Bold;
      > Font = new Font(oldFont, newStyle);
      > oldFont.Dispose ();
      > }
      >
      > This is a code copied from Mickey Williams' Visual C# .NET text book (see
      > page 470). What I require is only the text that is going to be entered in
      > the text box after the selection of the btnBold should appear as 'bold'[/color]
      and[color=blue]
      > the earlier text already present in the textbox. The textbox used is
      > 'RichTextBox' as the regular text box does not provide these facilities.
      >
      > Example: a RichTextBox below ...
      > --------------------------------------------------------------------------[/color]
      -[color=blue]
      > | This is just to demonstrate what I expect. Let us say, I want the[/color]
      text[color=blue]
      > |
      > | to be bold from this point
      > |
      > --------------------------------------------------------------------------[/color]
      -[color=blue]
      >
      > The moment, I type any character after the ' point ' in the above
      > RichTextBox after selecting the 'bold' option, all the characters from the[/color]
      '[color=blue]
      > This ' too gets changed to bold. That is something, I do not want.
      >
      > Any help on this would be appreciated. Awaiting response at the earliest[/color]
      and[color=blue]
      > would be grateful.
      >
      > p.s. Do not want to use the FontDialog to change my Font.
      > ---
      >
      > Regards,
      > Hariharan S
      >
      >
      >[/color]


      Comment

      Working...