c# - Label Text proprety would not change -
so trying show progress of application form2 form1, using huge label(located on form1), text proprety of label doesn't change using code.
there download process on executed via form2 , when finishes want label on form1 updated notify user of completion.
here code: in form1:
public string labeltext { { return this.label1.text; } set { this.label1.text = value; this.refresh(); } }
in form2:
private void dlclient(string link, string saveloc) { webclient = new webclient(); webclient.downloadprogresschanged += new downloadprogresschangedeventhandler(webclient_downloadprogresschanged); webclient.downloaddatacompleted += new downloaddatacompletedeventhandler(webclient_downloaddatacompleted); webclient.downloaddataasync(new uri(link)); } private void webclient_downloadprogresschanged(object sender, downloadprogresschangedeventargs e) { double bytesin = double.parse(e.bytesreceived.tostring()); double totalbytes = double.parse(e.totalbytestoreceive.tostring()); double percentage = bytesin / totalbytes * 100; progressbar1.value = int.parse(math.truncate(percentage).tostring()); } private void webclient_downloaddatacompleted(object sender, downloaddatacompletedeventargs e) { byte[] downloadedbytes = e.result; stream file = file.open(saveloc, filemode.create); file.write(downloadedbytes, 0, downloadedbytes.length); file.close(); webclient.dispose(); (new form1()).labeltext = "desired text";//the changing code close();
i don't think there use calling code of form2 within form1 posted because it's pretty working 100%.
pass form2 instance of form1, in form2's class constructor.
don't instantiate new (invisible) form1, you're doing here:
(new form1()).labeltext
the solution:
private form form1; public form2(form theform1) { form1 = theform1; }
...
((form1)form1).labeltext = "it works";
the calling code:
form2 frm2 = new form2(this);
"this" form1 instance.
Comments
Post a Comment