Affichage fichier texte

dino34 - 7 déc. 2015 à 13:33
 Yanb - 19 janv. 2016 à 07:52
Bonjour,

je cherche à remplir un listview avec le texte suivant ,



SHOW INFO

START

CAB VAR VAR2 VAR3
1 EDA 1F RST-6
1 EDA 1F RTA-6
1 EDA 1F BB-6


CAB VAR VAR2 VAR3
20 EBB 1F RST-23
20 EBB 1F RTA-23
20 EBB 1F CC-23

END


j'ai utilisé le code suivant pour afficher ( CAB & VAR3) dans la colone 2et 3

d'un listview




procedure TForm1.Button1Click(Sender: TObject);
var S, W: TStringList;
L: TListItem;
i, j: integer;
begin
S:=TStringList.Create;
W:=TStringList.Create;
try

S.LoadFromFile('c:\test.txt');
LV.Items.BeginUpdate;
LV.Items.Clear;
LV.AllocBy:=S.Count;
for i:=0 to S.Count-1 do
begin L:=LV.Items.Add;
W.CommaText:=S[i];
L.Caption:=W[2];
for j:=1 to W.Count-1 do
if w[3] = 'BB-6' then
L.SubItems.Add(W[3]);

if w[3] = 'CC-6' then
L.SubItems.Add(W[3]);
end;
LV.Items.EndUpdate;
finally
S.Free;
W.Free;
end;
end;
end.




COL2 COL3
CAB VAR3
1 BB-6
20 CC-23




NB: après compilation , j'ai le code d'erreur suivant:

list index out of bound (2).

il y a pas assez d'example dans le net pour le traitement de fichier texte et

listview


merci pour votre support

15 réponses

Salut,
exemple :
var
  S,W: TStringList;
  I,J: Integer;
  NewColumn: TListColumn;
begin
  ListView1.ViewStyle := vsReport;
  for I := 1 to 2 do
  begin
    NewColumn := ListView1.Columns.Add;
    NewColumn.Caption := Format('Col %d',[I]);
  end;
  ListView1.Items.Add;
  ListView1.Items[ListView1.Items.Count-1].Caption := 'CAB';
  ListView1.Items[ListView1.Items.Count-1].SubItems.Add('VAR3');
  S := TStringList.Create;
  W := TStringList.Create;
  try
    S.LoadFromFile('C:\Test.txt');
    for I := 0 to S.Count - 1 do
    begin
      W.CommaText := S[I];
      for J := 0 to W.Count - 1 do
      begin
        if W.Count > 3 then
        begin
          if (W[3] = 'BB-6') or (W[3] = 'CC-23') then
          begin
            if J = 0 then
            begin
              ListView1.Items.Add;
              ListView1.Items[ListView1.Count-1].Caption := W[J];
            end;
            if J = 3 then
              ListView1.Items[ListView1.Items.Count-1].SubItems.Add(W[J]);
          end;
        end;
      end;
    end;
  finally
    S.Free;
    W.Free;
  end;
end;
@+
0
Rejoignez-nous