Autor Beitrag
StrikeLucky
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 35



BeitragVerfasst: Sa 19.12.09 02:36 
Hallo,

ich habe ein kleines Problem mit einer repeat Schleife und der ListBox.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
    
repeat
  PositionEx1 := PosEx('<span class="names">', sHtml.Text, PositionEx1 + 20);
  if PositionEx1 <> 0 then
  begin
    PositionEx2 := PosEx('</span>', sHtml.Text, PositionEx1);
    if PositionEx2 <> 0 then
    begin
      ListBox.Items.Add(Copy(sHtml.Text, PositionEx1 + 20, PositionEx2 - PositionEx1 - 20));
      ....


Nun wird ja jeder gefundene Wert in die ListBox eingetragen.
Allerdings möchte ich das nur jeder zweite gefundene Wert in die ListBox eingetragen wird.
Hat jemand für mich einen Denkansatz?

Vielen Dank
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19274
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Sa 19.12.09 09:49 
Hallo, :welcome: ;-)

Du kannst dafür eine Boolean Variable nehmen, deren Status du jeweils umschaltest.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var
  AddCurrent: Boolean;
begin
  ...
  AddCurrent := True;
  ...
  if AddCurrent then
    ListBox.Items.Add(...
  AddCurrent := not AddCurrent;
  ...
StrikeLucky Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 35



BeitragVerfasst: Sa 19.12.09 20:22 
Besten Dank für deine Hilfe.
Dazu habe ich aber noch eine kleine Frage, normalerweise schalte ich Boolean Variablen immer mit := false bzw. := true um.
Du benutzt hier AddCurrent := not AddCurrent, was hat das für Vorteile?

Edit// Irgend wie kapier ich nicht wo ich die wo ich die Variable wieder auf auf true schalten muss. Denn wenn ich das innerhalb der repeat Schleiße tue, wird ja doch wieder jeder Eintrag in die ListBox hinzugefügt. Irgend wo muss ich ein Denkfehler haben :D
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19274
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 20.12.09 00:05 
Du musst die gar nicht direkt auf true schalten, sondern nur von true auf false und umgekehrt.
Und da "not true" dann ja false ist und "not false" wiederum true... ;-)
Es reicht also den Wert auf das Gegenteil zu setzen, darum not.
StrikeLucky Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 35



BeitragVerfasst: So 20.12.09 01:49 
Also ich habe es jetzt so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
repeat
  PositionEx1 := PosEx('<span class="names">', sHtml.Text, PositionEx1 + 20);
  if PositionEx1 <> 0 then
  begin
    PositionEx2 := PosEx('</span>', sHtml.Text, PositionEx1);
    if PositionEx2 <> 0 then
    begin
      if AddCurrent then
      begin
        ListBox1.Items.Add(Copy(sHtml.Text, PositionEx1 + 20, PositionEx2 - PositionEx1 - 20));
        AddCurrent := not AddCurrent;
      end;
    ....


AddCurrent schalte ich schon vor der repeat Schleife auf true. Mein Problem ist jetzt einfach, das ich nicht weis wann und wo ich AddCurrent wieder auf true schalten muss. Denn so wie es jetzt ist, wird AddCurrent nie mehr auf true gesetzt und somit wird nur ein einziger Eintrag in die ListBox hinzugefügt.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19274
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 20.12.09 06:51 
user profile iconStrikeLucky hat folgendes geschrieben Zum zitierten Posting springen:
Denn so wie es jetzt ist, wird AddCurrent nie mehr auf true gesetzt und somit wird nur ein einziger Eintrag in die ListBox hinzugefügt.
Richtig, denn du hast es nicht so gemacht wie ich...
Bei mir ist die Zeile, in der von true auf false und umgekehrt geschaltet wird, nicht umsonst nicht in der if-Abfrage drin.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
      if AddCurrent then
        ListBox1.Items.Add(Copy(sHtml.Text, PositionEx1 + 20, PositionEx2 - PositionEx1 - 20));
      AddCurrent := not AddCurrent;
    ....
Und schon klappt es...
StrikeLucky Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 35



BeitragVerfasst: So 20.12.09 21:22 
Ahhh jetzt habe ich es verstanden, hat zwar etwas gedauert, aber besser spät als nie xD
Danke jaenicke, BIG BIG THX