C# and RichTextBoxes
23yrold3yrold

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.

akderebur

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

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

BAF

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

akderebur

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

23yrold3yrold

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

Neil Walker

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

23yrold3yrold

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

Neil Walker
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

akderebur

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

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

BAF

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.

Thread #608069. Printed from Allegro.cc