Clone a Font object

When you want to assign a control’s Font to another control, the first obvious way is to assign the Font property directly, as in:

Set Text2.Font = Text1.Font

but in most cases this approach doesn’t really work, because it assigns a reference to the same font to both controls. In other words, when you later change either control’s font, also the other is affected. What you really need most often is to clone a Font object and assign to the latter control a copy of the Font object used by the former.

The simplest way to clone a font is to manually copy all the individual Font properties, as in:

Function CloneFont(Font As StdFont) As StdFont    Set CloneFont = New StdFont    CloneFont.Name = Font.Name    CloneFont.Size = Font.Size    CloneFont.Bold = Font.Bold    CloneFont.Italic = Font.Italic    CloneFont.Underline = Font.Underline    CloneFont.Strikethrough = Font.StrikethroughEnd Function' example of usageSet Text2.Font = CloneFont(Text1.Font)

If you’re using VB6 you can rely on the PropertyBag object to quickly copy all Font properties back an forth. This version is more concise than the previous one and also runs twice as faster:

Function CloneFont(Font As StdFont) As StdFont    Dim pb As New PropertyBag    ' copy the font into a PropertyBag object    pb.WriteProperty "Font", Font    ' and then create a new Font object out of it    Set CloneFont = pb.ReadProperty("Font")End Function

But you can write even better code by using the hidden IFont interface, which is exposed by all StdFont object. This interface exposes a Clone method, which is exactly what you need. This method works in an unusual manner: it creates a cloned Font object and returns a reference to it in its only argument. This is the most concise code that you can write to leverage this feature:

Function CloneFont(Font As IFont) As StdFont    Font.Clone CloneFontEnd Function

This last version is the most concise and fastest of the three (it runs about 3 times faster than the one based on the PropertyBag object).

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a