Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C# and RichTextBoxes

This thread is locked; no one can reply to it. rss feed Print
C# and RichTextBoxes
23yrold3yrold
Member #1,134
March 2001
avatar

Can someone tell me how you set the default font on one of these bastards? There's no font member to just assign a value to like most controls and every way I've tried to assign a new font family gets ignored. For the record, I need it to display in Verdana. Because it doesn't. Like, ever.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

akderebur
Member #13,217
August 2011

You can create a new font like this:
Font verdanaFont = new Font("Verdana", 20f, FontStyle.Regular);

You can change Verdana to anything you want: Arial, Arial Narrow, Comic Sans MS ...
Change the number before f to change the size.
Change font style to anything you want: Bold, Italic, Underline ...

For setting default font just write the code after InitializeComponent. For example:

using System.Drawing;

public Form1()
{
InitializeComponent();
Font verdanaFont = new Font("Verdana", 20f, System.Drawing.FontStyle.Regular);
richTextBox1.SelectionFont = verdanaFont;

}

So when you start typing the font will be Verdana.

23yrold3yrold
Member #1,134
March 2001
avatar

System.Windows.Forms.RichTextBox has a SelectionFont member. System.Windows.Controls.RichTextBox (which the program is using) does not. :(

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

BAF
Member #2,981
December 2002
avatar

Umm.... richtextbox.FontFamily? ???

akderebur
Member #13,217
August 2011

richTextBox1.FontFamily = new FontFamily("Verdana");

23yrold3yrold
Member #1,134
March 2001
avatar

Tried that, and various other things with a FontFamily member (like the richtextbox's Document). Doesn't have any effect.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Neil Walker
Member #210
April 2000
avatar

What about FontFamily property, or am I missing something obvious :)

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

23yrold3yrold
Member #1,134
March 2001
avatar

I'm pretty sure I've tried everything obvious, but feel free to talk to me like I'm a five year old, because I feel like one right now. :P

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Neil Walker
Member #210
April 2000
avatar

Quote:

This property only affects a control whose template uses the FontFamily property as a parameter. On other controls, this property has no impact.

RichTextBox has such a parameter so it should work.

http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

akderebur
Member #13,217
August 2011

I am not much into WPF applications but this code should work:
richTextBox1.FontFamily = new FontFamily("Verdana");

Just debug your program using each of these:
richTextBox1.FontFamily = new FontFamily("Verdana");
richTextBox1.FontFamily = new FontFamily("Arial Narrow");
richTextBox1.FontFamily = new FontFamily("Comic Sans MS");

You will see the difference.

23yrold3yrold
Member #1,134
March 2001
avatar

Neil Walker: So you're referring to setting the property in the XML? Or using ApplyPropertyValue()?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

BAF
Member #2,981
December 2002
avatar

Are you loading RTF into the box after attempting to set the font? According to msdn, setting that property (whether it be via XML, direct in code, or by whatever other method) will change the whole document, as well as changing the current font.

Go to: