Results 1 to 7 of 7

Thread: Using Custom Fonts

  1. #1

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Using Custom Fonts

    In my forms load event I placed the following code to load and use a custom font I purchased (all licensed and good to go for projects):

    IO.File.WriteAllBytes("test.ttf", My.Resources.RaintreeBold)
    Dim pfc As New PrivateFontCollection()
    pfc.AddFontFile("test.ttf")
    MyLabel.Font = New Font(pfc.Families(0), 14)

    When I run the program I get a "Parameter is not Valid" general error, but not as the code above is executed. It happens on an Update() or .ShowDialog() call right before the window is displayed.

    What is truly odd is that sometimes the code works; I see my label with the correct font. Then I run the program again and it doesn't having changed nothing. Then I run it again and the error occurs again.

    From what I have researched the "Parameter is not Valid" error is happening during the GDI drawing of the form. Is the font somehow lost after it is initially loaded in the Load event? What am I doing wrong here for what should be a basic task?

    Thanks in advance.
    Intermediate Level Programmer Extraordinaire

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,632

    Re: Using Custom Fonts

    The fact that the result is intermittent success, my first thought is that there is some sort of timing issue where the newly written font file is sometimes being accessed before the file has been completely created.

    To test that theory out, if you put some sort of a delay in between the font file being created and it being accessed you might see more consistent results.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Using Custom Fonts

    I'd probably also put in a check for the font first, rather than blindly copying it every time. Once it's there, there really shouldn't be a need to copy it again. It's possible (remotely) that it's that additional copying that could be the issue.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Custom Fonts

    I'm wondering why a resource is being extracted to a file in the first place. Are you trying to stop people making copies of the font file? If not, why not distribute the font as a file in the first place and forget the resource?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: Using Custom Fonts

    Quote Originally Posted by jmcilhinney View Post
    I'm wondering why a resource is being extracted to a file in the first place. Are you trying to stop people making copies of the font file? If not, why not distribute the font as a file in the first place and forget the resource?
    I get the same error whether I extract to a file or use AddFontFile(). I tried both hoping one would work, but neither did.
    Intermediate Level Programmer Extraordinaire

  6. #6

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: Using Custom Fonts

    Quote Originally Posted by techgnome View Post
    I'd probably also put in a check for the font first, rather than blindly copying it every time. Once it's there, there really shouldn't be a need to copy it again. It's possible (remotely) that it's that additional copying that could be the issue.

    -tg
    I suspect this might be the cause. How do I create a check for the font?
    Intermediate Level Programmer Extraordinaire

  7. #7

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: Using Custom Fonts

    So this is a thread from months ago that I wanted to update in case anyone else encounters the same problem.

    Upon returning to this problem I found a simple fix. I changed my PrivateFontCollection object (pfc) to a private variable in the form. No more error!
    Intermediate Level Programmer Extraordinaire

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width