Get Text from ListView Control - Managed C++ NET

P

Pantokrator

Hi,
I've searched the web and MSDN but couldn't find adequate information
on retrieving the text of a subitem of a listviewitem.

What I want to do is simple. I have a listview control with 2 columns
and a large number of listviewitems.
Every time I click on a row (one of the listviewitems), I would like
to get the String* representation of the second column cell.

First I get the index as follows:

int ind=0;

for (int j=0; j<listView1->Items->Count; j++)
{
if (listView1->Items->Item[j]->Selected)
ind = listView1->Items->Item[j]->Index;

}

Then, I get my listviewitem pointer as follows:
ListViewItem* item = listView1->Items->Item[ind];

if I write:
item->Text;
it will return me the string of the first column

What do I do after that in order to get the String* type of the second
subitem??

Many thanks in advance
 
B

Ben Voigt [C++ MVP]

Pantokrator said:
Hi,
I've searched the web and MSDN but couldn't find adequate information
on retrieving the text of a subitem of a listviewitem.

What I want to do is simple. I have a listview control with 2 columns
and a large number of listviewitems.
Every time I click on a row (one of the listviewitems), I would like
to get the String* representation of the second column cell.

First I get the index as follows:

int ind=0;

for (int j=0; j<listView1->Items->Count; j++)
{
if (listView1->Items->Item[j]->Selected)
ind = listView1->Items->Item[j]->Index;

}

Then, I get my listviewitem pointer as follows:
ListViewItem* item = listView1->Items->Item[ind];

if I write:
item->Text;
it will return me the string of the first column

What do I do after that in order to get the String* type of the second
subitem??

Did you look at the SubItems property?

Anyway, you should upgrade to C++/CLI as soon as possible. Managed
Extensions for C++ is buggy and abandoned by Microsoft.
 
P

Pantokrator

Hi Ben, you are right...I've spent enormous time just for one control
and all i want to do is something easy.
But I'm stuck you see, because I've written a lot of code and if I
change back to MFC it will take me time to re-write everything.
I've looked at the SubItems property but there isn't anything 'sound'
to say how to get the test from a subitem.
OK. you can easily add a subitem like item1->SubItems->Add(S"1"); but
how do you get that subitem back in a string....I don't understand...
This is from MSDN:

Property Value
A ListViewItem.ListViewSubItemCollection that contains the subitems.

Remarks
Using the ListViewItem.ListViewSubItemCollection, you can add
subitems, remove subitems, and obtain a count of subitems....

Doesn't say anything on How to retrieve the text...

Any more thoughts?
Thanks again
 
B

Ben Voigt [C++ MVP]

Pantokrator said:
Hi Ben, you are right...I've spent enormous time just for one control
and all i want to do is something easy.
But I'm stuck you see, because I've written a lot of code and if I
change back to MFC it will take me time to re-write everything.

Wasn't suggesting going back to MFC. Was recommending you use the new
managed C++, C++/CLI, which comes with Visual Studio 2005, and totally
replaces "Managed Extensions for C++".
I've looked at the SubItems property but there isn't anything 'sound'
to say how to get the test from a subitem.
OK. you can easily add a subitem like item1->SubItems->Add(S"1"); but
how do you get that subitem back in a string....I don't understand...
This is from MSDN:

Property Value
A ListViewItem.ListViewSubItemCollection that contains the subitems.

Remarks
Using the ListViewItem.ListViewSubItemCollection, you can add
subitems, remove subitems, and obtain a count of subitems....

Doesn't say anything on How to retrieve the text...

It's a collection, you can get the members just like you did with Items.
Each member is a ListViewSubItem, with ForeColor, Font, Text properties...
 
P

Pantokrator

Thanks Ben, but unfortunately my company doesn't want to pay for any
vs 2005 upgrade and I desperately need to solve this problem.
Have you got any example to retrieve the text from the collection
ListViewSubItem?? I'm stuck unfortuantely

Thanks for your help
 
M

Massimo

I dont't know if I can help you but in the past I worked with C++ .NET
2003 and I read text from subitems in this way:

String *s = listview->Items->get_Item(0)->SubItems->get_Item(0)->Text;

Bye
Massimo


Pantokrator ha scritto:
 
B

Ben Voigt [C++ MVP]

Pantokrator said:
Thanks Ben, but unfortunately my company doesn't want to pay for any
vs 2005 upgrade and I desperately need to solve this problem.
Have you got any example to retrieve the text from the collection
ListViewSubItem?? I'm stuck unfortuantely

using VC++ 2005 Express is preferable to VS 2003 Managed Extensions for C++
because a lot of runtime bugs were fixed.
 

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