garyamorris
garyamorris

Reputation: 2617

ListViewItem Construction

Quick and easy question on construction.

I have the following code for adding an Item to a list view.

ListViewItem 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.

Upvotes: 1

Views: 376

Answers (3)

ForbesLindesay
ForbesLindesay

Reputation: 10712

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"));

Upvotes: 1

Mikayil Abdullayev
Mikayil Abdullayev

Reputation: 12367

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);
}

Upvotes: 1

Stecya
Stecya

Reputation: 23266

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"));

Upvotes: 2

Related Questions