Click here to Skip to main content
14,450,535 members
Rate this:
Please Sign up or sign in to vote.
See more:
It need to display one word in one font and next line other font .
In Given sample program the last set font is applied for the entire text

What I have tried:

RichTextBox.Font = new Font("Arial", 14, FontStyle.Bold);
           RichTextBox.Text = Form1.FN+" "+Form1.LN+"\n";
           RichTextBox.Font = new Font("Arial", 10);
           RichTextBox.Text +=("Mobile: " )+ Form1.M;
Updated 22-Sep-18 6:58am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You might want to read the documentation on the RichTextBox control[^]. There, you'll find various properties beginning with "Selection". Use the SelectionStart and SelectionLength properties where you can select text and then use the other properties to make changes to the selected text, such as SelectionFont.
49R 22-Sep-18 12:35pm
RichTextBox.SelectionFont = new Font("Arial", 14, FontStyle.Bold);
RichTextBox.AppendText(Form1.FN + " " + Form1.LN + "\n");
RichTextBox.SelectionFont = new Font("Arial", 10);
RichTextBox.AppendText("Mobile: " + Form1.M);

got it Thank you
Dave Kreskowiak 22-Sep-18 13:24pm
Great. You have now a much bigger problem.

You're creating new Font objects every time this code runs. Font object hold onto unmannaged, and limited, GDI resources. You never dispose of these created objects, but you created them without any upper limit. This means your code leaks resources and will, if done enough, will crash Windows.

Create your Font objects at the class level and you can reuse them, over and over, without creating new ones. Then, when your app exists, you can call Dispose on those Font objects and properly release the resources they are holding onto.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100