title: C# - How to create progress bars dynamically
body: <p>I'm trying to develop a little program which can support several uploads simultaneously. However, I'm facing a problem : if I have one file uploading, there is a progress bar; but when I put more files to upload, I don't know how to create progress bars for each file independently and how to automatically add additional bars on multiple uploads.
  I use <code>WebClient</code> class with <code>UploadFileAsync</code>.</p>
  
  <p><code>String file</code> is the path needed for uploading and <code>uploadForm</code> is the <code>Form</code> where I would show progress bars and do uploads.</p>
  
  <p>EDIT : final code to make it functional</p>
  
  <pre><code>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) =&gt; UploadFini(sender, e, Path.GetFileName(file), nouvelleProgressBar, nouvelleTextBox, hauteur));
      clientUpload.UploadProgressChanged += new UploadProgressChangedEventHandler((sender, e) =&gt; UploadEnProgres(sender, e, Path.GetFileName(file), nouvelleProgressBar));
      clientUpload.UploadFileAsync(new Uri(urlServeur, "v1/ul"), file);
  }
  </code></pre>