Joe M Jordan

In VB, how can I store the font information of a Label in an Access Data Base



Re: Visual Basic Language Store Forms.Label Font in a Data Base

TaDa

There are two basic ways:

1. Save enough of the property values so that you can recreate a font using those.

2. The Font class is Serializable, so you could serialize your Font object and save that in your database.





Re: Visual Basic Language Store Forms.Label Font in a Data Base

Joe M Jordan

I have very little knowledge and have never done a Serialization.

Would you give me an code example of how I would Serialize a label font property

How can I store it in a Access data base





Re: Visual Basic Language Store Forms.Label Font in a Data Base

SJWhiteley

This sounded like an interesting problem. Here's some code to serialize/deserialize a Font to a Memory Stream, to a byte array and a base64 string:

' Create a binary formatter

Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

' create a memory stream

Dim ms As New System.IO.MemoryStream()

' Serialize the FONT to a memory stream

formatter.Serialize(ms, Me.Font)

' Get the length of the serialized stream as an integer

Dim serializedLength As Integer = Convert.ToInt32(ms.Length)

' Create an array to store the bytes

Dim b(0 To serializedLength - 1) As Byte

' Read the memory stream into the byte array

ms.Position = 0

ms.Read(b, 0, serializedLength)

' Convert it to a base 64 string

Dim str As String = Convert.ToBase64String(b)

Have a SNEEK-PEEK

Debug.WriteLine(str)

' Convert it back to bytes

Dim b2() As Byte = Convert.FromBase64String(str)

' Write it to (another) memory stream

Dim ms2 As New System.IO.MemoryStream()

ms2.Position = 0

ms2.Write(b2, 0, b2.Length)

' Get an OBJECT from this memory stream

ms2.Position = 0

Dim obj As Object = formatter.Deserialize(ms2)

' Cast the object to a font

Dim f As Font = TryCast(obj, Font)

' Have a SNEEK-PEEK

If f IsNot Nothing Then

Debug.WriteLine(f)

Else

Debug.WriteLine("Cannot convert the stream back to a font")

End If

You could store it in the database as a BLOB or a string (Memo field )