Clone a Font object

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as