c# - Invalidate and DrawRectangle don't work together -
imagine simple .net 2.0 windows-form multilinetextbox fills whole form. want (re-)draw rectangle everytime key pressed. in real application there fare more logic position , stuff of rectangle - keep simple.
i thought: "lets first invalidate textbox , draw rectangle." doesnt work. screen flickers shortly - thats it. if remove line "invaliate" rectangle drawn - old ones keep position..
whats wrong? , how repaint scratch?
thank in advance answers!
private void onkeydown(object sender, keyeventargs e) { textbox1.invalidate(); using (graphics g = this.textbox1.creategraphics()) { int startx = 100; int starty = 300; int height = 200; brush brush = new solidbrush(color.fromargb(60, 255, 0, 0)); pen mypen = new pen(color.black, 2); mypen.dashstyle = dashstyle.dash; g.drawrectangle(mypen, startx, starty, this.textbox1.width, height); g.fillrectangle(brush, startx, starty, this.textbox1.width, height); } }
your approach flawed. invalidate()
requests repaint delayed , happens after draw rectangle. disappears again immediately.
a short (but wrong) fix replace invalidate()
update()
, don't that.
you'll have move drawing logic textbox1.paint
event. , form needs (boolean) property decide whether draw rectangle or not.
your keydown should like:
private void onkeydown(object sender, keyeventargs e) { this.drawrect = true; textbox1.invalidate(); }
Comments
Post a Comment