winforms - C# - How to create progress bars dynamically -
i'm trying develop little program can support several uploads simultaneously. however, i'm facing problem : if have 1 file uploading, there progress bar; when put more files upload, don't know how create progress bars each file independently , how automatically add additional bars on multiple uploads. use webclient
class uploadfileasync
.
string file
path needed uploading , uploadform
form
show progress bars , uploads.
edit : final code make functional
private void uploadenprogres(object sender, uploadprogresschangedeventargs e, string file, progressbar nouvelleprogressbar) { console.writeline("{0} : {1} octet sur {2} au total. {3} % envoyés...", file, e.bytessent, e.totalbytestosend, e.progresspercentage); nouvelleprogressbar.value = (int)((e.bytessent * 100) / e.totalbytestosend); } private void uploadfini(object sender, uploadfilecompletedeventargs e, string file, progressbar nouvelleprogressbar, textbox nouvelletextbox, int32 hauteur) { if ((e.cancelled) || (e.error != null)) { console.writeline("{0} : erreur -- {1}", file, e.error); return; } console.writeline("{0} : upload terminé, statut : ", file, system.text.encoding.utf8.getstring(e.result)); uploadform.controls.remove(nouvelleprogressbar); uploadform.controls.remove(nouvelletextbox); hauteur = 12; } public void httpclientupload(string file) { console.writeline("upload button."); webclient clientupload = new webclient(); string authinfo; authinfo = utilisateur + ":" + motdepasse; authinfo = convert.tobase64string(encoding.default.getbytes(authinfo)); clientupload.headers["authorization"] = "basic " + authinfo; // code http upload this.hauteur += 22; progressbar nouvelleprogressbar = new progressbar(); nouvelleprogressbar.size = new size(180, 20); nouvelleprogressbar.maximum = 100; nouvelleprogressbar.minimum = 0; nouvelleprogressbar.location = new point(258, hauteur); nouvelleprogressbar.visible = true; uploadform.controls.add(nouvelleprogressbar); textbox nouvelletextbox = new textbox(); nouvelletextbox.size = new size(180, 20); nouvelletextbox.location = new point(70, hauteur); nouvelletextbox.text = path.getfilename(file); nouvelletextbox.enabled = false; uploadform.controls.add(nouvelletextbox); clientupload.headers.add("chemin", "/" + identifiantappareil + file.replace(@"\", "/")); clientupload.uploadfilecompleted += new uploadfilecompletedeventhandler((sender, e) => uploadfini(sender, e, path.getfilename(file), nouvelleprogressbar, nouvelletextbox, hauteur)); clientupload.uploadprogresschanged += new uploadprogresschangedeventhandler((sender, e) => uploadenprogres(sender, e, path.getfilename(file), nouvelleprogressbar)); clientupload.uploadfileasync(new uri(urlserveur, "v1/ul"), file); }
//create new progressbar each time start upload in code var progressbar = new progressbar() //add form uploadform.controls.add(progressbar );
you'll need how group / position them
Comments
Post a Comment