Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42225

Changing Fonts at Runtime.

$
0
0
Hi, I'd like an opinion on the following code...

vb.net Code:
  1. Private Sub Face(ByVal a As Integer)
  2.         'Style 0 regular, 1 Bold, 2 Italic, 3 Bold Italic.
  3.  
  4.  
  5.         If a = 1 Or a = 2 Then
  6.             Label2.Font = GFR(My.Resources.MTCORSVA, 48, 2)
  7.             Label3.Font = GFR(My.Resources.MTCORSVA, 34, 2)
  8.             Label4.Font = GFR(My.Resources.MTCORSVA, 34, 2)
  9.         Else
  10.             Label2.Font = GFR(My.Resources.albanyzc, 36, 3)
  11.             Label3.Font = GFR(My.Resources.albanyc, 28, 0)
  12.             Label4.Font = GFR(My.Resources.albanyc, 28, 0)
  13.         End If
  14.  
  15.  
  16.     End Sub                  'Set Type face.
  17.  
  18.     Private Function GFR(ByVal FontResource As Byte(), _
  19.                           ByVal Size As Integer, _
  20.                           ByVal Style As FontStyle) As Drawing.Font
  21.  
  22.         Dim Fc As New Drawing.Text.PrivateFontCollection
  23.         Fc.AddMemoryFont(Runtime.InteropServices.Marshal. _
  24.                                      UnsafeAddrOfPinnedArrayElement(FontResource, 0), _
  25.                                      FontResource.Length)
  26.         Return New Drawing.Font(Fc.Families(0), Size, Style)
  27.  
  28.     End Function        'Get Font from Resource

The font files are in Resources, and also in C:\Windows\Fonts.

I've been using this code for a couple of years or so, but every now and then something in the project causes the Labels to malfunction, in that they're just the outline of where they should be with no text, and crossed out in red, two red lines from opposite corners, corner to corner. (That's to the best of my memory, it doesn't happen all that often and my memory isn't what it once was).

Strangely, if I run the project again, even immediately after closing it when it malfunctioned, it'll display correctly. This problem occurs fairly often on my wife's laptop but quite rarely on my PC. This project is called from the Start Menu so all sorts of other things are likely to be happening at the same time, so I'm trying to eliminate this part of the equation.



Poppa.

Viewing all articles
Browse latest Browse all 42225

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>