I can write a ListView record but I can't read one
-
Hi, I'm using C++/CLI with Visual Studio 2005. I have finally gotten my ListView control to function properly when I add records to the control because there is a this->myListView->Items->Add(record). But the this->myListView->Items::get() function returns a ListViewItemCollection^ but the code will not let me define a ListViewItemCollection^ to read the collection into. I have been Googling this all day it's gotten to be hair pulling time. I have a ListView with 4 columns and all I want to do is walk the list, read each record, and sperate the record's fields into the appropriate variables. Any help would be appreciated. Thanks, Buck
-
Hi, I'm using C++/CLI with Visual Studio 2005. I have finally gotten my ListView control to function properly when I add records to the control because there is a this->myListView->Items->Add(record). But the this->myListView->Items::get() function returns a ListViewItemCollection^ but the code will not let me define a ListViewItemCollection^ to read the collection into. I have been Googling this all day it's gotten to be hair pulling time. I have a ListView with 4 columns and all I want to do is walk the list, read each record, and sperate the record's fields into the appropriate variables. Any help would be appreciated. Thanks, Buck
I don't think you can create one, it needs to be attached to a listview. Read the items directly, or create a typed List to copy them into with foreach, if you must.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
I don't think you can create one, it needs to be attached to a listview. Read the items directly, or create a typed List to copy them into with foreach, if you must.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Yes, reading the items directly would be fine. How do you do that? I can define the record and while trapped in the debugger I can see the information but it is of type ListViewSubItemCollection^ and the compiler will not let me define one. Here is the code. ListViewItem^ myRecord = gcnew ListViewItem(); myRecord = this->myListView->Items[0]; ListViewSubItemCollection^ myCollection = myRecord->SubItems::get(); // It won't let me define this collection. If I trap after assigning Items[0] to myRecord I can go to either the Autos or Locals tab and expand myRecord and I can see subItems collection with the data or expand the SubItems and I can see the subItems collection. I just need the correct syntax to read the data from the ListView. Buck
-
Yes, reading the items directly would be fine. How do you do that? I can define the record and while trapped in the debugger I can see the information but it is of type ListViewSubItemCollection^ and the compiler will not let me define one. Here is the code. ListViewItem^ myRecord = gcnew ListViewItem(); myRecord = this->myListView->Items[0]; ListViewSubItemCollection^ myCollection = myRecord->SubItems::get(); // It won't let me define this collection. If I trap after assigning Items[0] to myRecord I can go to either the Autos or Locals tab and expand myRecord and I can see subItems collection with the data or expand the SubItems and I can see the subItems collection. I just need the correct syntax to read the data from the ListView. Buck
Get is a "getter" method for a property. You don't call it directly. Does this work? ListViewSubItemCollection^ myCollection = myRecord->SubItems; Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I don't think you can create one, it needs to be attached to a listview. Read the items directly, or create a typed List to copy them into with foreach, if you must.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
I've got it now. String^ field_1; String^ field_2; String^ field_3; for each(ListViewItem^ record in myListView->Items) { field_1 = record->SubItem[0]->Text; field_2 = record->SubItem[1]->Text; field_3 = record->SubItem[2]->Text; } I had to figure this one out on my own. Any example I found on the web did not work for me. I'm amazed that reading from a ListView was not covered in Stephen Frasers book on C++/CLI which is very in depth. I could not find any example of this in the Microsoft documentation either. I guess we're supposed to know this though some kind of mental osmossis. Thanks, Buck
-
I've got it now. String^ field_1; String^ field_2; String^ field_3; for each(ListViewItem^ record in myListView->Items) { field_1 = record->SubItem[0]->Text; field_2 = record->SubItem[1]->Text; field_3 = record->SubItem[2]->Text; } I had to figure this one out on my own. Any example I found on the web did not work for me. I'm amazed that reading from a ListView was not covered in Stephen Frasers book on C++/CLI which is very in depth. I could not find any example of this in the Microsoft documentation either. I guess we're supposed to know this though some kind of mental osmossis. Thanks, Buck
That's fine. You can also do this, like you were trying to do before...
ListViewItem^ myRecord = this->listView1->Items[0];
System::Windows::Forms::ListViewItem::ListViewSubItemCollection^myCollection = myRecord->SubItems;String ^field_1 = myCollection[0]->Text;
String ^field_2 = myCollection[1]->Text;
String ^field_3 = myCollection[2]->Text;Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Get is a "getter" method for a property. You don't call it directly. Does this work? ListViewSubItemCollection^ myCollection = myRecord->SubItems; Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Hi Mark, No, if I try to define like that the compiler complains that ListViewSubItemCollection^ is not defined (the namespace System::Collections is defined). I get the same error if I try to define a ListViewItemCollection^ or if I try to use gcnew with them. Yeah, I usually don't call ::get(). I will type it out that way so that I can see through the intellisense what type the get() function is going to return and then I can define an appropriate variable. If you ever find out of a way to declare a ListViewItemCollection^ or ListViewSubItemCollection^ email me if you can. Thanks, Buck
-
Hi Mark, No, if I try to define like that the compiler complains that ListViewSubItemCollection^ is not defined (the namespace System::Collections is defined). I get the same error if I try to define a ListViewItemCollection^ or if I try to use gcnew with them. Yeah, I usually don't call ::get(). I will type it out that way so that I can see through the intellisense what type the get() function is going to return and then I can define an appropriate variable. If you ever find out of a way to declare a ListViewItemCollection^ or ListViewSubItemCollection^ email me if you can. Thanks, Buck
See my code in my most recent post - I tested it, it works fine. The compiler will always complain if it doesn't know the type. The "ListViewItem::ListViewSubItemCollection" class is not in "System::Collections". It's in System::Windows::Forms. "ListViewSubItemCollection" is a subclass nested class in the "ListViewItem" class. The fully qualified type is "System::Windows::Forms::ListViewItem::ListViewSubItemCollection" Of course, if you have "using System::Windows::Forms;" somewhere above, you can shorten the type to "ListViewItem::ListViewSubItemCollection". Make sense? Mark
Last modified: 22mins after originally posted --
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
That's fine. You can also do this, like you were trying to do before...
ListViewItem^ myRecord = this->listView1->Items[0];
System::Windows::Forms::ListViewItem::ListViewSubItemCollection^myCollection = myRecord->SubItems;String ^field_1 = myCollection[0]->Text;
String ^field_2 = myCollection[1]->Text;
String ^field_3 = myCollection[2]->Text;Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks Mark, I hate those kind of full namespace declarations. I really don't understand why sometimes you have to include the entire namespace name even though the namespace has already been defined with the 'using' keyword. It wouldn't be so bad if there was a list of types that require the full "path" for lack of a better word. Buck
-
Thanks Mark, I hate those kind of full namespace declarations. I really don't understand why sometimes you have to include the entire namespace name even though the namespace has already been defined with the 'using' keyword. It wouldn't be so bad if there was a list of types that require the full "path" for lack of a better word. Buck
BuckBrown wrote:
I really don't understand why sometimes you have to include the entire namespace
You don't, if you use "using"...
using namespace System::Windows::Forms;
...
ListViewItem^ myRecord = this->listView1->Items[0];
ListViewItem::ListViewSubItemCollection^myCollection = myRecord->SubItems;
String ^field_1 = myCollection[0]->Text;
...Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
See my code in my most recent post - I tested it, it works fine. The compiler will always complain if it doesn't know the type. The "ListViewItem::ListViewSubItemCollection" class is not in "System::Collections". It's in System::Windows::Forms. "ListViewSubItemCollection" is a subclass nested class in the "ListViewItem" class. The fully qualified type is "System::Windows::Forms::ListViewItem::ListViewSubItemCollection" Of course, if you have "using System::Windows::Forms;" somewhere above, you can shorten the type to "ListViewItem::ListViewSubItemCollection". Make sense? Mark
Last modified: 22mins after originally posted --
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
BuckBrown wrote:
I really don't understand why sometimes you have to include the entire namespace
You don't, if you use "using"...
using namespace System::Windows::Forms;
...
ListViewItem^ myRecord = this->listView1->Items[0];
ListViewItem::ListViewSubItemCollection^myCollection = myRecord->SubItems;
String ^field_1 = myCollection[0]->Text;
...Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
This is a good example of how to determine if I need to include the preceding ListViewItem:: (or any other preceding datatype::). I have run into declaration problems like this before but those first two lines after the ellipsis are illustrative of the approach. I am printing that one for my notes. Buck
-
This is a good example of how to determine if I need to include the preceding ListViewItem:: (or any other preceding datatype::). I have run into declaration problems like this before but those first two lines after the ellipsis are illustrative of the approach. I am printing that one for my notes. Buck
I didn't state that correctly, and I modified my post above... ListViewSubItemCollection is a subclass nested class in the ListViewItem class. It's the same thing you'd have to do with regular C++... the compiler has to know the class and the namespace :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: