J'ai une riche zone de texte que je veux pouvoir styler comme WORD etc avec BOLD, ITALIC, UNDERLINE et toute combinaison d'entre eux.
Je peux ajouter et supprimer le style dit BOLD avec le code ci-dessous et je peux également ajouter plusieurs styles, mais si j'ai plusieurs stylings ensemble et essayer d'enlever un rien ne se passe.vb net Supprimer le style de police partielle dans la zone de texte riche
style Changer le code:
Private Sub Underline_Text(rtBox As RichTextBox)
Dim newStyle As FontStyle
If rtBox.SelectionFont.Style = FontStyle.Underline Then
newStyle = rtBox.SelectionFont.Style And Not FontStyle.Underline
Else
newStyle = rtBox.SelectionFont.Style Or FontStyle.Underline
End If
Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
rtBox.SelectionFont = newFont
End Sub
complet Code:
Private Sub rtbDesc_KeyDown(sender As Object, e As KeyEventArgs) Handles rtbDesc.KeyDown
If e.Control AndAlso Not e.Alt AndAlso Not e.Shift Then
Select Case e.KeyCode.ToString
Case "B"
Bold_Text(DirectCast(sender, RichTextBox))
e.SuppressKeyPress = True
Case "I"
Italics_Text(DirectCast(sender, RichTextBox))
e.SuppressKeyPress = True
Case "U"
Underline_Text(DirectCast(sender, RichTextBox))
e.SuppressKeyPress = True
Case "R"
Reset_Text(DirectCast(sender, RichTextBox))
e.SuppressKeyPress = True
Case "K"
Strikeout_Text(DirectCast(sender, RichTextBox))
e.SuppressKeyPress = True
End Select
End If
End Sub
Private Sub Bold_Text(rtBox As RichTextBox)
Dim newStyle As FontStyle
If rtBox.SelectionFont.Style = FontStyle.Bold Then
newStyle = rtBox.SelectionFont.Style And Not FontStyle.Bold
Else
newStyle = rtBox.SelectionFont.Style Or FontStyle.Bold
End If
Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
rtBox.SelectionFont = newFont
End Sub
Private Sub Italics_Text(rtBox As RichTextBox)
Dim newStyle As FontStyle
If rtBox.SelectionFont.Style = FontStyle.Italic Then
newStyle = rtBox.SelectionFont.Style And Not FontStyle.Italic
Else
newStyle = rtBox.SelectionFont.Style Or FontStyle.Italic
End If
Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
rtBox.SelectionFont = newFont
End Sub
Private Sub Underline_Text(rtBox As RichTextBox)
Dim newStyle As FontStyle
If rtBox.SelectionFont.Style = FontStyle.Underline Then
newStyle = rtBox.SelectionFont.Style And Not FontStyle.Underline
Else
newStyle = rtBox.SelectionFont.Style Or FontStyle.Underline
End If
Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
rtBox.SelectionFont = newFont
End Sub
Private Sub Reset_Text(rtBox As RichTextBox)
Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, FontStyle.Regular)
rtBox.SelectionFont = newFont
End Sub
Private Sub Strikeout_Text(rtBox As RichTextBox)
Dim newStyle As FontStyle
If rtBox.SelectionFont.Style = FontStyle.Strikeout Then
newStyle = rtBox.SelectionFont.Style And Not FontStyle.Strikeout
Else
newStyle = rtBox.SelectionFont.Style Or FontStyle.Strikeout
End If
Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
rtBox.SelectionFont = newFont
End Sub
Comme toujours, votre aide est grandement appréciée!
Cela a du sens. Je me demandais si ce n'était pas vérifier les deux. semble très verbeux d'avoir à vérifier pour chaque situation tho ... N'y at-il pas une méthode CONTAINS ou quelque chose de similaire pour le style de police? À la fin, tout ce que je veux faire est d'activer/désactiver ce style spécifique sans affecter tout autre style qui pourrait être appliqué au même texte. – WoodChuckChuck
Je suis d'accord, c'est verbeux. Malheureusement, c'était la seule façon de le faire fonctionner. Vous pourriez probablement juste faire une méthode qui vérifie toutes les permutations de FontStyles et appelle juste cela dans chaque instruction If. –
Aussi, si cela a répondu à votre question, s'il vous plaît assurez-vous de marquer comme la réponse/upvote que vous vous sentez approprié. Merci. –