View previous topic :: View next topic |
Author |
Message |
mitja
Joined: 18 Feb 2005 Posts: 19
|
Posted: Mon Aug 22, 2005 12:29 am Post subject: Adding ListView items |
|
|
I am confused at how items and subitems to a ListView might be added.
The following code produces results as seen on the screen shot.
Code: | items.add("foo");
items[0].subItems.add("foo1");
items[0].subItems.add("foo2");
items[0].subItems.add("foo3");
items[0].subItems.add("foo4");
items[0].subItems.add("foo5");
items.add("bar");
items[0].subItems.add("bar1");
items[0].subItems.add("bar2");
items[0].subItems.add("bar3");
items[0].subItems.add("bar4");
items[0].subItems.add("bar5");
items.add("cool");
items[1].subItems.add("cool1");
items[1].subItems.add("cool2");
items[1].subItems.add("cool3");
items[1].subItems.add("cool4");
items[1].subItems.add("cool5");
items.add("blah");
items[1].subItems.add("blah1");
items[1].subItems.add("blah2");
items[1].subItems.add("blah3");
items[1].subItems.add("blah4");
items[1].subItems.add("blah5");
items.add("rox");
items[4].subItems.add("rox1");
items[4].subItems.add("rox2");
items[4].subItems.add("rox3");
items[4].subItems.add("rox4");
items[4].subItems.add("rox5"); |
Note the index number inconsistency, I have no idea how to automate that. Am I missing something? How do you add items (and subitems) to your listviews? |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Mon Aug 22, 2005 3:42 am Post subject: Re: Adding ListView items |
|
|
mitja wrote: | Note the index number inconsistency, I have no idea how to automate that. Am I missing something? How do you add items (and subitems) to your listviews? |
"add" functions will append after the last item, so you could use subItems[subItems.count-1] after adding, but that's not always the optimal choice. The best way would be to create an instance of ListViewItem, add sub items directly to it, then add that to the ListView. addRow() was recently added to ListView (one of the snapshots) that can do this for you if you provide it with an array of strings. This just gave me an idea to overload it with a typesafe variadic function so that it can be as easy as addRow("foo1", "foo2", "foo3", "foo4", "foo5"); |
|
Back to top |
|
|
mitja
Joined: 18 Feb 2005 Posts: 19
|
Posted: Mon Aug 22, 2005 9:28 am Post subject: |
|
|
Thanks, addRow() worked. |
|
Back to top |
|
|
mitja
Joined: 18 Feb 2005 Posts: 19
|
Posted: Wed Aug 31, 2005 7:12 am Post subject: |
|
|
Another thing: suppose records have associating indices in database.
When I add record's data to listview (add item), only strings get added, but not indices, because I don't want them to be shown. (ColumnHeaders also cannot be hidden.) So I lose associations of data to their indices. So when I later collect selectedItems, there is no way to grab their original indices. ListView indices don't help, because they get mixed up as soon I change SortOrder. So far I can compare data in selected records with the ones in the database, but that eventually takes too long.
Is this what is Object for? |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Thu Sep 01, 2005 6:01 am Post subject: |
|
|
mitja wrote: | Another thing: suppose records have associating indices in database.
... |
I'm a little confused but perhaps this solves it: derive from ListViewItem and add instances of it to the ListView. Then you can cast the items back to your derived class to access your extra data.
Code: |
class MyData: ListViewItem
{
int extraStuff;
this(char[] text)
{
super(text);
}
}
ListView lview = new ListView;
lview.items.add(cast(ListViewItem)new MyData("hi"));
MyData data = cast(MyData)lview.items[0];
data.extraStuff = 3;
|
Note that the only reason why you can add Object is so that it satisfies the IList interface. If the Object you add isn't a ListViewItem it only adds its toString(). |
|
Back to top |
|
|
mitja
Joined: 18 Feb 2005 Posts: 19
|
Posted: Thu Sep 01, 2005 8:19 am Post subject: |
|
|
This might probably work, but requires some redesigning of how raw data are handled. I've been using simple char[][][] so far. |
|
Back to top |
|
|
|