Adding Bulk Items / Subitems in the ListView

1

Good afternoon,

I'm trying to add bulk items to a ListView.

I need to put the text of a Memo separated by semicolons (;) as items and subitems in this ListView.

Example:

  

Text typed in Memo:
  João da Silva; Curitiba - PR
  Maria Oliveira; São Paulo - SP
  Roberto Silva
  Pedro Miranda; Porto Alegre - RS

In the 1st column of the ListView will be the names. In the 2nd column the cities (sometimes the city will not be mentioned).

How could I do this?

It seems simple, but when searching the internet I found only very complicated examples.

Thank you in advance.

    
asked by anonymous 18.09.2017 / 20:20

1 answer

0
    procedure TForm3.Button1Click(Sender: TObject);
    Var Item: TListItem;
        Conta, Posicao : Integer;
    begin

      // pega o memo e adiciona no listview
      ListView1.Items.Clear;

      for Conta := 0 to Memo1.Lines.Count-1 do
      begin
        Item := ListView1.Items.Add;
        Posicao := Pos('-', Memo1.Lines[Conta]);

        if (Posicao = 0) then
          Item.Caption := Memo1.Lines[Conta]
        else
        begin
          Item.Caption := Copy(Memo1.Lines[Conta],0,Posicao-1);
          Item.SubItems.Add(Copy(Memo1.Lines[Conta],Posicao+1, Length(Memo1.Lines[Conta])));
        end;
      end;
    end;

    procedure TForm3.FormCreate(Sender: TObject);
    var
      Col : TListColumn;
    begin
      ListView1.ViewStyle := TViewStyle.vsReport;

      // Criando titulo das colunas
      Col := ListView1.Columns.Add;
      Col.Caption := 'Cidade';
      Col.Alignment := taLeftJustify;
      Col.Width := 200;

      Col := ListView1.Columns.Add;
      Col.Caption := 'Estado';
      Col.Alignment := taLeftJustify;
      Col.Width := 100;
      // Fim criando titulos das colunas

      // Preenchendo o memo
      Memo1.Lines.Clear;
      Memo1.Lines.Add('João da Silva;Curitiba - PR');
      Memo1.Lines.Add('Maria Oliveira;São Paulo - SP');
      Memo1.Lines.Add('Roberto Silva');
      Memo1.Lines.Add('Pedro Miranda;Porto Alegre - RS');
      // Fim Preenchendo o memo
    end;
    
19.09.2017 / 01:15