walkman
walkman

Reputation: 498

Is it possible to do a ListViewItem array?

Is it possible to declare a ListViewItem array? E.g.

ListViewItem[] arrayItems;

Upvotes: 1

Views: 4471

Answers (3)

CragG
CragG

Reputation: 1

If using Visual Studio and MFC:

        array<ListViewItem^>^ listviewarray = gcnew array<System::Windows::Forms::ListViewItem^>(100);

        int i;
        for (i = 0; i < 100; i++)
        {
            listviewarray[i] = gcnew ListViewItem("Text1"); // create each sensor listview element
        }

Upvotes: 0

Zein Makki
Zein Makki

Reputation: 30022

Yes ! It is possible to declare a List as an array.

For Specified length use below:

ListViewItem[] arrayItems = new ListViewItem[5];

arrayItems[0] = new ListViewItem("Text1");
arrayItems[1] = new ListViewItem("Text2");
// so on

For Unspecified length use below:

List<ListViewItem> arrayItems = new List<ListViewItem>();

arrayItems.Add(new ListViewItem("Text1"));
arrayItems.Add(new ListViewItem("Text2"));

// When you want to pass it as array, use arrayItems.ToArray();

Or if you have some list of objects with some text Property:

List<ListViewItem> arrayItems = dataSourceObject.Select(x => new 
                                                  ListViewItem(x.TextProperty)).ToList();

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

It seems that you're looking for List<ListViewItem>, not for array (ListViewItem[]):

List<ListViewItem> myItems = new List<ListViewItem>();

// Just add items when required; 
// have a look at Remove, RemoveAt, Clear as well
myItems.Add(new ListViewItem("Text 1"));

// When you want read/write the item do as if you have an array
ListViewItem myItem = myItems[0]; 

You can use Linq to obtain items from existing ListView:

myItems = listView1.Items
  .OfType<ListViewItem>()
  .ToList();

or append existing list:

List<ListViewItem> myItems = new List<ListViewItem>();
...
myItems.AddRange(listView1.Items.OfType<ListViewItem>());

Upvotes: 1

Related Questions