[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

User Control Font Object

Mike Williams

8/17/2011 7:07:00 PM

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/librar...(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


5 Answers

(nobody)

8/17/2011 8:37:00 PM

0

"Mike Williams" <Mike@WhiskyAndCoke.com> wrote in message
news:j2h6hi$smr$1@dont-email.me...
>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/librar...(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







Karl E. Peterson

8/17/2011 9:00:00 PM

0

Mike Williams brought next idea :
> 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://vfre...


Mike Williams

8/17/2011 9:36:00 PM

0

"Karl E. Peterson" <karl@exmvps.org> wrote in message
news:j2ha3l$kul$1@dont-email.me...

> 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

8/17/2011 10:01:00 PM

0

Mike Williams presented the following explanation :
> "Karl E. Peterson" <karl@exmvps.org> wrote in message
> news:j2ha3l$kul$1@dont-email.me...
>
>> 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://vfre...


Mike Williams

8/17/2011 11:22:00 PM

0

"Nobody" <nobody@nobody.com> wrote in message
news:j2h8q7$i6v$1@speranza.aioe.org...
> "Mike Williams" <Mike@WhiskyAndCoke.com> wrote in message
>> I've been following the procedure outlined at the following MSDN page:
>> http://msdn.microsoft.com/en-us/librar...(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