创建包含progreesbar列表的winform [英] Creating a winform that contains a progreesbar list

查看:53
本文介绍了创建包含progreesbar列表的winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想创建一个包含要执行的任务列表的表单,我在运行时获取任务列表,所以我不知道要使用多少进度条。我需要创建一个包含列的表格:任务(taskName),详细信息,进度(progressBar),开始时间和结束时间。


$ b $你能帮我找到一个好办法吗?



感谢你帮助巴拉克。



我尝试了什么:



我想用ListView或GridView做到这一点,但我不明白(或如何为特定任务添加进度条或如何与进度条交互)。

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).

推荐答案

我这样做的方法是创建一个继承的UserControl来自ProgressBar或封装它。每次创建它时,都会给它一个任务,然后启动一个BackgroundWorker来执行它,更新进入进度条。

然后你需要在表单中完成所有工作代码是创建控件的每个实例,将其添加到Form.Controls集合并相应地调整大小/定位,或将每个控件添加到列表视图。
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.


另一种方法,是使用 DataGridView 控件,并使用派生 DataGridViewTextBoxCell 的自定义类来实现ProgressBar绘画,如下图所示。使用 DataGridViewProgressCell DataGridViewProgressColumn 类可以添加到网格的集合。然后将此列绑定到提供数据源中的进度值的字段。然后您可以选择在ProgressBar上绘制任何文本。





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.


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();
    }
}


这篇关于创建包含progreesbar列表的winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆