Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Clone a Font object

Total Hit ( 2897)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
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:

Click here to copy the following block
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.Strikethrough
End Function

' example of usage
Set 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:

Click here to copy the following block
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:

Click here to copy the following block
Function CloneFont(Font As IFont) As StdFont
  Font.Clone CloneFont
End 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).



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.