Changer la taille du text écrit dans un TextB

Salut ,

j'ai un petit problème et je veux votre aide svp les amis :'(
je suis débutant en VB.NET , et je veux réaliser un petit programme qui permet de changer la taille du texte écrit dans un textBox ( 8px , 12px , 18px ) , et aussi de changer le Style entre Gras et Italic ( deux CheckBox ) .

mon problème c'est que :

1) quand j'écris mon texte , et je coche la case Italic, après je coche la case Gras , je veux que mon texte soit en Gras et Italic en même temps

2) je vx changer la taille du texte quand je sélectionne une taille dans un ComboBox ( 8px , 12px , ou 18px ) ,

Voilà mon code :

Code Visual Basic :
Public Class Form1
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        'traitement pour les tailles 8px , 12px et 18px
        'pour : 8px
        Dim taille As Single
        If ComboBox1.SelectedIndex = 0 Then
            'taille = 8.0F
            'TextBox1.Font = New Font(TextBox1.Font.Name, taille, Font.Style) sa marche pas , sa change la taille de la zone du texte et pas la taille du texte 
        End If

        'pour : 12px
        If ComboBox1.SelectedIndex = 1 Then
           
        End If

        'pour : 18px
        If ComboBox1.SelectedIndex = 2 Then
           
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If (CheckBox1.Checked) Then
            If Not TextBox1.Font.Bold Then
                TextBox1.Font = New Font(TextBox1.Font, FontStyle.Bold)
            End If
        Else
            If TextBox1.Font.Bold Then
                TextBox1.Font = New Font(TextBox1.Font, FontStyle.Regular)
            End If
        End If
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If (CheckBox2.Checked) Then
            If Not TextBox1.Font.Italic Then
                TextBox1.Font = New Font(TextBox1.Font, FontStyle.Italic)
            End If
        Else
            If TextBox1.Font.Italic Then
                TextBox1.Font = New Font(TextBox1.Font, FontStyle.Regular)
            End If
        End If
    End Sub
End Class

1 réponse

  1. Contributeur
    Bonjour,
    Ton "petit" problème n'est pas si petit que ça, .net ne fait certainement pas dans la simplicité !!
    Colle ce code dans ta form avec ton combo et 2 checkBox
    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 
            TextBox1.Text = "Essais" 
            S = TextBox1.Font.Size 
            For i = 8 To 12 Step 2 
                ComboBox1.Items.Add(i) 
                If i = S Then L = ComboBox1.Items.Count - 1 
            Next 
    
            Try 
                ComboBox1.SelectedIndex = L 
            Catch 
                ComboBox1.SelectedIndex = ComboBox1.Items.Count - 1 
            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 
        End Sub 
    
        Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged 
            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 CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged 
            Dim style As FontStyle = IIf(TextBox1.Font.Style And FontStyle.Italic, TextBox1.Font.Style Xor FontStyle.Italic, TextBox1.Font.Style Or FontStyle.Italic) 
            TextBox1.Font = New Font(TextBox1.Font, style) 
        End Sub 
    End Class

    A+
    L'expérience instruit plus sûrement que le conseil. (André Gide)
    Si tu te cognes à un pot et que ça sonne creux, c'est pas forcément le pot qui est vide. ;-)(Confucius)
    0