Discussion:
User Control Font Object
(too old to reply)
Mike Williams
2011-08-17 19:06:54 UTC
Permalink
I have been attempting to create a User Control in which I can expose a font
object to use for various things in the Control and I've been following the
procedure outlined at the following MSDN page:

http://msdn.microsoft.com/en-us/library/aa261313(v=vs.60).aspx

The code works in that if you drop such a User Control on a Form and select
it you can see the Font in its Properties and you can set it fine, with the
User Control immediately displaying the newly selected font. However, when
you then run the program the font on the User Control reverts to its default
setting. It is still possible to set its various font properties at runtime
in code, but I really would like it to use the font that was set by the user
at design time in the same way that other controls do. I'm obviously doing
something wrong and I wonder if anyone knows how to solve this problem?

The code in my User Control is as shown below.

Mike

Option Explicit
Private WithEvents mFont As StdFont

Private Sub UserControl_Initialize()
Set mFont = New StdFont
Set UserControl.Font = mFont
End Sub

Public Property Get Font() As StdFont
Set Font = mFont
End Property

Public Property Set Font(mnewFont As StdFont)
With mFont
.Bold = mnewFont.Bold
.Italic = mnewFont.Italic
.Name = mnewFont.Name
.Size = mnewFont.Size
End With
PropertyChanged "Font"
End Property

Private Sub mFont_FontChanged(ByVal PropertyName As String)
Set UserControl.Font = mFont
Refresh
End Sub

Private Sub UserControl_Paint()
Cls
Print "Hello"
End Sub
Nobody
2011-08-17 20:37:22 UTC
Permalink
Post by Mike Williams
I have been attempting to create a User Control in which I can expose a
font object to use for various things in the Control and I've been
http://msdn.microsoft.com/en-us/library/aa261313(v=vs.60).aspx
The code works in that if you drop such a User Control on a Form and
select it you can see the Font in its Properties and you can set it fine,
with the User Control immediately displaying the newly selected font.
However, when you then run the program the font on the User Control
reverts to its default setting. It is still possible to set its various
font properties at runtime in code, but I really would like it to use the
font that was set by the user at design time in the same way that other
controls do. I'm obviously doing something wrong and I wonder if anyone
knows how to solve this problem?
The code in my User Control is as shown below.
You need to supply these events:

UserControl_InitProperties
UserControl_ReadProperties
UserControl_WriteProperties

There is an easy way to do that without writing code. Use "VB6 ActiveX Ctrl
Interface Wizard" Add-in, which generates the code for you and without
deleting your UserControl code. Just start the Wizard, and click Next(You
can keep clicking on Next till the end if you wish). If you don't like what
it generated, and want to try again, just press Ctrl+Z several times. It
adds the most common properties and methods for you so you don't have to add
them one by one. The right list includes the properties and methods that you
want to add. Add or remove what you want. By default "Font" property is
included. In "Set Mapping" stage, you can map some of these properties and
method to a control inside the UserControl. For example, if you have added a
Label control inside the UserControl, you can tell the wizard to map the
UserControl BackColor to the Label's BackColor, and it will generate the
necessary code. By default it maps to the UserControl properties, so you
don't have to select anything. You can run the wizard again and add more
properties later if you want. Here is what it generates if I just select the
Font property:

Option Explicit
'Property Variables:
Dim m_Font As Font


'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
'MemberInfo=6,0,0,0
Public Property Get Font() As Font
Set Font = m_Font
End Property

Public Property Set Font(ByVal New_Font As Font)
Set m_Font = New_Font
PropertyChanged "Font"
End Property

'Initialize Properties for User Control
Private Sub UserControl_InitProperties()
Set m_Font = Ambient.Font
End Sub

'Load property values from storage
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

Set m_Font = PropBag.ReadProperty("Font", Ambient.Font)
End Sub

'Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)

Call PropBag.WriteProperty("Font", m_Font, Ambient.Font)
End Sub
Mike Williams
2011-08-17 23:22:02 UTC
Permalink
Post by Mike Williams
http://msdn.microsoft.com/en-us/library/aa261313(v=vs.60).aspx
The code works in that if you drop such a User Control on a Form
and select it you can see the Font in its Properties and you can set
it fine <smip> However, when you then run the program the font on
the User Control reverts to its default setting. I'm obviously doing
something wrong and I wonder if anyone knows how to solve this
You need to supply these events: UserControl_InitProperties
UserControl_ReadProperties
UserControl_WriteProperties
There is an easy way to do that without writing code. Use "VB6
ActiveX Ctrl Interface Wizard" Add-in, which generates the code
for you . . . . .
Thanks Nobody. I didn't know about that wizard. I've just tried it now and
unfortunately it does not solve my problem. It generates code very similar
to the code I was already using (I left out a couple of Subs in my original
post for brevity and because nothing I placed in there solved the problem).
Basically it behaved in the same way as my existing code, losing the details
of any font that may have been selected in the IDE when the project was run.
Try it and you'll see. In the end it was Karl's suggestion of using the User
Control's Ambient property which solved the problem. I'm very grateful to
you for responding though, and of course to Karl for providing the solution.
Thanks a lot to both of you. I've learnt something very useful today ;-)

Mike

Karl E. Peterson
2011-08-17 20:59:31 UTC
Permalink
Post by Mike Williams
The code works in that if you drop such a User Control on a Form and select
it you can see the Font in its Properties and you can set it fine, with the
User Control immediately displaying the newly selected font. However, when
you then run the program the font on the User Control reverts to its default
setting. It is still possible to set its various font properties at runtime
in code, but I really would like it to use the font that was set by the user
at design time in the same way that other controls do. I'm obviously doing
something wrong and I wonder if anyone knows how to solve this problem?
As Nobody suggested, you need to persist the design-time choice.
Something like this, maybe...

Private m_Font As StdFont

Private Sub UserControl_InitProperties()
Set Me.Font = Ambient.Font
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Set Me.Font = PropBag.ReadProperty("Font", Ambient.Font)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("Font", m_Font, Ambient.Font)
End Sub

Public Property Get Font() As StdFont
Set Font = m_Font
End Property

Public Property Set Font(ByVal New_Font As StdFont)
Set m_Font = New_Font
Set UserControl.Font = m_Font
PropertyChanged "Font"
End Property
--
.NET: It's About Trust!
http://vfred.mvps.org
Mike Williams
2011-08-17 21:36:00 UTC
Permalink
Post by Karl E. Peterson
As Nobody suggested, you need to persist the design-time choice.
Something like this, maybe... [code snipped]
Thanks Karl. I had actually added code to the UserControl_ReadProperties and
WriteProperties Subs which already contains code for other things I was
doing, but nothing I placed there for fonts solved the problem and so for
brevity I left it out of the code I posted. It was your suggestion of using
the the Ambient property which did the trick. I very rarely get involved
with UserControls and I did not know about that. I suppose I should have
spent more time investigating the various properties in the help files, but
by the time I had decided to post here I had tried all sorts of stuff for
hours and hours and my head was beginning to spin ;-) Thank you, Karl. It
is working fine now. Thank you to Nobody as well for his solution. I'll have
a look at that later.

Mike
Karl E. Peterson
2011-08-17 22:00:44 UTC
Permalink
Post by Mike Williams
As Nobody suggested, you need to persist the design-time choice. Something
like this, maybe... [code snipped]
Thanks Karl. I had actually added code to the UserControl_ReadProperties and
WriteProperties Subs which already contains code for other things I was
doing, but nothing I placed there for fonts solved the problem and so for
brevity I left it out of the code I posted. It was your suggestion of using
the the Ambient property which did the trick. I very rarely get involved with
UserControls and I did not know about that. I suppose I should have spent
more time investigating the various properties in the help files, but by the
time I had decided to post here I had tried all sorts of stuff for hours and
hours and my head was beginning to spin ;-) Thank you, Karl. It is working
fine now. Thank you to Nobody as well for his solution. I'll have a look at
that later.
Y'know, that's one I'm not sure I've ever seen written up anywhere,
either. It's a great thing to know about, as your control will inherit
the default font of the parent object. (The docs lie, doesn't matter
what the Container's font is.)
--
.NET: It's About Trust!
http://vfred.mvps.org
Loading...