c# - I still can't clear the rectangle i drawn over a control inside OnPaint event why i can'y clear it? -
by clear mean redraw or paint or color control it's original. working code:
using system; using system.componentmodel; using system.collections.generic; using system.diagnostics; using system.text; using system.threading; using system.windows.forms; using system.drawing; namespace ftp_progressbar { public partial class form1 : form { public form1() { initializecomponent(); txthost.textchanged += anytextbox_textchanged; txtuploadfile.textchanged += anytextbox_textchanged; txtdir.textchanged += anytextbox_textchanged; anytextbox_textchanged(null, null); if ((txthost.text == "") || txtuploadfile.text == "") { btnupload.enabled = false; } if (txtdir.text == "") { checkbox1.enabled = false; } } private void anytextbox_textchanged(object sender, eventargs e) { btnupload.enabled = txthost.textlength > 0 && txtuploadfile.textlength > 0; checkbox1.enabled = txtdir.textlength > 0; this.invalidate(); } private void form1_load(object sender, eventargs e) { } private void btnbrowse_click(object sender, eventargs e) { if(this.openfiledialog1.showdialog() != dialogresult.cancel) this.txtuploadfile.text = this.openfiledialog1.filename; } private void btnupload_click(object sender, eventargs e) { if(this.ftpprogress1.isbusy) { this.ftpprogress1.cancelasync(); this.btnupload.text = "upload"; } else { ftpsettings f = new ftpsettings(); f.host = this.txthost.text; f.username = this.txtusername.text; f.password = this.txtpassword.text; f.targetfolder = this.txtdir.text; f.sourcefile = this.txtuploadfile.text; f.passive = this.chkpassive.checked; try { f.port = int32.parse(this.txtport.text); } catch { } this.toolstripprogressbar1.visible = true; this.ftpprogress1.runworkerasync(f); this.btnupload.text = "cancel"; } } private void ftpprogress1_progresschanged(object sender, progresschangedeventargs e) { this.toolstripstatuslabel1.text = e.userstate.tostring(); // message like: 45 kb / 102.12 mb this.toolstripprogressbar1.value = math.min(this.toolstripprogressbar1.maximum, e.progresspercentage); } private void ftpprogress1_runworkercompleted(object sender, runworkercompletedeventargs e) { if(e.error != null) messagebox.show(e.error.tostring(), "ftp error"); else if(e.cancelled) this.toolstripstatuslabel1.text = "upload cancelled"; else this.toolstripstatuslabel1.text = "upload complete"; this.btnupload.text = "upload"; this.toolstripprogressbar1.visible = false; } protected override void onpaint(painteventargs e) { base.onpaint(e); pen penborder; if (txthost.textlength <= 0) { penborder = new pen(color.red, 3); e.graphics.drawrectangle(penborder, txthost.location.x, txthost.location.y, txthost.width - 1, txthost.height - 1); } if (txtuploadfile.textlength <= 0) { penborder = new pen(color.red, 3); e.graphics.drawrectangle(penborder, txtuploadfile.location.x, txtuploadfile.location.y, txtuploadfile.width - 1, txtuploadfile.height - 1); } } } }
i saw without breakpoint if minimize form1 when program running after typed text in both textboxes , resize form1 clear rectangles.
strange seems it's taking effect when minimize , resize form1.
in textchanged event tried add: txthost.invalidate(); didn't help. way rectangle clear if minmize , resize form1.
or adding this.invalidate(); did trick.
onpaint()
gets called when window needs updated. basic principle how windows works. if need window updated then, yes, need invalidate window onpaint()
called.
but ok redraw form?
sure, it's not performant redrawing areas don't need redrawing. invalidate()
should have version accepts rectangle argument. use invalidate area want update.
Comments
Post a Comment