Is it possible to do a ListViewItem array? - c#

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?

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

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

Related

C# adding elements to list in the list (two dimensional list)

I have a list:
List <List <string>> aList = new List <List <string>> ();
I want add something to the list in this list like that:
aList [0].Add ("item");
But I get ArgumentOutOfRangeException, why?
Adding list to list works perfect:
List <string> testList = new List <string> ();
testList.Add ("item");
aList.Add (testList);
Without any bugs.
What am I doing wrong?
The first statement adding with index assumed that the size of list is >=1 which is not. Just initialization doesn't create a sized list.
Ok, I found a solution to my own question!
I must add any list to my own list before I can do that because it's empty so I can't access the list which not exists.
List <string> testList = new List <string> ();
aList.Add (testList);
aList [0].Add ("item");
Without any errors.
aList [0].Add ("value");
This fails as there is no List at position 0 to add your value to.
Try
aList [0] = new List<string>;
aList [0].Add ("value");
public class Row
{
public List<string> Elements { get; private set; }
public Row()
{
Elements = new List<string>();
}
public Row(List<string> elements)
{
Elements = elements;
}
}
Using :
List<Row> rows = new List<Row>();
rows.Add(new Row( new List<string>(){"abc","abc","abc"}));
rows.Add(new Row(new List<string>() { "xyz", "xyz", "xyz" }));
or
Row row = new Row();
row.Elements.Add("abc");
row.Elements.Add("xyz");
rows.Add(row);
You can not add elements to a list which is not instateated

Why am I getting ArgumentOutOfRangeException in Lists?

I am using List of Lists in my project. When i run program i get ArgumentOutOfRangeException. But there is no range specified in list.
I declared list like this:
public static List<List<string>> list = new List<List<string>>();
Now i want to add my name in the first "list" which is in the List of lists.
list[0].Add("Hussam"); //Here i get ArgumentOutOfRange Exception.
What should I do now?
But there is no range specified in list
No, there's an index specified (as an argument), and that's what's out of range. Look at your code:
list[0].Add("Hussam");
That's trying to use the first list in list - but is list is empty, there is no first element. The range of valid arguments to the indexer is empty, basically.
So first you want:
list.Add(new List<string>());
Now list[0] will correctly refer to your empty List<string>, so you can add "Hussam" to it.
You want to add an item to the first item in an empty list... That isn't going to work. First, add the list inside the other list:
public static List<List<string>> list = new List<List<string>>();
List<string> innerList = new List<string>();
list.Add(innerList);
innerList.Add("Hussam");
Why are you creating a list of a list? Wouldn't List suffice? What is happening here is the inner list is not being initialized.
list.Add(new List<string>());
list[0].Add("Jimmy");
In this case ocurred an exception because you tried acess an index which not exists, then you must add an inner initial list, which could be done follows:
list.Add(new new List<string>());
Or, if you want add an first name directly:
list.Add(new new List<string>(){"Hussam"});
Ok so first, you have to understand that the "index" only comes after the value has been declared. Lists behave different. They are not like arrays. You get the index in which you want to store the item and when you do that, you use the code array[index] = value;.
But in a List, to give a value to a completely new item, you use the method Add(value).
So here's a reminder: Systems.Collections.Generic.List<> has nothing to do with array[ ]s
You cannot access list[0] as there is no item at index 0. The list is empty.
You need to add a new List like this:
list.Add(new List<string> { "Hussam" });
or, assign a list to index 0 and then add to it as per your posted code:
list.Add(new List<string>());
list[0].Add("Hussam");
If you don't always know if the list will be be empty or not you can use FirstOrDefault (a LINQ method) to check if there is any entry at index 0 and assign one if not, otherwise use the existing inner list:
var innerList = list.FirstOrDefault();
if (innerList == null)
{
innerList = new List<string>();
list.Add(innerList);
}
innerList.Add("Hussam");
The problem is, your nested list hasn't been initialized, with anything.
So, calling the first item of the nested list is correctly telling you there is nothing in it.
To verify:
int superlistCounter = 1;
int sublistCounter = 1;
foreach(var sublist in list)
{
Console.WriteLine("Now in List #" + superlistCounter);
foreach(var item in sublist)
{
Console.WriteLine("List item #" + sublistCounter + ": " + item)
}
}
The output will be:
Now in List #1
It sounds like you're expecting:
Now in List #1
List Item #1: Hussam
To fix this, simply initialize your list!
public static List<List<string>> list = new List<List<string>>();
// ...
List<string> subList1 = new List<string>();
list.Add(subList1);
subList1.Add("Hussam");

how to Copy listView selected items into an array

I'm trying to get listView items into an array..
in listBox
listBox1.SelectedItems
would do the trick.
But it didn't work in listView...
any Ideas???
Do like this,
var myList = new List<string>();
foreach(ListViewItem Item in ListView.SelectedItems)
{
myList.add(Item.Text.ToString());
}
var myArray = myList.ToArray();

getting arraylist from BulletedList control

I have a BulletedList control in my project. I want to assign BulletedList control's all items to an array variable.
There are 3 items in BulletedList.
string[] array = new string[3];
array = blistselected.Items.Value;
How can I do this?
Thanks.
Just iterate through the Items Collection using for or foreach like this.
string listCount = blistselected.Items.Count;
string[] array = new string[listCount];
for (int i=0; i<blistselected.Items.Count; i++)
{
array[i] = blistselected.Items[i].Text;
}
You need the ListItemCollection.CopyTo() method.
So it would be:
string[] array = new string[3];
blistselected.Items.CopyTo(array, 0);
I've purely done this through looking at the docs, so may need changes and type conversions etc.
Also as there are 3 items your array has an extra element.

how to get datagridView display content of list rather than just length

List A = new List();
A.Add("Apple");
A.Add("Banana");
A.Add("Pineapple");
dataGridView.DataSource = a;
Result: is the length of each item in the list rather than Item itself.
5
6
9
How can I make datagridView to display string instead of length.
Try using a List to actually take the string values rather than representing them by the char[].Count();
For example:
List<string> A = new List<string>();
A.Add("Apple");
A.Add("Banana");
A.Add("Pineapple");
dataGridView.ItemSource = A;

Categories