可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!
回答1:
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
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!
To switch a Style on and off you can use maybe code like this in the Click events of the style boxes:
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! 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.
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 ;-)
回答2:
use this code
richTextBox1.Font = new Font("Tahoma", 12, FontStyle.Bold | FontStyle.Italic);
回答3:
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(); } }
回答4:
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);