Introduction
abbListView.Update
class provides a way to create a run time form to update listview
item.
Background
I was having difficulties to update ListView
Items. I solved mostly creating individual forms for every Listview
to update Items in the past. It was difficult and took up a lot of my time.
I created a class which creates a run time form and updates Listview
Item by itself automatically to save time.
Using the Code
There is a pre-requirement to use abbListview.Update
you must consider:
Label
for Textbox
will use your ListView
's column Header as Label Text.
- Column Tags will be TextBox Names, Column Tags mustn't be empty and must be unique.
The using class is simple, you must add the following lines to your Listview
's MouseDoubleClick
event. I assume your ListView
's name is listView1
.
listView1 = abb.abbListview.Update(listView1, "Form Header", listView1.SelectedIndices[0]);
Class source code is in the following:
using System;
using System.Windows.Forms;
namespace abb
{
public class abbListview
{
public static ListView Update(ListView lv, string lvHeader,
int itemId, int LocInd=27, int tbWidth=200, int tbHeight = 25)
{
Form newForm = new Form();
newForm.MaximizeBox = false;
newForm.MinimizeBox = false;
newForm.ShowInTaskbar = false;
newForm.FormBorderStyle = FormBorderStyle.FixedDialog;
newForm.Text = lvHeader + " / Line :" + itemId.ToString();
for (int i = 0; i < lv.Columns.Count; i++)
{
Label labelRun = new Label();
labelRun.Name = "labelRun" + i.ToString();
labelRun.Location = new System.Drawing.Point(10, 18 + (LocInd * i));
labelRun.AutoSize = true;
labelRun.Text = lv.Columns[i].Text;
newForm.Controls.Add(labelRun);
TextBox txtRun = new TextBox();
txtRun.Name = lv.Columns[i].Tag.ToString();
txtRun.Location = new System.Drawing.Point(200, 18 + (LocInd * i));
txtRun.Size = new System.Drawing.Size(tbWidth, tbHeight);
txtRun.Text = lv.Items[itemId].SubItems[i].Text;
newForm.Controls.Add(txtRun);
}
Button okButtonRun = new Button();
okButtonRun.Name = "OK";
okButtonRun.Text = "OK";
okButtonRun.Location = new System.Drawing.Point(500, 38 + lv.Columns.Count * LocInd);
okButtonRun.Size = new System.Drawing.Size(75, 23);
okButtonRun.DialogResult = DialogResult.OK;
okButtonRun.Click += delegate
{
newForm.DialogResult = DialogResult.OK;
newForm.Close();
};
newForm.Controls.Add(okButtonRun);
Button cancelButtonRun = new Button();
cancelButtonRun.Name = "Cancel";
cancelButtonRun.Text = "Cancel";
cancelButtonRun.Location = new System.Drawing.Point(400, 38 + lv.Columns.Count * LocInd);
cancelButtonRun.Size = new System.Drawing.Size(75, 23);
cancelButtonRun.DialogResult = DialogResult.Cancel;
cancelButtonRun.Click += delegate
{
newForm.DialogResult = DialogResult.Cancel;
newForm.Close();
};
newForm.Controls.Add(cancelButtonRun);
newForm.Size = new System.Drawing.Size(600, 38 + lv.Columns.Count * LocInd+3 + 60);
newForm.ShowDialog();
if(newForm.DialogResult==DialogResult.OK)
{
for (int i = 0; i < lv.Columns.Count; i++)
{
foreach (Control X in newForm.Controls)
{
if (X.GetType() == typeof(TextBox))
{
if (((TextBox)(X)).Name == lv.Columns[i].Tag.ToString())
{
lv.Items[itemId].SubItems[i].Text = ((TextBox)(X)).Text;
break;
}
}
}
}
}
return lv;
}
}
}
History
- 23rd September, 2015: Initial version