ListView selected items

G

GTi

I have a listview that with selected items.
But I want to "reselect" items after a refresh.
Each items have a uniqe value in the Tag object.
After a refresh some new items may be added or removed
so using item index is not possible.

To get the list of selected items I use:
ListView.SelectedListViewItemCollection selItems =
this.listView1.SelectedItems;

And to get the currrent focused item (may not be selected):
focusItem = this.listView1.FocusedItem;

But how can I reselect items after a refresh, including the focused item ?
foreach(ListViewItem item in selItems)
{
?????
}
 
M

Michael Voss

Hi!
I have a listview that with selected items.
But I want to "reselect" items after a refresh.
Each items have a uniqe value in the Tag object.
After a refresh some new items may be added or removed
so using item index is not possible.

To get the list of selected items I use:
ListView.SelectedListViewItemCollection selItems =
this.listView1.SelectedItems;

And to get the currrent focused item (may not be selected):
focusItem = this.listView1.FocusedItem;

But how can I reselect items after a refresh, including the focused item ?
foreach(ListViewItem item in selItems)
{
?????
}

The selected items will no longer be present in the ListView after a refresh
(usually, you will create new items based on your collection). So, instead
of remembering this.listView1.SelectedItems, remember their tag values:
(Warning: untested pseudocode)

ArrayList selectedTags = new ArrayList();

foreach (ListViewItem item in selItems)
{
selectedTags.Add(item.Tag);
};


To reselect the items, iterate over this.listView1.Items:

foreach (ListViewItem item in this.listView1.Items)
{

if (selectedTags.Includes(item.Tag))
{
item.Selected = true;
};
};
 
G

GTi

Michael Voss said:
Hi!


The selected items will no longer be present in the ListView after a
refresh
(usually, you will create new items based on your collection). So, instead
of remembering this.listView1.SelectedItems, remember their tag values:
(Warning: untested pseudocode)

ArrayList selectedTags = new ArrayList();

foreach (ListViewItem item in selItems)
{
selectedTags.Add(item.Tag);
};


To reselect the items, iterate over this.listView1.Items:

foreach (ListViewItem item in this.listView1.Items)
{

if (selectedTags.Includes(item.Tag))
{
item.Selected = true;
};
};

THANKS!!!
Looks nice, but...
if (selectedTags.Includes(item.Tag))
..Includes is unknown...

I can't find anything similar.
 
M

Michael Voss

Hi !

GTi wrote:

[...snip...]
THANKS!!!
Looks nice, but...
.Includes is unknown...

I can't find anything similar.

That's why I wrote:

[...snip...][...snip...]

You might want to write another method like:

private bool includes(ArrayList list, object testObject)
{
foreach (object item in list)
{
// Here, you might want to use a different
// comparison, maybe item == testObject
// or (int)item == int(testObject),
// depending on your unique tags

if (item.Equals(testObject))
return true;
}

return false;
}


and call it like

if (this.includes(selectedTags, item.Tag))
{
....
};

Wouldn't it be nice if we could extend existing classes ;-)
 
G

GTi

Michael Voss said:
Hi !

GTi wrote:

[...snip...]
THANKS!!!
Looks nice, but...
.Includes is unknown...

I can't find anything similar.

That's why I wrote:

[...snip...][...snip...]

You might want to write another method like:

private bool includes(ArrayList list, object testObject)
{
foreach (object item in list)
{
// Here, you might want to use a different
// comparison, maybe item == testObject
// or (int)item == int(testObject),
// depending on your unique tags

if (item.Equals(testObject))
return true;
}

return false;
}


and call it like

if (this.includes(selectedTags, item.Tag))
{
...
};

Wouldn't it be nice if we could extend existing classes ;-)

Michael,
I know the code was untested, and I really appreciate your contribute to
learn
up an newbie.
Wouldn't it be nice if we could extend existing classes ;-)
But we can do it, Create a new class and then inheritance the ListView
class.
But we loose the design view ...... (?)
 
M

Michael Voss

GTi wrote:
[...snip...]
But we can do it, Create a new class and then inheritance the ListView
class.
[...snip...]

That's not what I call extending a class beacuse you'd effectively create a
new class (one you don't really need). I would like to add a method like
"public bool includes(object item)" to some _existing_ classes like
"ArrayList" or "CollectionBase"...
 
G

GTi

I know what you mean now,
Yeah, that would be nice.
Missing a contribute keyword.

In c/WIN32 it was possible to
Subclassing functions.
I don't know how this is done in C# (yet)



Michael Voss said:
GTi wrote:
[...snip...]
But we can do it, Create a new class and then inheritance the ListView
class.
[...snip...]

That's not what I call extending a class beacuse you'd effectively create
a
new class (one you don't really need). I would like to add a method like
"public bool includes(object item)" to some _existing_ classes like
"ArrayList" or "CollectionBase"...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top