【问题标题】:C# - How to create progress bars dynamicallyC# - 如何动态创建进度条
【发布时间】:2014-08-18 15:46:58
【问题描述】:

我正在尝试开发一个可以同时支持多个上传的小程序。但是,我面临一个问题:如果我有一个文件上传,会有一个进度条;但是当我上传更多文件时,我不知道如何为每个文件独立创建进度条以及如何在多个上传时自动添加额外的进度条。 我使用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);
}

【问题讨论】:

  • 我不明白你在问什么......
  • 哦...对不起。我会尽量简洁:我会上传多个带有自己的进度条的文件,以一种形式显示它们(一个文件名/它的进度条;另一个文件名/它的进度条;第三个文件名/它的进度条;... ) 更好吗?

标签: c# winforms progress-bar webclient dynamically-generated


【解决方案1】:
//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 所说,记住在完成后将其删除。祝你好运!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多