我正在編寫一個用於上傳和下載文件的C#應用程序。下載使用WebClient對象及其DownloadAsycDownload
方法。下載適用於多個文件。它下載儘可能多的文件,我想要的。C#多個進度條用於多個文件下載
我的問題是我無法顯示所有文件在不同進度條上的動態添加到窗體的flowlayout control
的進度。
這裏是我的代碼:
public ProgressBar[] bar;
public int countBar=0;
...
bar[countBar] = new ProgressBar();
flowLayoutPanel1.Controls.Add(bar[countBar]);
countBar++;
request.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownoadInProgress);
request.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
request.DownloadFileAsync(new Uri(this.uri), localPath);
byte[] fileData = request.DownloadData(this.uri);
FileStream file = File.Create(localPath);
file.Write(fileData, 0, fileData.Length);
file.Close();
}
public void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
flowLayoutPanel1.Controls.Remove(bar[countBar]);
countBar--;
MessageBox.Show("Download Completed");
}
public void DownoadInProgress(object sender, DownloadProgressChangedEventArgs e)
{
bar[countBar].Maximum = total_bytes;
bar[countBar].Value = (int)e.BytesReceived;
}
謝謝,這真的很有幫助。 – Habtamu 2013-05-02 12:10:38
如果這完全幫助你,請將其標記爲已接受的答案。否則,讓我知道缺少的東西。 – SimpleVar 2013-05-02 13:55:44
我想從另一個類中添加組件。在運行時無法將進度條添加到表單中。我發佈的這些方法在另一個類中。 – Habtamu 2013-05-02 14:41:03