PDA

View Full Version : Passing values to a Font object



mikerickson
10-17-2011, 03:06 PM
I am using Class modules to make a custom object, for use in a userform.

This object has many Labels, all with the same properties.

My object has properties like BackColor and the Property Let routines apply the input values to all the labels. So that code like this gets the expected results
'in UserForm module
Dim myObject as New Class1
' ...
myObject.BackColor = RGB(100,200,100)


'in Class module
Dim myColor as OLE_Color

Property Let BackColor(newColor As OLE_Color)
myColor = newColor
Label1.BackColor = myColor
Label2.BackColor = myColor
' etc.
End PropertyThis works well, until I come to the Font property.

My objective is that a line in the userform module
'in uf module

LineAlpha:
myObject.Font.Size = 12 will set the font size of all the labels to 12.

The problem is how to do that.
I can define a property

'in class module
Dim myFont as NewFont

Property Get Font() as NewFont
Set Font = myFont
End Property

Property Set Font(aFont as NewFont)
Set myFont = aFont

Label1.Font.Size = myFont.Size
Label2.Font.Size = myFont.Size
Label1.Font.Bold = myFont.Bold

' etc.
End Property

The problem with that approach is that LineAlpha calls the Property Get code, not the Property Set.

I could write a custom MyFont object with properties like Bold, Charset, etc. and Class1's Font property would return a MyFont object.
In that case, LineAlpha would call a Property Get routine from Class1 and a Property Let routine from MyFont.
The Property Let routines in MyFont could raise an event that would get the values into the labels.

Or am I missing something, is there another way that LineAlpha could trigger the passing of Size=12 down to the lables without creating a MyFont class?

Bob Phillips
10-17-2011, 04:54 PM
You shouldn't need to do that Mike, you should be able to access the object's properties.

What is NewFont? When I replaced that with Object it seemed to work fine for me.

mikerickson
10-17-2011, 05:38 PM
Look at the attached. LineAlpha is in the userforms TextBox2.AfterUpdate event.

I don't see how to get the result (both labels changing to the same font size)
by changing the Font property of my custom object, without the extra class clsDistributiveFont.

I strongly want to be able to keep the syntax myObject.Font.Size = newSize. I want all the machinery behind that result to be in the custom class, no extra requirements on the UF developer. (developer level "user friendliness")

Aflatoon
10-18-2011, 02:47 AM
Does it have to be:
myObject.Font.Size = newSize
rather than:
myObject.FontSize = newSize
?

mikerickson
10-18-2011, 07:44 AM
^^ Yes.
I seek to make my custom control as close to a built in control as possible.

(Managing what is shown in the Object Browser has led me to some interesting choices.)
I guess that this is part of how VBA set up inheritance of properties.

Oh, BTW, an overview of what I'm doing is here
http://www.ozgrid.com/forum/showthread.php?t=159123&page=2

If I were on a Windows machine, I could use an extra control, but on my Mac I have to write one. (Aww shucks, :type )

Aflatoon
10-18-2011, 08:46 AM
Shame - Access controls use Fontbold rather than Font.Bold but I presume you wish to be consistent with Excel? ;) (especially if you are on a Mac)

I will have a think - does it need to be compatible with 2004 and prior? (not sure what that version did not support as I currently only have 2011 on my Mac, but can install 2004 for testing)

mikerickson
10-18-2011, 09:22 AM
I have both 2011 and 2004. Apparently 2004 does not support RaiseEvent statements, so the entire issue probably a no-go on that platform.

Aflatoon
10-18-2011, 09:43 AM
To be honest, I think you will need the extra class to do it with that syntax. A UDT won't work, and passing the Font of one of the controls wouldn't provide any method of signalling a change required for the others, that I can think of at the moment, anyway.

mikerickson
10-18-2011, 09:53 AM
Well, if I really want to get fussy, I can merge the two class modules.
And use different instances differntly
One instance would be the custom control that the user sees. (with a bunch of font type code that never gets called)
The other instance would be the myFont object. (with a bunch of fmControl code that never gets called)