RichTextBox font style button for Bold and/or Italic (how to code so more than one can be chosen instead of replacing

前端 未结 5 986
小鲜肉
小鲜肉 2021-01-27 19:48

Right now I am using some buttons with the following code:

richTextBox1.SelectionFont = new Font(\"Tahoma\", 12, FontStyle.Bold);
richTextBox1.SelectionColor = S         


        
5条回答
  •  忘掉有多难
    2021-01-27 20:30

    Sharing a VB.NET Implementation:

    Set Bold

     Private Sub BoldToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BoldToolStripMenuItem.Click
    
    If Not RichTextBox1.SelectionFont Is Nothing Then
    
                Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont
                Dim newFontStyle As System.Drawing.FontStyle
    
                If RichTextBox1.SelectionFont.Bold = True Then
                    newFontStyle = FontStyle.Regular
                   
                    BoldToolStripMenuItem.Checked = False
                Else
                    newFontStyle = FontStyle.Bold
                  
                    BoldToolStripMenuItem.Checked = True
                End If
    
                RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Xor FontStyle.Bold)
    
            End If
        End Sub
    

    Set Italic

    Private Sub ItalicToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ItalicToolStripMenuItem.Click
    
     If Not RichTextBox1.SelectionFont Is Nothing Then
    
                Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont
                Dim newFontStyle As System.Drawing.FontStyle
    
                If RichTextBox1.SelectionFont.Italic = True Then
                    newFontStyle = FontStyle.Regular
                   
                    ItalicToolStripMenuItem.Checked = False
                Else
                    newFontStyle = FontStyle.Italic
                    
                    ItalicToolStripMenuItem.Checked = True
                End If
    
                RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Xor FontStyle.Italic)
    
            End If
        End Sub
    

    Set Underline

     Private Sub UnderlineToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UnderlineToolStripMenuItem.Click
    
      If Not RichTextBox1.SelectionFont Is Nothing Then
    
                Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont
                Dim newFontStyle As System.Drawing.FontStyle
    
                If RichTextBox1.SelectionFont.Underline = True Then
                    newFontStyle = FontStyle.Regular
                   
                    UnderlineToolStripMenuItem.Checked = False
                Else
                    newFontStyle = FontStyle.Underline
                   
                    UnderlineToolStripMenuItem.Checked = True
                End If
    
                RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Xor FontStyle.Underline)
    
            End If
        End Sub
    

    RichTextBox SelectionChanged Event

     Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
    
     If RichTextBox1.SelectionFont.Bold = True Then
    
                  
                    BoldToolStripMenuItem.Checked = True
                Else
    
                  
                    BoldToolStripMenuItem.Checked = False
                End If
    
     If RichTextBox1.SelectionFont.Italic = True Then
    
               
                ItalicToolStripMenuItem.Checked = True
            Else
    
              
                ItalicToolStripMenuItem.Checked = False
            End If
    
     If RichTextBox1.SelectionFont.Underline = True Then
    
                   
                    UnderlineToolStripMenuItem.Checked = True
                Else
    
                  
                    UnderlineToolStripMenuItem.Checked = False
                End If
    
    End Sub
    

提交回复
热议问题