我有以下代码,我想弄清楚,但我完全难过。我将进度条添加到列表视图中,但我真的不知道如何访问每个进度条以更新进度值。
public ProgressBar LvAddProgB(ListView LV, int LVII, int LVColI, string lvName)
{
Rectangle SizeR = default(Rectangle);
ProgressBar ProgBar = new ProgressBar();
SizeR = LV.Items[LVII].Bounds;
SizeR.Width = LV.Columns[LVColI].Width;
if (LVColI > 0)
{
SizeR.X = SizeR.X + LV.Columns[LVColI - 1].Width;
}
ProgBar.Parent = LV;
ProgBar.Name = lvName;
ProgBar.SetBounds(SizeR.X, SizeR.Y, SizeR.Width, SizeR.Height);
ProgBar.Visible = true;
ProgBar.Maximum = 1000;
ProgBar.Step = 1;
return ProgBar;
}
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < 3; ++x)
{
ListViewItem item = new ListViewItem();
item.Text = "d.Name";
item.SubItems.Add(" ");
listView1.Items.Add(item);
LvAddProgB(listView1, x, 1, "Lview" + x.ToString());
}
}
答案 0 :(得分:6)
如果您使用某种键,可以将它从Controls集合中取出来进行更新。由于每个都显示为它是ListView的一部分,因此两者之间似乎存在某种联系。密钥还将提供链接项目和相关ProgressBar
。
假设您的ListView
是Details
视图,只需在最后添加一个子项,不带相关的ColumnHeader
。数据不会显示,但仍与项目相关。使用与ProgressBar
名称相同的文字,很容易找到。
我的ListView
有3列:{Item, Name, Completion}
,但代码会添加第4个子项来存储密钥:
private void AddLVItem(string key, string name, int value)
{
ListViewItem lvi = new ListViewItem();
ProgressBar pb = new ProgressBar();
lvi.SubItems[0].Text = name;
lvi.SubItems.Add(value.ToString());
lvi.SubItems.Add("");
lvi.SubItems.Add(key); // LV has 3 cols; this wont show
lv.Items.Add(lvi);
Rectangle r = lvi.SubItems[2].Bounds;
pb.SetBounds(r.X, r.Y, r.Width, r.Height);
pb.Minimum = 1;
pb.Maximum = 10;
pb.Value = value;
pb.Name = key; // use the key as the name
lv.Controls.Add(pb);
}
然后,更新给定键的值和进度条的方法:
private void UpdateItemValue(string key, int value)
{
ListViewItem lvi;
ProgressBar pb;
// find the LVI based on the "key" in
lvi = lv.Items.Cast<ListViewItem>().FirstOrDefault(q => q.SubItems[3].Text == key);
if (lvi != null)
lvi.SubItems[1].Text = value.ToString();
pb = lv.Controls.OfType<ProgressBar>().FirstOrDefault(q => q.Name == key);
if (pb != null)
pb.Value = value;
}
用法:
// add some data
AddLVItem("A", "Ziggy", 1);
AddLVItem("B", "Zacky", 1);
AddLVItem("C", "Zoey", 1);
AddLVItem("D", "Zeke", 1);
// update the displayed value and progressbar using the key:
UpdateItemValue("A", 6);
UpdateItemValue("B", 5);
UpdateItemValue("C", 8);
UpdateItemValue("D", 2);
答案 1 :(得分:0)
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(12, 64);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(504, 164);
this.listView1.TabIndex = 1;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Width = 99;
//
// columnHeader2
//
this.columnHeader2.Width = 117;
//
// columnHeader3
//
this.columnHeader3.Width = 117;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(528, 261);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.VScrollBar vScrollBar1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
public ProgressBar LvAddProgB(ListView LV, int X, int Y, string lvName)
{
ProgressBar ProgBar = new ProgressBar();
ProgBar.Parent = LV;
ProgBar.Name = lvName;
ProgBar.Location = new Point(X, Y);
ProgBar.Visible = true;
ProgBar.Maximum = 1000;
ProgBar.Step = 1;
return ProgBar;
}
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < 3; ++x)
{
ListViewItem item = new ListViewItem();
item.Text = "d.Name";
item.SubItems.Add(" ");
listView1.Items.Add(item);
listView1.Controls.Add(LvAddProgB(listView1, item.Position.X + item.Bounds.Width, item.Position.Y, "Lview" + x.ToString()));
}
}