Thursday, 15 August 2013

c# - Why these lines of code doesn't run as expected? -



c# - Why these lines of code doesn't run as expected? -

i'm creating chat application , want display names in bold . when form first load, display history conversation database on richtextbox command using these lines of code , want display name in bold :

here's code create possible :

string strprovider = "data source=" + srv_host + ";database=" + srv_db + ";user id=" + srv_user + ";password=" + srv_pass; mysqlconnection myconn = new mysqlconnection(strprovider); seek { myconn.open(); string strcmd = "select * comments task_id=@task_id , ((from_usr=@from , to_usr=@to) or (from_usr=@to , to_usr=@from)) order at_time asc"; mysqlcommand mycmd = new mysqlcommand(strcmd, myconn); mycmd.parameters.addwithvalue("from", frm_usr); mycmd.parameters.addwithvalue("to", to_usr); mycmd.parameters.addwithvalue("task_id", tid); mycmd.executenonquery(); // execute mysqldatareader dr = mycmd.executereader(); while (dr.read()) { string text = dr.getvalue(1).tostring() + ": " + dr.getvalue(6) + environment.newline; richtextbox1.appendtext(text); richtextbox1.selectionstart = 0; richtextbox1.selectionlength = dr.getvalue(1).tostring().length; richtextbox1.selectionfont = new font(richtextbox1.font,fontstyle.bold); } myconn.dispose(); } grab (exception e) { messagebox.show(e.message); }

and these lines of code doesn't work expected :

while (dr.read()) { string text = dr.getvalue(1).tostring() + ": " + dr.getvalue(6) + environment.newline; richtextbox1.appendtext(text); richtextbox1.selectionstart = 0; richtextbox1.selectionlength = dr.getvalue(1).tostring().length; richtextbox1.selectionfont = new font(richtextbox1.font,fontstyle.bold); }

edit: dr.getvalue(1).tostring() holds tha total name of user dr.getvalue(6).tostring() hold message problem code above displays first name in bold other lines not affected. see image could 1 please tell me reason why code isn't working. couldn't figure out error.thank-you

the problem having here richtextbox1.selectionstart zero, meaning formatting ever applied first line in text box.

try setting richtextbox1.selectionstart = richtextbox1.textlength.

edit:

try setting to

richtextbox1.selectionstart = richtextbox1.textlength - text.length;

edit 2:

i think invalid argument thing caused utilize of environment.newline. if utilize instead "\n", code works fine. problem environment.newline of course of study \r\n on windows, yet richtextbox1 seems ignoring \r. results in richtextbox1.textlength - text.length beingness -1 in first iteration.

c# mysql .net richtextbox

No comments:

Post a Comment