Reputation: 498
Is it possible to declare a ListViewItem
array? E.g.
ListViewItem[] arrayItems;
If it's possible, how can I populate the array?
Is it possible to have the array which is not fixed size?
Upvotes: 1
Views: 4471
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
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
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