Font change ...

H

Hariharan S

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(object sender, System.EventArgs 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.
 
M

Morten Wennevik

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

Noah Coad [MCP & MVP]

Hey Hariharan,

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

private void btnBold_Click(object sender, System.EventArgs e)
{
rtf.SelectionFont = new Font(rtf.Font, FontStyle.Bold);
}

Good luck!

-Noah Coad
Microsoft MVP & MCP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top