[VB.Net] Changer polices, couleurs et style des contrôles
Tout est dans le titre.
Comme d'habitude VB.Net a des réactions bizarre, quand la police d'un contrôle est changée les styles sont réinitialiser, il faut donc les redéfinir.
Agencez une forme comme le modèle ci-dessus.
Le 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
Téléchargement
Télécharger la solution démo
Serveur 1 : StyleTexte(2).zip
Ce document intitulé « [VB.Net] Changer polices, couleurs et style des contrôles » issu de Comment Ça Marche (www.commentcamarche.net) est mis à disposition sous les termes de la licence Creative Commons. Vous pouvez copier, modifier des copies de cette page, dans les conditions fixées par la licence, tant que cette note apparaît clairement.