ListViewItem Construction
Quick and easy question on construction.
I have the following code for adding an Item to a list view.
ListViewIte开发者_开发技巧m item = new ListViewItem();
item.Text = file;
item.SubItems.Add("Un-Tested");
lvJourneys.Items.Add(item);
However I wish to use code more similar to the following, but i'm unable to find the correct syntax,
lvJourneys.Items.Add(new ListViewItem(file, "Un-Tested"));
Appreciate any help.
Create a factory
static class ListViewItemFactory
{
public static ListViewItem Create(string text,string subItem)
{
ListViewItem item = new ListViewItem();
item.Text = text;
item.SubItems.Add(subItem);
return item;
}
}
And then use
lvJourneys.Items.Add(ListViewItemFactory.Create(file, "Un-Tested"));
You just need to make your own custom constructor like such:
public ListViewItem(string receivedFile, string theItem){ //I assume File is of type String
this.Text=receivedFile;
this.SubItems.Add(theItem);
}
Create your own ListViewItem to add a new constructor
public class ItemWithSubItem:ListViewItem
{
public ItemWithSubItem(string ItemText, string SubItemText)
{
this.Text=ItemText;
this.SubItems.Add(SubItemText);
}
}
Then you can just use
lvJourneys.Items.Add(new ItemWithSubItem(file, "Un-Tested"));
精彩评论