Adding bulk items/subitens in Listview

Asked

Viewed 1,875 times

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 subheadings 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 first column of Listview will be the names.
In the 2nd column the cities (sometimes the city will not be mentioned).

How could I do that?

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

Thanks in advance.

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;
  • It worked 100%. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.