NET WinForm implements the method of adding progressbar to listview

  • 2021-12-13 09:04:22
  • OfStack

This article illustrates the method of. NET WinForm to add progressbar to listview. Share it for your reference, as follows:

After looking for it for a long time, I didn't find it. Later, I simply wrote one myself:

First, add progressbar to the event that loads data to listview:


foreach (string d in arr)
{
    int index = lv.Items.Count + 1;
    item = new ListViewItem(new string[] { index.ToString(), d, "", "", "", "" });
    lv.Items.Add(item);
    float progress = 0;
    Rectangle SizeR = default(Rectangle);
    System.Windows.Forms.ProgressBar ProgBar = new System.Windows.Forms.ProgressBar();
    SizeR = item.SubItems[2].Bounds;
    SizeR.Width = lv.Columns[2].Width;
    ProgBar.Parent = lv;
    ProgBar.SetBounds(SizeR.X, SizeR.Y, SizeR.Width, SizeR.Height);
    ProgBar.Value = (int)progress;
    ProgBar.Visible = true;
    // Take 1 Individual only 1 The name is easy to find in the future 
    ProgBar.Name = d + "progressbar";
}

Then set the value of progressbar where it needs to be modified:


// Cycle listview Find all controls on the progressbar
foreach (Control item in lv.Controls)
{
    if (item.Name == d.Name + "progressbar")
    {
      ProgressBar bar = (ProgressBar)item;
      bar.Value = (int)((d.Progress) * 100);
    }
}

In fact, we only fix progressbar in the grid specified by listview according to its length, width and height. If we drag the columns in listview, the position of the grid will change. At this time, we need to modify the position corresponding to proressbar. We need to add ColumnWidthChanging event. When dragging column, progressbar will change its position:


private void lvt_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
  Rectangle SizeR = default(Rectangle);
  int width = e.NewWidth;
  foreach (Control item in lv.Controls)
  {
    // Find all the names progressbar
    if (item.Name.IndexOf("progressbar") >= 0)
    {
      ProgressBar bar = (ProgressBar)item;
      //Rectangle size=bar.Bounds;
      SizeR=bar.Bounds;
      //lv.Columns[2] Is placed progressbar The place of 
      SizeR.Width=lv.Columns[2].Width;
      bar.SetBounds(lv.Items[0].SubItems[2].Bounds.X, SizeR.Y, SizeR.Width, SizeR.Height);
      //bar.Width = width;
    }
  }
}

For more readers interested in C # related content, please check the topics on this site: "WinForm Control Usage Summary", "C # Form Operation Skills Summary", "C # Data Structure and Algorithm Tutorial", "C # Common Control Usage Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: