0

I have a method name DownloadProgressChanged. My program will be able to download multiple things, so therefore it will have different progressbars. The problem with DownloadProgressChanged is that I'm unsure how to get it to use a different progressbar depending on what is being downloaded. So essentially I'm wondering if I can pass the progressBar name through as a parameter when the method is called. (Like if downloading something else, progressBar2 will activate instead of progressBar1). My code is below.

WebClient wc = new WebClient();
        wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
        wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);


void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        double bytesIn = double.Parse(e.BytesReceived.ToString());
        double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
        double percentage = bytesIn / totalBytes * 100;
        progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
    }
John Smith
  • 21
  • 1

1 Answers1

0

Why not use a Dictionary<WebClient, ProgressBar>?

When creating your WebClient, assign the WebClient field of your dictionary to be the relevant ProgressBar. You could then retrieve this ProgressBar in your DownloadProgressChanged method by casting sender to a WebClient and doing a lookup in your dictionary.

Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44