Click here to Skip to main content
15,886,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I want to create a form that will include a list of tasks to be executed, I'm getting the task list at run time so I can't know how many progress bars to use. I need to create a form with a kind of a table that contains the columns: Task(taskName), Details, progress(progressBar), start Time and End Time.

can you help me find a good way to do that?

Thank you for your help Barak.

What I have tried:

I was thinking to do that with ListView or GridView but I didn't understand(or how to add a progress bar for a specific task or how to interact with the progress bars).
Posted
Updated 12-Mar-16 23:43pm

The way I'd do it is to create a UserControl which either inherits from ProgressBar or encapsulates it. Each time it is created, it is given a "task" to do, and kicks off a BackgroundWorker to do it, with updates coming to the progress bar.
Then all you have to do in the form code is create each instance of the control, add it to the Form.Controls collection and size / locate it appropriately, or add each control to a list view.
 
Share this answer
 
Another way of doing this, is to use a DataGridView control and to use a custom class that derives DataGridViewTextBoxCell to implement the ProgressBar painting as illustrated below. The DataGridViewProgressColumn class that uses the DataGridViewProgressCell can be added to the Columns collection of the grid. Then bind this column to a field providing the progress values in your datasource. You then have the option to also paint any text over the ProgressBar.


C#
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

// This is the class that represents your cell which can paint the ProgressBar
public class DataGridViewProgressCell : DataGridViewTextBoxCell
{

    public DataGridViewProgressCell()
    {
    }

    protected override void Paint(Graphics graphics, 
        Rectangle clipBounds, Rectangle cellBounds, int rowIndex, 
        DataGridViewElementStates cellState, object value, 
        object formattedValue, string errorText, DataGridViewCellStyle cellStyle, 
        DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, string.Empty, 
            string.Empty, string.Empty, cellStyle, advancedBorderStyle, paintParts);

        // Convert the value parameter to a number
        double progress = (double)value;

        // Create an instance of VisualStyleRenderer for the ProgressBar.Bar element
        VisualStyleRenderer backgoundRenderer = new VisualStyleRenderer(VisualStyleElement.ProgressBar.Bar.Normal);

        if (backgoundRenderer != null)
        {
            // Paint the ProgressBar background
            backgoundRenderer.DrawBackground(graphics, clipBounds);
            
            // Define a rectangle representing the progress value
            Rectangle rBar = new Rectangle(clipBounds.X + 1, clipBounds.Y + 1, Convert.ToInt32((progress * clipBounds.Width)) - 2, clipBounds.Height - 2);

            // Create an instance of VisualStyleRenderer for the ProgressBar.Chunk element
            VisualStyleRenderer barRenderer = new VisualStyleRenderer(VisualStyleElement.ProgressBar.Chunk.Normal);

            if (barRenderer != null)
            {
                // Paint the Progress value
                barRenderer.DrawBackground(graphics, rBar);
            }            
        }
    }
}

// This is the class that represents your column which can use your cell class
public class DataGridViewProgressColumn : DataGridViewTextBoxColumn
{
    public DataGridViewProgressColumn()
    {
        // Specify the column to use your custom cell class...
        base.CellTemplate = new DataGridViewProgressCell();
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900