ByRef ?? wny not working??

kimosavi

Active member
Joined
Apr 9, 2009
Messages
34
Location
Chicago, IL
Programming Experience
3-5
Hi. I am trying to change values in all the objects on my form.

First off I call my function when the form loads

SetFonts(me)

VB.NET:
Public Sub SetFonts(ByRef Frm As Form)
        For Each Ctr As Object In Frm.Controls
            ChangeFont(Ctr)
        Next
    End Sub

Public Sub ChangeFont(ByRef Ctr As Object)
        For x As Integer = 0 To FontList.Families.Length - 1
            If Ctr.Font.FontFamily.Name = FontList.Families(x).Name Then
                Ctr.Font = New Font(FontList.Families(x), Ctr.Font.Size)
            End If
        Next
        If Ctr.Controls.Count > 0 Then
            For Each Itm As Object In Ctr.Controls
                ChangeFont(Itm)
            Next
        End If
    End Sub

if I write the following code on the form works:

Label1.Font = New Font(FontList.Families(0), 10)

I can even do this

Dim Lbl as label = me.Label1
Lbl.Font = New New Font(FontList.Families(0), 10)

Both work ok. What is wrong with the functions?!?

your help is hightly appreciated. Thanks!
 
You aren't changing anything though:
VB.NET:
        For x As Integer = 0 To FontList.Families.Length - 1
            If Ctr.Font.FontFamily.Name = FontList.Families(x).Name Then
                Ctr.Font = New Font(FontList.Families(x), Ctr.Font.Size)
            End If
        Next
You're creating a new Font with the same family and the same size as the existing Font, so what are you expecting to change?

Also, there's no need or point to declaring either of those method parameters ByRef. You aren't assigning new values to the parameters so ByRef has no value.
 
my appologies, I didn't explain the whole process completely. I must have looked like an arse.

FontList is a Private Font Collection of Embedded Fonts.

My PC have TTF that my clients don't have, so instead of assigning each one individually I just design the forms and assign the fonts from my PC. By embedding them is like carrying the TTF with the App, but they don't exist so I reassign embeeded font at runtime.

two more declarations

VB.NET:
    Public FontArray() As String = {"Amienne.ttf", "AmienneBold.ttf", "AntiquaSSK.ttf", "Carolingia.ttf", "YiBaiti.ttf", "Centaur.ttf"}
    Public FontList As Drawing.Text.PrivateFontCollection = GetFont(FontArray)

GetFont gets the fonts from the Assemby at runtime.

I did a little more testing and the functions seems to be working fine, I replaced Ctr.Font.Size with 30 and ALL my object in the form blew up.

Seems it's something to do with the Embedded fonts functions.

I need to review this in more details.

Thank you for your reply, I will post back the whole thing when I figure out what was wrong.

Any Thoughts appreciated!

Thanks.
 
I did some further testing...

This function I downloaded from the net and loads all the Embedded fonts from memory into the Fonts object (private font collection)

I change the code to use the AddFontFile and works great in XP and Vista

but the embedded method is not working. The font seems to get changed but it is not showing on screen. Does it has to do with the Drawing namespace?

if i change the font in the form it works for XP but not for Vista, XP shows the font on screen even if not installed on the PC, but Vista doesn't.

Any thoughts?


VB.NET:
    Public Function GetFont(ByVal FontResource() As String) As Drawing.Text.PrivateFontCollection
        Dim NameSpc As String = Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()
        Dim FntStrm As IO.Stream
        Dim FntFC As New Drawing.Text.PrivateFontCollection()
        Dim i As Integer
        For i = 0 To FontResource.GetUpperBound(0)
            FntStrm = Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(NameSpc + "." + FontResource(i))
            Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte
            FntStrm.Read(ByteStrm, 0, Int(CType(FntStrm.Length, Integer)))
            Dim FntPtr As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(GetType(Byte)) * ByteStrm.Length)
            Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, FntPtr, ByteStrm.Length)
            FntFC.AddMemoryFont(FntPtr, ByteStrm.Length)
            Dim pcFonts As Int32
            pcFonts = 1
            AddFontMemResourceEx(FntPtr, ByteStrm.Length, 0, pcFonts)
            Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr)
        Next
        Return FntFC
    End Function
 
Back
Top