VB.Net - Change fonts, colors and style of controls

September 2017



Intro


As usual VB.Net has strange reactions when the font of a control is changed, the styles are reset and they should be redefined.

To handle this issue:

The code


Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Byte, L, S As Integer
        Dim FonteSys As FontFamily

        TextBox1.Text = "Si tu te cognes à un pot et que ça sonne creux, c'est pas forcément le pot qui est vide. ;-)(Confucius)"
        S = TextBox1.Font.Size
        For i = 8 To 24 Step 2
            ComboBox1.Items.Add(i)
            If i = S Then L = ComboBox1.Items.Count - 1
        Next

        Try
            ComboBox1.SelectedIndex = L
        Catch ex As Exception
            ComboBox1.SelectedIndex = ComboBox1.Items.Count - 1
        End Try
        'Propriétés de la boite de dialogue couleur
        'BoiteCouleur.SolidColorOnly = True  'Couleurs unies
        'BoiteCouleur.AllowFullOpen = True  'Affiche le bouton des couleurs personnalisées
        'BoiteCouleur.FullOpen = True  'Affiche les couleurs personnalisées
        'BoiteCouleur.Color = Color.Red  'Préselection de la couleur

        For Each FonteSys In System.Drawing.FontFamily.Families
            CombPolice.Items.Add(FonteSys.Name)
            If TextBox1.Font.Name = FonteSys.Name Then L = CombPolice.Items.Count - 1
        Next
        Try
            CombPolice.SelectedIndex = L
        Catch ex As Exception
            ComboBox1.SelectedIndex = 0
        End Try
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim oldFont As Font = TextBox1.Font
        Dim newFont As Font = New Font(oldFont.FontFamily, Val(ComboBox1.Text))
        TextBox1.Font = newFont
        ChangeStyle()
    End Sub

    Private Sub CheckBox1_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, _
        CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged
        ChangeStyle()
        'Autre syntaxe
        'Dim style As FontStyle = IIf(TextBox1.Font.Style And FontStyle.Bold, TextBox1.Font.Style Xor FontStyle.Bold, TextBox1.Font.Style Or FontStyle.Bold)
        'TextBox1.Font = New Font(TextBox1.Font, style)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If BoiteCouleur.ShowDialog() = DialogResult.OK Then
            TextBox1.ForeColor = BoiteCouleur.Color
        End If
        'Autre syntaxe
        'TextBox1.ForeColor = Color.FromArgb(&HFF0000)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If BoiteCouleur.ShowDialog() = DialogResult.OK Then
            TextBox1.BackColor = BoiteCouleur.Color
        End If
    End Sub

    Private Sub CombPolice_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CombPolice.SelectedIndexChanged
        If TextBox1.Font.Name <> CombPolice.Text Then
            TextBox1.Font = New System.Drawing.Font(CombPolice.Text, CInt(ComboBox1.Text))
            ChangeStyle()
        End If
    End Sub
    Sub ChangeStyle()
        Dim style As FontStyle = FontStyle.Bold And CheckBox1.Checked Or FontStyle.Italic And CheckBox2.Checked _
                                 Or FontStyle.Underline And CheckBox3.Checked Or FontStyle.Strikeout And _
                                 CheckBox4.Checked
        'Pas nécessaire à chaque fois.
        'TextBox1.Font = New System.Drawing.Font(CombPolice.Text, CInt(ComboBox1.Text))
        TextBox1.Font = New Font(TextBox1.Font, style)
    End Sub
End Class

Download


Download the demo solution
Server 1:here

Related

Published by deri58. Latest update on November 21, 2011 at 12:30 PM by deri58.
This document, titled "VB.Net - Change fonts, colors and style of controls," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).