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

前端 未结 5 840
小鲜肉
小鲜肉 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:49

    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();
          }
        }
    

提交回复
热议问题