Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在没有WPF的情况下将ProgressBar嵌入到ListView中?_C#_.net_Winforms_Listview_Progress Bar - Fatal编程技术网

C# 如何在没有WPF的情况下将ProgressBar嵌入到ListView中?

C# 如何在没有WPF的情况下将ProgressBar嵌入到ListView中?,c#,.net,winforms,listview,progress-bar,C#,.net,Winforms,Listview,Progress Bar,我找到的所有互联网资源都声称,在WinForms中,实现这一点的唯一可能性是创建自己的UserControl(示例:,,) 是否可以仅使用提供的WinForms控件实现此目的?是的,可以: 为列表中的每个项目创建一个图像列表,并向其中添加一个位图 创建一个ListView并将上面创建的ImageList附加为SmallImageList 将ListViewstyle设置为Details 根据需要添加尽可能多的列-第一列必须包含图片 添加后续的ListViewItems,为每列添加适当的字符串,

我找到的所有互联网资源都声称,在
WinForms
中,实现这一点的唯一可能性是创建自己的
UserControl
(示例:,,)

是否可以仅使用提供的
WinForms
控件实现此目的?

是的,可以:

  • 为列表中的每个项目创建一个
    图像列表
    ,并向其中添加一个
    位图
  • 创建一个
    ListView
    并将上面创建的
    ImageList
    附加为
    SmallImageList
  • ListView
    style设置为
    Details
  • 根据需要添加尽可能多的列-第一列必须包含图片
  • 添加后续的
    ListViewItem
    s,为每列添加适当的字符串,并引用在第一步中创建的适当的
    ImageIndex
    位图

  • 要刷新进度条,请执行以下操作:

其中
progressBarBitmap
progressBarImageList
中相应
progressBar
的相应位置(
index
)的图像,当然,每个
ListViewItem
都分配了自己的
progressBar

关键是将同一图像重新分配到
ImageList
中的同一位置-这会导致重新绘制,如果没有重新绘制,则无法工作。

优点: 快速(无需编写自己的
UserControl
),便宜(对此进行了大量调查,但最终编写的代码不多),而且有效

缺点: 我注意到当有大量物品时,有些东西会闪烁。此外,Mono上还有一些令人耳目一新的问题

示例结果:

使用示例应用程序编写代码:(BSD许可证)-尤其请参见BatchExperimentWindow类:

是,可能:

  • 为列表中的每个项目创建一个
    图像列表
    ,并向其中添加一个
    位图
  • 创建一个
    ListView
    并将上面创建的
    ImageList
    附加为
    SmallImageList
  • ListView
    style设置为
    Details
  • 根据需要添加尽可能多的列-第一列必须包含图片
  • 添加后续的
    ListViewItem
    s,为每列添加适当的字符串,并引用在第一步中创建的适当的
    ImageIndex
    位图

  • 要刷新进度条,请执行以下操作:

其中
progressBarBitmap
progressBarImageList
中相应
progressBar
的相应位置(
index
)的图像,当然,每个
ListViewItem
都分配了自己的
progressBar

关键是将同一图像重新分配到
ImageList
中的同一位置-这会导致重新绘制,如果没有重新绘制,则无法工作。

优点: 快速(无需编写自己的
UserControl
),便宜(对此进行了大量调查,但最终编写的代码不多),而且有效

缺点: 我注意到当有大量物品时,有些东西会闪烁。此外,Mono上还有一些令人耳目一新的问题

示例结果:

使用示例应用程序编写代码:(BSD许可证)-具体请参见BatchExperimentWindow类:

//...
{
  foreach (/*item to add to list*/)
  {
    Bitmap progressBarBitmap = new Bitmap(
        this.imageList.ImageSize.Width,
        this.imageList.ImageSize.Height);
    this.imageList.Images.Add(progressBarBitmap);
    ProgressBar progressBar = new ProgressBar();
    progressBar.MinimumSize = this.imageList.ImageSize;
    progressBar.MaximumSize = this.imageList.ImageSize;
    progressBar.Size = this.imageList.ImageSize;

    // probably create also some BackgroundWorker here with information about
    // this particular progressBar

    ListViewItem lvi = new ListViewItem(
        new[] { "column1", ... },
        this.listView.Items.Count);

    lvi.UseItemStyleForSubItems = true;
    this.listView.Items.Add(lvi);
    lvi.Tag = /* some convenient info class to refer back to related objects */
  }
//...
}
int previousProgress = progressBar.Value;
progressBar.Value = ...
if (progressBar.Value != previousProgress)
{
  progressBar.DrawToBitmap(progressBarBitmap, bounds);
  progressBarImageList.Images[index] = progressBarBitmap;
}