Wednesday 15 August 2012

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



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

right using buttons next code:

richtextbox1.selectionfont = new font("tahoma", 12, fontstyle.bold); richtextbox1.selectioncolor = system.drawing.color.red;

i want add together buttons bold, italic, etc using:

richtextbox1.selectionfont = new font("tahoma", 12, fontstyle.italic);

but if have bold option, code remove bold , add together italic. how can retain bold , italic?

thanks!

what need alter both existing , coming text 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 in order work selection must not have mix of styles..

also ought utilize checkboxes appearence=button

if using checkboxes create sure don't code default event checkedchanged fire when set state in code!

to switch style on , off can utilize maybe code in click events of 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 new state set , set it.

note did not utilize checked property of checkbox! reflect selection mix of bold , non-bold text need 3rd state, checkboxes should have threestate=true.

code set states on 2 style boxes 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 little larger thought @ beginning? well, is.. take time!! (and haven't begun fonts , sizes ;-)

c# winforms

No comments:

Post a Comment