richTextBox设置选中的字体属性
执行一次设置选中的字体样式 再执行一次恢复正常
//粗体
public void ToggleBold() {
if (richTextBox1.SelectionFont == null) richTextBox1.SelectionFont = richTextBox1.Font; FontStyle style = richTextBox1.SelectionFont.Style; if (richTextBox1.SelectionFont.Bold) style &= ~FontStyle.Bold;//恢复正常 else style |= FontStyle.Bold; richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style); }
斜体
public void ToggleItalic() { if (richTextBox1.SelectionFont == null) richTextBox1.SelectionFont = richTextBox1.Font; FontStyle style = richTextBox1.SelectionFont.Style; if (richTextBox1.SelectionFont.Italic) style &= ~FontStyle.Italic;//恢复正常 else style |= FontStyle.Italic; richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style); }
下划线
public void ToggleUnderLine() { if (richTextBox1.SelectionFont == null) richTextBox1.SelectionFont = richTextBox1.Font; FontStyle style = richTextBox1.SelectionFont.Style; if (richTextBox1.SelectionFont.Underline) style &= ~FontStyle.Underline;//恢复正常 else style |= FontStyle.Underline; richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style); }