关于建筑的快速和简单的问题。
我有以下代码用于向列表视图中添加项。
ListViewItem item = new ListViewItem();
item.Text = file;
item.SubItems.Add("Un-Tested");
lvJourneys.Items.Add(item);然而,我希望使用更类似于下面的代码,但是我找不到正确的语法,
lvJourneys.Items.Add(new ListViewItem(file, "Un-Tested"));感谢任何人的帮助。
发布于 2011-09-07 19:09:48
你只需要像这样制作你自己的自定义构造函数:
public ListViewItem(string receivedFile, string theItem){ //I assume File is of type String
this.Text=receivedFile;
this.SubItems.Add(theItem);
}发布于 2011-09-07 19:05:30
创建工厂
static class ListViewItemFactory
{
public static ListViewItem Create(string text,string subItem)
{
ListViewItem item = new ListViewItem();
item.Text = text;
item.SubItems.Add(subItem);
return item;
}
}然后使用
lvJourneys.Items.Add(ListViewItemFactory.Create(file, "Un-Tested"));发布于 2011-09-07 19:13:13
创建您自己的ListViewItem以添加新的构造函数
public class ItemWithSubItem:ListViewItem
{
public ItemWithSubItem(string ItemText, string SubItemText)
{
this.Text=ItemText;
this.SubItems.Add(SubItemText);
}
}然后你可以直接使用
lvJourneys.Items.Add(new ItemWithSubItem(file, "Un-Tested"));https://stackoverflow.com/questions/7332734
复制相似问题