Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6915819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:31:22+00:00 2026-05-27T09:31:22+00:00

I’m trying to create a custom download app. Its all working except for the

  • 0

I’m trying to create a custom download app. Its all working except for the download all button that cant pick up the “percent1” variable from the “DownloadProgressChangedEventArgs”. I have instantiated it prior to the mainForm constructor but it wont read the changed value.

Here’s the code, partially stripped since most of it isnt 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 i found of letting you know what im trying to pull off, if there’s something missing please let me know, i’ll try to add any info as fast as possible.

I have tried to instantiate “progress1” to try and acess its percentage1 variable, but it didnt work. I’ve tried doing the same thing with the webClient but didnt work either. I have used google and stackflow search to no avail. So im not sure if the question is too dumb, or there’s a diferent way to look at the issue thats completely out of my mindset.

So main problem is updating the “percentage1” variable and using it.
There are other problems regarding the “progressBarAll.Value” calculation that will be solved when i can get my hands on the right value. So no need to worry about that if you see it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:31:23+00:00Added an answer on May 27, 2026 at 9:31 am

    Try not to think about ‘using the event arguments outside the event’. Think about updating the state of your form.

    Use properties to simplify the update logic:

    public partial class Main : Form
    {
      private double percentage1;
      private double percentage2;
      private double percentage3;
      private double percentage4;
      private double percentage5;
    
      private double Percentage1 
      {
        get
        {
          return this.percentage1;
        }
        set
        {
          this.percentage1 = value;
          this.UpdatePercentageAll();  // this will update overall progress whenever the first one changes
    
          progressBar1.Value = GetValueFromPercentage(value);
        }
      }
      private double Percentage2
      // same code as for Percentage1
    
      void UpdatePercentageAll()
      {
        this.PercentageAll = (this.Percentage1 + this.Percentage2 + this.Percentage3 + this.Percentage4 + this.Percentage5) / 5;
      }
    
      static int GetValueFromPercentage(double percentage)
      {
        return (int)Math.Truncate(percentage);
      }
    
      double percentageAll;
      private double PercentageAll
      {
        get
        {
          return this.percentageAll;
        }
        set
        {
          this.percentageAll = value;
    
          progressBarAll.Value = GetValueFromPercentage(value);
        }
      }
    
      //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))
        {
            this.StartDownloadFile1();
        }
        //took out the try/catch, other ifs to try and cut it down
      }
      void StartDownloadFile1()
      {
            this.Percentage1 = 0;
            _startDate1 = DateTime.Now;
            DldFile(url1, fileName1, localPath1, completed1, progress1);
      }
      //Download Progress Changed event for Download 1
      public void progress1(object sender, DownloadProgressChangedEventArgs e)
      {
        this.Percentage1 = e.ProgressPercentage; // update property, not field
    
        //this will be done in property setters
        //progressBar1.Value = int.Parse(Math.Truncate(percentage1).ToString());
      }
      // then add similar code for other download buttons
    
      //Button that starts all downloads click event where all my problems are at the moment
      private void btnDldAll_Click(object sender, EventArgs e)
      {
        //Checks if the link exists and starts it from the download button click event
        if (url1 != "")
        {
            this.StartDownloadFile1();
        }
        //Continues for url2, 3, 4, 5 and else
      }
    }
    

    I would refactor the code even further, but I think it will be easier for you to understand if the code is closer to the original.

    The main idea is to create a set of linked properties which work like mathematical functions. When writing the PercentageX properties I’m kind of saying ‘let PercentageAll be the average of all percentages’. Then I have each download update it’s own progress. Once any progress is updated it updates the average, and I don’t have to rememver that inside the progress changed event handler.

    And the last point is updating progress bars from percentage properties. It’s quite straightforward: once a percentage is changed, I need to update a bar. If so, why bother writing something like

    this.Percentage1 = x;
    this.progressBar1.Value = (int)Math.Truncate(x);
    

    In this case I have to remember everywhere that once I change the Percentage1 I have to update the bar. And in my example I just create a strict rule for that which is only in one place and works everytime. So I just cannot forget it. And if I need to change the rule, I need to change only one place, so again I cannot make a mistake.

    The technique I demonstrate can be expressed as a well-known rule: ‘one rule – one place’, which means that you should try to have only single place in code that expresses each logical rule that exists in your program. It is a very important idea, I suggest you learn and use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.