Home > Article > Backend Development > .NET WinForm implements the method of adding progressbar in listview
This article mainly introduces the method of adding progressbar in listview through .NET WinForm. It briefly analyzes the addition and use of progress bar control in the form of examples. Friends in need can refer to the examples of this article.
Describes the method of adding progressbar in listview using .NET WinForm. Share it with everyone for your reference, the details are as follows:
I searched for a long time but couldn’t find it, and then I simply wrote one myself:
First, in the event# of loading data into the listview ##Add progressbar in:
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; //取一个唯一的名字,以后好找 ProgBar.Name = d + "progressbar"; }Then set its value where the value of progressbar needs to be modified:
//循环listview上的所有控件,按名字找到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 just fixed the progressbar in the grid specified by the listview according to the length, width and height. Here, if we drag the column in the listview, the position of the grid will change. At this time, we need to modify the position of the corresponding progressbar. We need to add the ColumnWidthChanging event. When dragging the column, the 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) { //根据名字找到所有的progressbar if (item.Name.IndexOf("progressbar") >= 0) { ProgressBar bar = (ProgressBar)item; //Rectangle size=bar.Bounds; SizeR=bar.Bounds; //lv.Columns[2]是放置progressbar的地方 SizeR.Width=lv.Columns[2].Width; bar.SetBounds(lv.Items[0].SubItems[2].Bounds.X, SizeR.Y, SizeR.Width, SizeR.Height); //bar.Width = width; } } }
The above is the detailed content of .NET WinForm implements the method of adding progressbar in listview. For more information, please follow other related articles on the PHP Chinese website!