VB.Net - Endre fonter, farger og stil av kontroller

Intro

Som vanlig har VB.Net merkelige reaksjoner når skrifttypen til en kontroll endres, stilene tilbakestilles og de skal omdefineres.

For å håndtere dette problemet:

Koden

 Public Class Form1 Private Sub Form1_Load (ByVal sender som System.Object, ByVal e As System.EventArgs) Håndterer MyBase.Load Dim jeg som Byte, L, S som helhet Dim FonteSys som FontFamily TextBox1.Text = "Si tu te cognes à un (Confucius) "S = TextBox1.Font.Size For i = 8 til 24 trinn 2 ComboBox1.Items.Add (i) Hvis jeg = S Så L = ComboBox1.Items.Count - 1 Neste Prøv ComboBox1.SelectedIndex = L Fang ut som unntak ComboBox1.SelectedIndex = ComboBox1.Items.Count - 1 End Prøv 'Proprietés de la boite de dialog 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 'Preeksjon de la couleur For hver FonteSys I System.Drawing.FontFamily. Familier CombPolice.Items.Add (FonteSys.Name) Hvis TextBox1.Font.Name = FonteSys.Name Så L = CombPolice.Ite ms.Count - 1 Neste Prøv CombPolice.SelectedIndex = L Fang ex som unntak ComboBox1.SelectedIndex = 0 Slutt Prøv End Sub Private Sub ComboBox1_SelectedIndexChanged (ByVal sender som System.Eventil, ByVal e As System.EventArgs) Håndterer ComboBox1.SelectedIndexChanged Dim oldFont As Font = TextBox1.Font Dim newFont Som Font = Ny skrifttype (oldFont.FontFamily, Val (ComboBox1.Text)) TextBox1.Font = newFont ChangeStyle () End Sub Private Sub CheckBox1_CheckedChanged_1 (ByVal sender som System.Object, ByVal e As System. EventArgs) Håndterer CheckBox1.CheckedChanged, _ CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged ChangeStyle () 'Autre syntax' Dim stil Som FontStyle = IIf (TextBox1.Font.Style og FontStyle.Bold, TextBox1.Font.Style Xor FontStyle .Bold, TextBox1.Font.Style eller FontStyle.Bold) 'TextBox1.Font = Ny skrift (TextBox1.Font, stil) Slutt Sub Private Sub Button1_Click (ByVal sender som System.Object, ByVal e As System.EventArgs) Håndterer Button1. Klikk Hvis BoiteCouleur.ShowDialog () = Di alogResult.OK Så TextBox1.ForeColor = BoiteCouleur.Color End Hvis 'Autre syntax' TextBox1.ForeColor = Color.FromArgb (& HFF0000) Slutt Sub Private Sub Button2_Click (ByVal sender som System.Object, ByVal e As System.EventArgs) Håndterer Button2. Klikk Hvis BoiteCouleur.ShowDialog () = DialogResult.OK Så TextBox1.BackColor = BoiteCouleur.Color End Hvis End Sub Private Sub CombPolice_SelectedIndexChanged (ByVal sender som System.Object, ByVal e As System.EventArgs) Håndterer CombPolice.SelectedIndexChanged Hvis TextBox1.Font. Navn CombPolice.Text Then TextBox1.Font = Nytt System.Drawing.Font (CombPolice.Text, CInt (ComboBox1.Text)) ChangeStyle () Slutt Hvis End Sub Sub ChangeStyle () Dim Style Som FontStyle = FontStyle.Bold Og CheckBox1.Checked Eller FontStyle.Italic og CheckBox2.Checked _ eller FontStyle.Underline og CheckBox3.Checked eller FontStyle.Strikeout og _ CheckBox4.Checked 'Pas nécessaire à chaque fois. 'TextBox1.Font = Nytt System.Drawing.Font (CombPolice.Text, CInt (ComboBox1.Text)) TextBox1.Font = Ny skrifttype (TextBox1.Font, stil) End Sub End Class 

nedlasting

Last ned demo-løsningen

Server 1: her

Forrige Artikkel Neste Artikkel

Beste Tips