创建包含progreesbar列表的winform [英] Creating a winform that contains a progreesbar list
问题描述
您好我想创建一个包含要执行的任务列表的表单,我在运行时获取任务列表,所以我不知道要使用多少进度条。我需要创建一个包含列的表格:任务(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 aDataGridView
control and to use a custom class that derivesDataGridViewTextBoxCell
to implement the ProgressBar painting as illustrated below. TheDataGridViewProgressColumn
class that uses theDataGridViewProgressCell
can be added to theColumns
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屋!