现在我正在使用一些带有以下代码的按钮:
richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold);
richTextBox1.SelectionColor = System.Drawing.Color.Red;
Run Code Online (Sandbox Code Playgroud)
我还想使用以下方法添加一些粗体、斜体等按钮:
richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Italic);
Run Code Online (Sandbox Code Playgroud)
但如果我有粗体选项,此代码将删除粗体并添加斜体。如何保留粗体和斜体?
谢谢!
这对我有用希望对你们有帮助......
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();
}
Run Code Online (Sandbox Code Playgroud)
字体大小
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();
}
}
Run Code Online (Sandbox Code Playgroud)