简体   繁体   English

粗体和/或斜体的 RichTextBox 字体样式按钮(如何编码以便可以选择多个而不是替换

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

Right now I am using some buttons with the following code:现在我正在使用一些带有以下代码的按钮:

richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold);
richTextBox1.SelectionColor = System.Drawing.Color.Red;

I also want to add some buttons for Bold, Italic, etc using:我还想使用以下方法为粗体、斜体等添加一些按钮:

richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Italic);

But if I have a Bold option, this code will remove the Bold and add Italic.但是如果我有一个粗体选项,这个代码将删除粗体并添加斜体。 How can I retain the Bold and Italic?如何保留粗体和斜体?

Thanks!谢谢!

This Works for me hope it will help you guys...这对我有用,希望它会帮助你们......

private void cmdBold_Click(object sender, EventArgs e)
{
   Font new1, old1;
   old1 = rtxtBox.SelectionFont;
   if (old1.Bold)
      new1 = new Font(old1, old1.Style & ~FontStyle.Bold);
   else
      new1 = new Font(old1, old1.Style | FontStyle.Bold);

   rtxtBox.SelectionFont = new1;
   rtxtBox.Focus();
}

private void cmdItalic_Click(object sender, EventArgs e)
{
  Font new1, old1;
  old1 = rtxtBox.SelectionFont;
  if (old1.Italic)
    new1 = new Font(old1, old1.Style & ~FontStyle.Italic);
  else
    new1 = new Font(old1, old1.Style | FontStyle.Italic);

  rtxtBox.SelectionFont = new1;      

  rtxtBox.Focus();
}

private void cmdUnderline_Click(object sender, EventArgs e)
{
  Font new1, old1;
  old1 = rtxtBox.SelectionFont;
  if (old1.Underline)
    new1 = new Font(old1, old1.Style & ~FontStyle.Underline);
  else
    new1 = new Font(old1, old1.Style | FontStyle.Underline);

  rtxtBox.SelectionFont = new1;
  rtxtBox.Focus();
}

Font Size字体大小

private void cmbSize_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == 13)
      {
        if (float.Parse(cmbSize.Text.Trim()) == 0)
        {
          MessageBox.Show("Invalid Font Size, it must be Float Number", "Invalid Font Size", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          cmbSize.Text = "10";
          return;
        }
        if (cmbSize.Text.Trim() != "")
        {
          Font new1, old1;
          old1 = rtxtBox.SelectionFont;
          new1 = new Font(FontFamily.GenericSansSerif, float.Parse(cmbSize.Text.Trim()), old1.Style);
          rtxtBox.SelectionFont = new1;
        }
        rtxtBox.Focus();
      }
    }

What you really need to change both existing and coming text is this:您真正需要更改现有和即将到来的文本是这样的:

if (richTextBox2.SelectionLength > 0 ) richTextBox2.SelectionFont =
 new Font(richTextBox1.SelectionFont, FontStyle.Bold | richTextBox1.SelectionFont.Style);
else richTextBox2.Font =
 new Font(richTextBox1.Font, FontStyle.Bold | richTextBox1.Font.Style);

Note that in order to work the selection must not have a mix of styles..请注意,为了工作,选择不能混合样式..

Also you probably ought to use CheckBoxes with Appearence=Button此外,您可能应该将CheckBoxesAppearence=Button一起使用

If you are using those CheckBoxes make sure you don't code their default event CheckedChanged as this would also fire when you set their state in code!如果您正在使用这些 CheckBoxes,请确保您没有编写它们的默认事件CheckedChanged因为当您在代码中设置它们的状态时,这也会触发!

To switch a Style on and off you can use maybe code like this in the Click events of the style boxes:要打开和关闭样式,您可以在样式框的Click事件中使用这样的代码:

FontStyle style = checkBox1.CheckState == CheckState.Checked ? 
                    FontStyle.Italic : FontStyle.Regular;

if (richTextBox2.SelectionLength > 0) richTextBox2.SelectionFont =
    new Font(richTextBox1.SelectionFont, style | richTextBox1.SelectionFont.Style);
else richTextBox2.Font =
    new Font(richTextBox1.Font, style | richTextBox1.Font.Style);

This first decides on the new state to set and then set it.这首先决定要设置的新状态,然后再设置它。

note that I did not use the Checked Property of the CheckBox!请注意,我没有使用CheckBox的Checked属性! To reflect a selection with a mix of bold and non-bold text we need a third state, so the CheckBoxes should have ThreeState=true .为了反映混合了粗体和非粗体文本的选择,我们需要第三个状态,因此 CheckBoxes 应该具有ThreeState=true

Code to set the states on only two style boxes could look like this:仅在两个样式框上设置状态的代码可能如下所示:

private void richTextBox2_SelectionChanged(object sender, EventArgs e)
{
   // mixed state:   
   if (richTextBox2.SelectionFont == null)
   {
     checkBox1.CheckState = CheckState.Indeterminate;
     checkBox2.CheckState = CheckState.Indeterminate;
     return;
   }
   checkBox1.Checked =
      (richTextBox2.SelectionFont.Style & FontStyle.Bold) == FontStyle.Bold;
   checkBox2.Checked =
      (richTextBox2.SelectionFont.Style & FontStyle.Italic) == FontStyle.Italic;
}

Starting to look a little larger than you thought at the beginning?开始看起来比你一开始想象的要大一点? Well, it is.. take your time!!嗯,它是..慢慢来!! (And we haven't even begun with Fonts and sizes ;-) (我们甚至还没有开始使用字体和大小;-)

使用此代码

richTextBox1.Font = new Font("Tahoma", 12, FontStyle.Bold | FontStyle.Italic);

you need to do something like this..你需要做这样的事情..

in order ot make it italic.为了使它成为斜体。

 richTextBox1.SelectionFont = new Font("Tahoma", 12, richTextBox1.SelectionFont.Style | FontStyle.Italic);

//in order to make it bold //为了让它加粗

richTextBox1.SelectionFont = new Font("Tahoma", 12, richTextBox1.SelectionFont.Style | FontStyle.Bold);

Sharing a VB.NET Implementation:共享一个 VB.NET 实现:

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 RichTextBox SelectionChanged 事件

 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使字体斜体和粗体 - Make font italic and bold 如何在 WPF RichTextBox 中使字符串的某些部分变为粗体并具有不同的字体样式(大小、字体粗细) - How to make some parts of a string to be bold and with different font style (size, font weight) within a WPF RichTextBox 字体不支持“常规”,“粗体”,“斜体”等任何样式 - font does not support any style like 'regular' 'bold' 'Italic' Pdf锋利的字体样式大胆,斜体和下划线在一起 - Pdf sharp font style Bold,Italic and Underline together C#:如何将以粗体显示的日期添加到RichTextBox中,但继续以常规字体(非粗体)书写 - C#: How can I add the DATE in BOLD into a RichTextBox but keep writing in regular font (not BOLD) RichTextBox粗体等宽字体? - RichTextBox bold monospace font? 如何检查RichTextBox中选定的文本是否具有粗体样式 - How to check if the selected text in RichTextBox has bold style 如何在 WPF 中的 Textbox/Richtextbox 中为所选文本制作粗体按钮? - How can i make a button for Bold a selected text in Textbox/Richtextbox in WPF? 如何停止我的粗体/斜体/下划线选项将文本的字体更改回默认字体和大小 - How to stop my bold/italic/underline options changing the text's font back to the default font and size 如何使用C#以不同的颜色,大小,字体,粗体和斜体格式以excel或csv格式写入数据 - How to write data in excel or csv with different color, size , Font , Bold and Italic format using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM