Autor Beitrag
Cool-Fux
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Di 10.04.07 20:08 
Hallo,

ich möchte bestimmte Werte aus einer Textdatei mit TStringList holen.
Die Textdatei ist so aufgebaut:
ausblenden Quelltext
1:
2:
3:
4:
5:
#NENN  H  B    S    T     T1  R  R2 D1   W1  W3    A OFL    GEW     IY      WY      IZ     

100    96 100  5.0  8.0   8.0 12 0  13.0  56  0   21.2 0.56  16.7    349    72.8   134   
120   114 120  5.0  8.0   8.0 12 0  17.0  66  0   25.3 0.67  19.9    606   106.0   231  
140   133 140  5.5  8.5   8.5 12 0  21.0  76  0   31.4 0.79  24.7   1030   155.0   389

usw.
Nach der Auswahl in der ComboBox2 z.B. 100 sollen die Werte 16.7 GEW und 72.8 WY in eine
StringGrid oder TEdit eingetragen werden.

Mein Code:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.ComboBox2Change(Sender: TObject);
var tempStringList : TStrings;
    aFile : String;
    i : Integer;
begin
  //ExtractFilePath(ParamStr(0)) = Anwendungsverzeichnis!
  aFile := ExtractFilePath(ParamStr(0))+'tab\'+ComboBox1.Text+'.txt';
  if FileExists(aFile) then
  begin
  tempStringList := TStringList.Create;
    try
    tempStringList.LoadFromFile(aFile);
    for i := 2 to tempStringList.Count -1 do//ab 2te Zeile, delete entfällt
      EditGewicht.Text:=(Copy(tempStringList[i],4,pos(' ',tempStringList[i])));
    finally
      tempStringList.Free;
      end;
    end;
end;


Wie kann man das am besten Umsetzen?

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Di 10.04.07 20:11 
Weise die Zeile(n) .DelimitedText zu, dann kannst du per Index(.Strings[x]) auf die Werte zugreifen

_________________
Markus Kinzler.
Cool-Fux Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Di 10.04.07 20:30 
Hallo,
Danke für die Schnelle Antwort.
Ich bin Anfänger.
DelimitedText kenn ich (noch) nicht!
Ein Beispiel mit Erklärung währe sehr hilfreich!
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Di 10.04.07 20:35 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
var 
    tempStringList, sl2 : TStrings; 
...
    tempStringList := TStringList.Create; 
    sl2 := TStringList.Create; 
...
    for i := 2 to tempStringList.Count -1 do
    begin
        sl2.DelimitedText := tempStringList[i];
        gew := sl2.Strings[14];
        wy := sl2.Strings[16];
...

_________________
Markus Kinzler.
Cool-Fux Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Di 10.04.07 21:03 
Hallo,

Mein Code sieht jetzt so aus:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
procedure TForm1.ComboBox2Change(Sender: TObject);
var tempStringList : TStrings;
    aFile : String;
    i : Integer;
begin
  //ExtractFilePath(ParamStr(0)) = Anwendungsverzeichnis!
  aFile := ExtractFilePath(ParamStr(0))+'tab\'+ComboBox1.Text+'.txt';
  if FileExists(aFile) then
  begin
  tempStringList := TStringList.Create;
    try
    tempStringList.LoadFromFile(aFile);
    for i := 2 to tempStringList.Count -1 do//ab 2te Zeile, delete entfällt
      begin
      tempStringList.DelimitedText := tempStringList[i];
        EditGewicht.Text := tempStringList.Strings[14];
        EditWerkstoff.Text := tempStringList.Strings[16];
      end;
    finally
      tempStringList.Free;
      end;
    end;
end;


Ich erhalte bei der ComboBox Auswahl folgenden Fehler:
Listenindex überschreitet das Maximum (14)

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Di 10.04.07 21:08 
Du hast meinen Code falsch übernommen, du benötigst 2 StringListen.

_________________
Markus Kinzler.
Cool-Fux Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Di 10.04.07 21:50 
Ich glaube jetzt funst es.
Nur werden die Werte aus der letzen Zeile ausgelesen!?
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Di 10.04.07 21:52 
Es werden die Werte aller Zeilen übernommen, aber immer wieder überschrieben.

_________________
Markus Kinzler.
Cool-Fux Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Mi 11.04.07 23:13 
Hallo,

ich habe immer noch Probleme mit meinen Code:

Code:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
procedure TForm1.ComboBox2Change(Sender: TObject);
var s11 : TStrings;
    s12 : TStrings;
    aFile : String;
    i : Integer;
begin
  //ExtractFilePath(ParamStr(0)) = Anwendungsverzeichnis!
  aFile := ExtractFilePath(ParamStr(0))+'tab\'+ComboBox1.Text+'.txt';
  if FileExists(aFile) then
  begin
    s11 := TStringList.Create;
    s12 := TStringList.Create;
    try
    s11.LoadFromFile(aFile);
    s12.LoadFromFile(aFile);
    for i := 2 to s11.Count-24 do//ab 2te Zeile, delete entfällt
      begin
        s12.DelimitedText := s11.Strings[i];
        if ComboBox2.Text =('100'then
        EditPreis.Text := s12.Strings[16];
        EditBreite.Text := s12.Strings[2];
        EditGewicht.Text := s12.Strings[13];
        EditOFL.Text := s12.Strings[12];
      end;
    finally
     s11.Free;
     s12.Free;
    end;
   end;
end;


Die erste Zeile nach If Combibox2.Text =('100') then wird nicht übergeben!
(EditPreis.Text := s12.strings[16];)

Wie kann ich jetzt noch die anderen Werte für 120, 140, 160 usw. in der ComboBox2 auf der Textdatei hohlen. Ich komme hier nicht richtig weiter.

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 11.04.07 23:30 
Schau Dir mal TStringList.CommaText an ;-)

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Cool-Fux Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Mi 11.04.07 23:46 
Hallo,
Vielleicht ein Beispiel?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 12.04.07 19:24 
Siehe Delphi-Hilfe oder FoSuFu. :mahn: Oder hast Du deren Installation aus Platzgründen ausgelassen? ;-)

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Cool-Fux Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Fr 13.04.07 13:44 
Hallo,
daten auslesen funst jetzt, aber wie kann ich jetzt noch erreichen
wenn ich in der ComboBox den Wert 100 auswähle die Werte aus der 5. Zeile ausgelesen werden
und bei den Wert 120 die Werte aus Zeile 7. usw.
Cool-Fux Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Mo 16.04.07 20:33 
Hallo,
hat keiner eine Idee?

Mein Code sieht jetzt so aus:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
procedure TForm1.ComboBox2Change(Sender: TObject);
var sl1 : TStrings;
    sl2 : TStrings;
    aFile : String;
    i : Integer;
begin
     //ExtractFilePath(ParamStr(0)) = Anwendungsverzeichnis!
  aFile := ExtractFilePath(ParamStr(0))+'tab\'+ComboBox1.Text+'.txt';
  if FileExists(aFile) then
  begin
    sl1 := TStringList.Create;
    sl2 := TStringList.Create;
    try
    sl1.LoadFromFile(aFile);
    sl2.LoadFromFile(aFile);
    if ComboBox2.Text =('100'then;
        sl2.CommaText := sl1.Strings[2];
        EditOFL.Text := sl2.Strings[12];
        EditGewicht.Text := sl2.Strings[13];
    if ComboBox2.Text =('120'then;
        sl2.CommaText := sl1.Strings[3];
        EditOFL.Text := sl2.Strings[12];
        EditGewicht.Text := sl2.Strings[13];
    if ComboBox2.Text =('140'then;
        sl2.CommaText := sl1.Strings[4];
        EditOFL.Text := sl2.Strings[12];
        EditGewicht.Text := sl2.Strings[13]
    finally
    sl1.Free;
    sl2.Free;
    end;
   end;
end;


Es funst immer noch nicht!
Es werde immer die Werte gleichen Werte ausgegeben, egal was man in der ComboBox2
auswählt.
Wenn ich die if Anweisung in begin und end setze werden keine Werte ausgegeben.
Bitte um Hilfe / Idee!

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt