By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

ListView selected items

P: n/a
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)
{
?????
}
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi!

GTi wrote:
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;
};
};
Nov 17 '05 #2

P: n/a
GTi
"Michael Voss" <mi********************@lvrREMOVE.CAPSde> wrote in message
news:42a01f0a$1@news...
Hi!

GTi wrote:
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;
};
};


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

..Includes is unknown...

I can't find anything similar.

Nov 17 '05 #3

P: n/a
Hi !

GTi wrote:

[...snip...]
THANKS!!!
Looks nice, but...
if (selectedTags.Includes(item.Tag))

.Includes is unknown...

I can't find anything similar.


That's why I wrote:

[...snip...]
(Warning: untested pseudocode)

[...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 ;-)
Nov 17 '05 #4

P: n/a
Oops...

That
// or (int)item == int(testObject), -----------------------^^^^^^^^^^^^^^^

should have read:
// or (int)item == (int)testObject


of course!
Nov 17 '05 #5

P: n/a
GTi
"Michael Voss" <mi********************@lvrREMOVE.CAPSde> wrote in message
news:42a023b7@news...
Hi !

GTi wrote:

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

I can't find anything similar.


That's why I wrote:

[...snip...]
> (Warning: untested pseudocode)

[...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 ...... (?)

Nov 17 '05 #6

P: n/a
GTi wrote:
[...snip...]
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.

[...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"...
Nov 17 '05 #7

P: n/a
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" <mi********************@lvrREMOVE.CAPSde> wrote in message
news:42a02e57$1@news...
GTi wrote:
[...snip...]
> 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.

[...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"...

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.