1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Public Class FormCustomFont2
Private fnt As Font = New Font("arial", 12.0, FontStyle.Bold) 'default font
Private path As String = Directory.GetCurrentDirectory() + "\fnt.bin"
'button change font
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If FontDialog1.ShowDialog() = DialogResult.OK Then
fnt = FontDialog1.Font
End If
TextBox1.Font = fnt
End Sub
Private Sub FormCustomFont2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fnt = DeSerializeFont(fnt)
TextBox1.Text = "Bonsoir tous le monde." _
& "Suite au développement d'une application, je permet à l'utilisateur de modifier la police, le style etc etc etc de la form." _
& " Ce que j'aimerai ensuite c'est pouvoir sauvegarder dans un fichier le choix de l'utilisateur et le re-appliquer à la nouvelle ouverture du logiciel."
TextBox1.Font = fnt
End Sub
Private Sub FormCustomFont2_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
SerializeFont(fnt)
End Sub
Private Sub SerializeFont(ByVal thefont As Font)
Dim fb As New BinaryFormatter
Using ms As New FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)
fb.Serialize(ms, thefont)
End Using
End Sub
Private Function DeSerializeFont(ByVal thefont As Font) As Font
If Not File.Exists(path) Then Return thefont 'return the default font
Dim fb As New BinaryFormatter
Using ms As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
thefont = CType(fb.Deserialize(ms), Font)
End Using
Return thefont
End Function
End Class |
Partager