First of all, I want you all to know that I started learning C # as my first programming approach, and therefore I am the absolute hobbiest beginner who started coding 2 months ago, so please come with me.
So what I'm trying to do is create a custom download application. It all works, except for the "Download All" button, which cannot select the variable "percent1" from the "DownloadProgressChangedEventArgs". I created it before the mainForm constructor, but it will not read the changed value.
Here's the code, partially divided, since most of it is not relevant to the question:
public partial class Main : Form { //Variables (not all, just the one im having issues with) private double percentage1; //Main form constructor public Main(){...} //Download File Async custom method public void DldFile(string url, string fileName, string localPath, AsyncCompletedEventHandler completedName, DownloadProgressChangedEventHandler progressName) { WebClient webClient = new WebClient(); webClient.DownloadFileAsync(new Uri(url), localPath + "\\" + fileName); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(completedName); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(progressName); } //Button 1 click event to start download private void btnDld1_Click(object sender, EventArgs e) { if (url1 != "" && Directory.Exists(localPath1)) { _startDate1 = DateTime.Now; DldFile(url1, fileName1, localPath1, completed1, progress1); } //took out the try/catch, other ifs to try and cut it down } //Download Progress Changed event for Download 1 public void progress1(object sender, DownloadProgressChangedEventArgs e) { percentage1 = e.ProgressPercentage; //THIS IS WHERE I WAS EXPECTING TO UPDATE "percentage1" progressBar1.Value = int.Parse(Math.Truncate(percentage1).ToString()); } //Button that starts all downloads click event where all my problems are at the moment private void btnDldAll_Click(object sender, EventArgs e) { //The progress bar that should let me know the global status for all webClients progressBarAll.Value = ( int.Parse(Math.Truncate(percentage1).ToString()) + //HERE IS MY PROBLEM int.Parse(Math.Truncate(percentage2).ToString()) + //HERE IS MY PROBLEM int.Parse(Math.Truncate(percentage3).ToString()) + //HERE IS MY PROBLEM int.Parse(Math.Truncate(percentage4).ToString()) + //HERE IS MY PROBLEM int.Parse(Math.Truncate(percentage5).ToString())) / 5; //HERE IS MY PROBLEM //Checks if the link exists and starts it from the download button click event if (url1 != "") { btnDld1.PerformClick(); } //Continues for url2, 3, 4, 5 and else } }
So, this is the shortest way that I have found to inform you that I am trying to shoot, if something is missing there, please let me know, I will try to add any information as quickly as possible.
I tried to create an instance of "progress1" to try to use the percent1 variable, but it did not work. I tried to do the same with webClient but did not work. I used google search and stackflow to no avail. So I'm not sure if the question is too stupid, or is there another way to look at a problem that completely leaves my mind.
Thus, the main problem is updating the percent1 variable and using it. There are other problems associated with calculating "progressBarAll.Value" that will be resolved when I can get the correct value. So no need to worry about it if you see it.
I am so sorry for the long post, but I'm not sure where the problem is, so I had to give you more information to make sure that I did not mess up elsewhere.
Happy coding; D