Help with ListView control in Managed C++ .NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I was wondering how I can use a listview effectively in managed c++
..net. Basically I'm trying to get the index of the selected item in
the list view, then get the "tag" attribute of that item to invoke
data in other area of my app.

Here's some example code:
//////////////////////////////////////

private: System::Void lstTeams_SelectedIndexChanged(System::Object *
sender, System::EventArgs * e)
{
int index = lstTeams->SelectedIndices->get_Item(0);

}
//////////////////////////////////////

That's basically just assigning the index of my selected item to int
"index". Is this correct? When I go to my form and change to a
different item in my listview box, I get a "Specified argument was out
of range of valid values". Also, it APPEARS that when I change to a
different item in my listview, lstTeams_SelectedIndexChanged fires
TWICE. Any idea why?

I've been working of this project lately, and these .NET GUI controls
are really binding me.

Thanks,
Kyle
 
See if this helps.

private: System::Void lstTeams_SelectedIndexChanged(System::Object * sender,
System::EventArgs * e)
{
if (lstTeams->SelectedIndices && lstTeams->SelectedIndices->Count >
0) {
int index = lstTeams->SelectedIndices->get_Item(0);
// rest of your code here
}
}
 
Thanks for the very fast response!

The code works great! One last question:

How would I access this item's tag?

I'm thinking: lstTeams->SelectedItems->get_Item(index)

Is this correct?

Thanks much for your help, your a star!

-ReMEn
 
Here is how you would set the tag.

if (lstTeams->SelectedIndices && lstTeams->SelectedIndices->Count > 0) {
int index = lstTeams->SelectedIndices->get_Item(0);
ListViewItem* item = lstTeams->Items->Item[index];
item->Tag = S"Anything"; // can be a string, or another object
}
 
Here is how you would set the tag.

if (lstTeams->SelectedIndices && lstTeams->SelectedIndices->Count > 0) {
int index = lstTeams->SelectedIndices->get_Item(0);
ListViewItem* item = lstTeams->Items->Item[index];
item->Tag = S"Anything";
}
 
Here is how you would set the tag.

if (lstTeams->SelectedIndices && lstTeams->SelectedIndices->Count > 0) {
int index = lstTeams->SelectedIndices->get_Item(0);
ListViewItem* item = lstTeams->Items->Item[index];
item->Tag = S"Anything";
}
 
Thanks for helping me out. Your examples work perfectly.

I searched all over the internet including MSDN for material on C++ .NET
listviews. Couldn't find what I needed. Even went to Barnes n' Nobles to look
for a c++ .net ref book, turns out they are no help either.

Luckily for this community, it helped me out :)

*Bookmarked*

TY again

ReMEn
 
Back
Top