Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. I can write a ListView record but I can't read one

I can write a ListView record but I can't read one

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++visual-studiohelp
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BuckBrown
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • B BuckBrown

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      B 2 Replies Last reply
      0
      • C Christian Graus

        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 )

        B Offline
        B Offline
        BuckBrown
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • B BuckBrown

          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

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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:

          B 1 Reply Last reply
          0
          • C Christian Graus

            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 )

            B Offline
            B Offline
            BuckBrown
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • B BuckBrown

              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

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              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:

              B 1 Reply Last reply
              0
              • M Mark Salsbery

                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:

                B Offline
                B Offline
                BuckBrown
                wrote on last edited by
                #7

                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

                M 1 Reply Last reply
                0
                • B BuckBrown

                  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

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  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:

                  B 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    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:

                    B Offline
                    B Offline
                    BuckBrown
                    wrote on last edited by
                    #9

                    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

                    M 1 Reply Last reply
                    0
                    • B BuckBrown

                      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

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #10

                      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:

                      B 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        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:

                        B Offline
                        B Offline
                        BuckBrown
                        wrote on last edited by
                        #11

                        Yeah it makes sense, but I didn't know that ListViewItemCollection was of type ListViewItem. Just not intuitively obvious when writing code. Buck

                        1 Reply Last reply
                        0
                        • M Mark Salsbery

                          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:

                          B Offline
                          B Offline
                          BuckBrown
                          wrote on last edited by
                          #12

                          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

                          M 1 Reply Last reply
                          0
                          • B BuckBrown

                            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

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #13

                            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:

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups