Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Copying the content of a Listview to an array.- Or how can I select a row in my program?


I have 3 columns of data I read in from a file to an array and then load into a Listview using the Add function.

I then change, add or delete some items in the Listview. Finally I would like to dump the Listview content bask to the
array and then to a file.

However, I have not been able to find a way to move the content from the Listview to the array. Mainly because
I cannot find how to select an item from my program.

If I could just select e.g. Item [0], then I could loop through the Listview and then copy the subitems to my arrays.

I look forward to hear any suggestions.

Best regards

Poul Thomsen
0
PoulHThomsen
Asked:
PoulHThomsen
  • 2
  • 2
1 Solution
 
rcarlanCommented:
You do not need to select a list item to extract the list items' data from a ListView.

The simplified object model for ListView is:

ListView ---> (1) ListViewItemCollection ---> (*) ListViewItem ---> (1) ListViewSubItemCollection ---> (*) ListViewSubItem

In actual fact, a ListView maintains several ListViewItemCollection (not just one, as shown above) - there's one that contains all items, one that contains selected items, and one for checked items. But, for the purposes of extracting all items - which is what I understand you want - I simplified the object model a bit.

You get the ListViewItemCollection containing all items (rows) in a ListView through the ListView.Items property. The collection can be navigated like any other collection (including foreach). Each item in this collection is a ListViewItem.
You get the ListViewSubItemCollection containing all sub-items (columns) through the ListViewItem.SubItems property. Once again, you can navigate this collection in the usual way. Each item in this collection is a ListViewSubItem.

However, since you already populate the ListView, you would've been presumably aware of all this. So, it's possible I have misunderstood your question.

Radu
0
 
PoulHThomsenAuthor Commented:
Hello Radu and thanks for your answer.

I must admit that I am quite a beginner to this platform and language, so my code for this application are
based on examples and tutorials e.g. in the Visual Studio Guide comming with the Software.

However, I find it difficult to find my way around in the help files describing e.g. the listview class. The same
'sample code' where you can populate the listview is repeated many times, but that is it.

But there is no information about how to navigate in the listview in a program.

I would be grateful if you could provide a codesnippet or a reference to your solutions.

Best regards

Poul Thomsen
0
 
rcarlanCommented:
Hi Poul,

I feel for your predicament. Doing .NET programming in C++ is not the easiest or most natural thing to do.

I do all my .NET programming in C#. Unless you have constraints that force you to use the managed extensions for C++, I would advise to give C# a go. It has been specifically designed for .NET programming. Coming from C++ you'll find it takes no time at all to learn C#. And you can still do your low level programming in C++, if you so desire.

Anyway, the following code snippet shows how to navigate the ListView object model:

System::Windows::Forms::ListView* pListView = GetMyListView();   //  replace this with your access to the ListView control
System::Windows::Forms::ListView::ListViewItemCollection* pLVItems = pListView->Items;
for (int i=0; i<pLVItems->Count; ++i)
{
      System::Windows::Forms::ListViewItem* pLVItem = pLVItems->Item[i];
      System::Windows::Forms::ListViewItem::ListViewSubItemCollection* pLVSubItems = pLVItem->SubItems;
      for (int j=0; j<pLVSubItems->Count; ++j)
      {
            System::Windows::Forms::ListViewItem::ListViewSubItem* pLVSubItem = pLVSubItems->Item[j];
            System::String* psCellText = pLVSubItem->Text;      // this is the text in the corresponding row-column cell
      }
}

Now, the same code in C#:

using System.Windows.Forms;

ListView listView = GetMyListView();   //  replace this with your access to the ListView control
foreach (ListViewItem lvItem in listView.Items)
{
      foreach (ListViewItem.ListViewSubItem lvSubItem in lvItem.SubItems)
      {
            string cellText = lvSubItem.Text;
      }
}

I cheated a bit. I could have obviously used a using statement in the C++ code, too.
Even so, you have to admit the C# code is a lot clearer (and less to type, too).

Radu
0
 
PoulHThomsenAuthor Commented:
Hello Radu.

That did it indeed.

Thanks a lot for your help.

I wish I started this in C#. In case I run into more big problems, I will evaulate to change language. But at this time I have the most of the functions in my program working. This was one of the last 'hurdles'

Thanks again. I will now see how I can stamp you answer as accepted.

Best regards

Poul Thomsen.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now