2015-11-19 91 views
0

我有一个方法名称DownloadProgressChanged。我的程序将能够下载多个东西,所以它会有不同的进度条。 DownloadProgressChanged的问题是我不确定如何让它使用不同的进度条,具体取决于正在下载的内容。所以基本上我想知道如果我可以传递progressBar名称作为参数调用该方法。 (就像下载其他东西一样,progressBar2将会激活而不是progressBar1)。我的代码如下。将进度条作为参数传递?

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()); 
    } 
+0

哈哈!在发布问题后的30秒内下降。典型的StackOverflow。 –

+0

请参阅http://stackoverflow.com/questions/2465040/using-lambda-expressions-for-event-handlers。您可以捕获lambda中的进度条UI元素 – pm100

+0

@ pm100如果OP不想使用lambda表达式,则可能不太理想。 –

回答

0

为什么不使用Dictionary<WebClient, ProgressBar>

创建WebClient时,请将字典中的WebClient字段指定为相关的ProgressBar。然后,您可以通过将sender转换为WebClient并在字典中进行查找来检索DownloadProgressChanged方法中的ProgressBar