c# - How to programatically set RichTextbox text and line color? -
i'm using this answer programatically add colored lines text richtextbox.
my richtextboxextensions
class same 1 in solution. have outputmessage
class:
class outputmessage { private string _message; private color _color; public string message { { return _message; } } public color color { { return _color; } } public outputmessage(string message, codedeployer.enums.outputtypes ot) { _message = message; switch (ot) { case enums.outputtypes.success: _color = color.green; break; case enums.outputtypes.error: _color= color.red; break; case enums.outputtypes.warning: _color= color.darkorange; break; default: _color = color.black; break; } } }
on form, maintain list<outputmessage>
, have method that'll iterate on list
, try put contents richtextbox
on form.
private void foo() { this.txtoutput = getoutput(); this.txtoutput.text = getoutput().text; } private richtextbox getoutput() { richtextbox results = new richtextbox(); foreach (outputmessage om in output) results.appendtext(om.message, om.color); return results; }
if execute foo()
first line of code, txtoutput
doesn't change @ all. it's empty textbox.
if execute foo()
second line of code, txtoutput
does have of text list<outputmessage>
, color lost.
if change getoutput
directly interact control on form, works expected. having accept richtextbox
argument works well.
can explain me? i'm guessing has how things passed reference / value, don't understand.
the .text
property of richtextbox
plain unformatted text.
the text property not return information formatting applied contents of richtextbox. rich text formatting (rtf) codes, use rtf property.
so should try using .rtf
property instead:
this.txtoutput.rtf = getoutput().rtf;
Comments
Post a Comment