ListView SelectedIndexChanged and accessing the previous value

D

David Jackson

Hello,

I'm using VS.NET 2005.

I have a ListView populated with data pulled from a SQL Server database.

I have wired up a SelectedIndexChanged event, but I can't find any way of
retrieving the value of the previously selected record.

For example, if the ListView looks like this:

Record A
Record B
Record C
Record D
Record E
Record F

where Record B currently selected.

If the user then clicks on Record E, the SelectedIndexChanged event fires,
of course, but from within the event itself I can't seem to find any way of
finding out what the previously selected record was. Based on the event
model for some other controls, I thought there might have been a
SelectedIndexChanging event, but there isn't.

Obviously, a workround is simple enough - store the value of the currently
selected record in a form-level variable, but is that the best / only way to
do this?

Thank you,

DJ
 
N

Nicholas Paldino [.NET/C# MVP]

David,

Pretty much, yes. As you stated, if you wanted the previous value, a
SelectedIndexChanging event would have been perfect.

It's easy enough to derive a class from ListView though and then add the
method yourself, and encapsulate the storing of the state in your derived
class.
 
D

David Jackson

Pretty much, yes.

OK, cool. I thought that would be the case, but thanks for the confirmation.
As you stated, if you wanted the previous value, a SelectedIndexChanging
event would have been perfect.

Maybe that will find its way into a future version. I do understand, though,
that the Framework can't possibly include every single piece of
functionality that every single developer in the entire world could ever
possibly need, otherwise it would be too big to install :)
It's easy enough to derive a class from ListView though and then add the
method yourself, and encapsulate the storing of the state in your derived
class.

I see. I've never done anything like that before, but I can appreciate that
it would be a useful exercise in my learning curve.

Thanks again.

DJ
 

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