Help with ListView control in Managed C++ .NET

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
 
G

Guest

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
}
}
 
G

Guest

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
 
G

Guest

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
}
 
G

Guest

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";
}
 
G

Guest

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";
}
 
G

Guest

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
 

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