Search code examples
listviewc++-cli

How to split text when read from file to Listview c++?


This code shows all text in first column of ListView, but I need to split it by columns. I tried to write Split('|') after ReadLine() but it gives an error.

    String^ textFile = String::Concat("C:\\p.txt");
    StreamReader^ reader = gcnew  StreamReader(textFile);
    do
    {
        listView1->Items->Add(reader->ReadLine());

    } while (reader->Peek() != -1);

Solution

  • You need to add the sub items separately. So you also need to split the string ahead of adding it...

    String^ stitems[] = reader->ReadLine()->Split('|');
    
    ListViewItem^ item1 = gcnew ListViewItem( "item1",0 );
    for each (String^ x in stitems) item1->SubItems->Add(x);
    

    There's a good c++/cli example here... MSDN