C # - How to dynamically create progress bars

I am trying to develop a small program that can support multiple downloads at the same time. However, I ran into a problem: if I have one file download, there is a progress bar; but when I add more files to upload, I don’t know how to create progress bars for each file independently and how to automatically add extra bars for multiple uploads. I am using class WebClient

c UploadFileAsync

.

String file

is the path required for the download, and uploadForm

is Form

where I would show progress bars and perform downloads.

EDIT: final code to make it 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);
}

      

+3


source to share


2 answers


//Create a new progressbar each time you start an upload in code
var progressbar = new ProgressBar()

//add it to the form
uploadForm.Controls.Add(progressbar );

      



you need to look at how you group / position them

+3


source


For organization, I would suggest a TableLayoutPanel for adding newly created controls.



0


source







All Articles