How to add a percentage text % with number from 0 to 100 or 1 to 100 on a progressBar1 ? And how to make it to download each file int it's turn ?

sharon glipman 441 Reputation points
2021-10-09T23:40:16.203+00:00

Now it's downloading all the files at once I mean the progressbar is moving from start to end once when all the files downloading instead how can I make as option that the progress bar will start from 0 to 100 per file download ?

And how to add a percentages text for example %34 I added a label already to the middle of the progressBar1.

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    protected override async void OnLoad(EventArgs e) {
        var urls = new[] {
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
        };
        var filenames = new List<string>();
        using var wc = new WebClient();
        var progressPerUrl = 100 / (float)urls.Length;
        for (var i = 0; i < urls.Length; i++) {
            var url = urls[i];
            wc.Headers.Add("User-Agent", "User-Agent: CoolTool/0.0 (https://example.org/cool-tool/; cool-tool@example.org) generic-library/0.0");
            var filename = $"out-{i}.jpg";
            filenames.Add(filename);
            wc.DownloadProgressChanged += (e, sender) => {
                var urlProgressStart = progressPerUrl * i;
                progressBar1.Value = (int)(urlProgressStart + progressPerUrl * sender.ProgressPercentage / 100f);
            };
            await wc.DownloadFileTaskAsync(url, filename);
        }
        var saveTasks = new List<Task>();
        foreach (var filename in filenames) {
            saveTasks.Add(Task.Run(() => {
                var newFilename = Path.ChangeExtension(filename, ".gif");
                Image.FromFile(filename).Save(newFilename, ImageFormat.Gif);
            }));
        }
        await Task.WhenAll(saveTasks);
    }
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,918 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,212 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 86,496 Reputation points
    2021-10-10T07:33:02.15+00:00

    Use ProgressBarRenderer :

    139158-download-progressbar-test.gif

    Minimal class, to be improved :
    (just replace your progressBar1 with a progressBarText1)

        class ProgressBarText : ProgressBar  
        {  
            public ProgressBarText()  
            {  
                this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);  
            }  
      
            protected override void OnPaint(PaintEventArgs e)  
            {  
                Rectangle rect = this.ClientRectangle;  
                ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rect);               
                Rectangle rectCurrent = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)this.Value / this.Maximum) * rect.Width), rect.Height);  
                ProgressBarRenderer.DrawHorizontalChunks(e.Graphics, rectCurrent);  
      
                string sText = this.Value.ToString() + '%';  
                using (Font font = new Font("Arial", 12, FontStyle.Bold))  
                {  
                    SizeF strSize = e.Graphics.MeasureString(sText, font);  
                    Point pt = new Point((int)(rect.Width - strSize.Width) / 2, (int)(rect.Height - strSize.Height) / 2);  
                    e.Graphics.DrawString(sText, font, Brushes.Black, pt);  
                }  
            }  
        }  
    

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,471 Reputation points
    2021-10-10T01:35:57.933+00:00

    A great ProgressBar is Progress-O-Doom.

    Here I'm using this progress bar in a windows form utility.

    139172-doom.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.