C# - 如何动态创建进度条

3
我正在开发一个小程序,可以同时支持多个上传。然而,我面临一个问题:如果我有一个文件正在上传,就会有一个进度条;但是当我添加更多文件进行上传时,我不知道如何为每个文件单独创建进度条,并且如何在多个上传中自动添加额外的进度条。
我使用WebClient类和UploadFileAsync方法。 String file是上传所需的路径,uploadFormForm,用于显示进度条和执行上传操作。
编辑:最终代码使其功能正常。
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);
}

我不明白你在问什么... - matan7890
哦...抱歉。我会尽量简明扼要:我想上传多个文件,并显示它们在表单中的进度条(一个文件名/其进度条;另一个文件名/其进度条;第三个文件名/其进度条; ...)。这样更好吗? - Fluf
2个回答

3
//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 );

你需要了解如何对它们进行分组/定位。

上传完成后别忘了删除它。 - matan7890
1
完全不是。这个答案只是给你最少的程序化创建和添加每个条形图所需的内容。您仍然有很多工作要完成,例如如何正确定位它们,如何跟踪每个上传的正确条形图,并像matan7890所说的那样在完成后记得将其删除。祝你好运! - ajg

0

为了组织性,我建议使用TableLayoutPanel来添加新创建的控件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接