Form's font not propogated to user-control

L

Lee

(Initially posted in microsoft.public.dotnet.framework.controls, but I
am posting it here after not having gotten a response after 3 days.)

Visual Studio 2005 + Windows XP (with all service packs)

I have a user control defined as follows:

==========
namespace ControlLibrary
{
public class LabelEx : System.Windows.Forms.Label
{
public LabelEx()
{
this.AutoSize = true;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
}
}
}
==========

When I drop this control on a Form and then change the Font on the
Form, the Font on this control does not change while the Font on the
other controls on the form (standard controls and other user controls)
does.


When I look at the Form.Designer.cs file, the Font for this control is
"MS Sans Serif" -- what the Form's font was before it was changed.


The question is why is the Font of this control not reflecting the
change in the Font on the Form?
 
B

Bob Powell [MVP]

Controls, including the standard label, will use the parent's font if the
child control's font has not been set. If you're object defines it's own
Font property then you'll have to duplicate this behaviour by referencing
the parent object if the local font is null.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
L

Lee

Bob:

Thanks for the response.

I showed all of the source for the user-control partly to show that it
does not do anything with the font. That is why I am puzzled -- the
user-control sets AutoSize and TextAlign but doesn't even reference any
other properties.
 
B

Bob Powell [MVP]

If you're defining thos properties yourself you'll need to do such things as
use default value attributes and provide a ShouldSerialize method for the
properties too.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
L

Lee

Bob:

Found the problem.... Somewhere along the way I explicitly set the Font
property to the default MS Sans Serif.

I dropped another instance of this user-control on the form, changed
the form's Font, and the change took affect as expected.

Thanks for assistance.
 

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

Top