Autor Beitrag
magic87
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45


Delphi 7, PHP
BeitragVerfasst: Mo 12.02.07 10:52 
Hallo!

Habe irgendwie ein Problem beim durchsuchen der Listbox. Mein Problem sieht folgendermaßen aus:

Ich habe eine sortierte Listbox mit zB 5 Einträgen.
In einem Edit gebe ich das zu suchende Wort ein.
Jetzt nur noch auf den Button klicken und er soll das Wort finden und zB. eine Showmesage mit gefunden oder eine Showmessage mit nicht gefunden ausgeben.
Bei mir gibt er doch so viele Showmessages aus, wieviele Einträge vorhanden sind. Und an der Richtigen Stelle gibt er auch 'gefunden aus'! Aber ich will ja nur eine showmessage. Wie mache ich das?
Hier ist auch nochmal mein Quelltext:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
begin
  suchwort := edSuche.Text;
  for i:=1 to lsbBestand.items.count do
  begin
    Auto := TAuto(lsbBestand.Items.Objects[i-1]);
    if Auto.marke = edSuche.Text then
    begin
      showmessage('gefunden in Zeile ' + intToStr(i));
    end else showmessage('nichts gefunden!');
  end;
end;


Müsste die if-Abfrage nach der Schleife? Aber wie mache ich das?Ich hoffe mir kann jemand bei meinem kleinen Denkfehler helfen.

Liebe Grüße

magic87
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 12.02.07 11:58 
wie wär es einfach die strings in einem string zu sammeln und erst am schluss ausgeben. wie bspw. if länge(string_nichts_gefunden) > 0 then showmessage.... <HTH>
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mo 12.02.07 12:06 
Nimm eine Variable vom Typ Integer und setze sie vorher auf -1:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
begin  
  Found := -1;
  Suchwort := edSuche.Text;  

  for I:= 1 to lsbBestand.Items.Count do  
  begin  
    Auto := TAuto(lsbBestand.Items.Objects[I - 1]);  

    if Auto.Marke = edSuche.Text then  
      Found := I;
  end;  

  if Found = -1 then
    ShowMessage('Nichts gefunden.')
  else
    ShowMessage('Gefunden an Position ' + IntToStr(Found));
end;

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)


Zuletzt bearbeitet von GTA-Place am Mo 12.02.07 12:48, insgesamt 2-mal bearbeitet
Aya
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1964
Erhaltene Danke: 15

MacOSX 10.6.7
Xcode / C++
BeitragVerfasst: Mo 12.02.07 12:31 
user profile iconGTA-Place hat folgendes geschrieben:
Nimm eine Variable vom Typ Integer und setze sie vorher auf -1:

Warum nich der einfachheit halber nen Boolean? :P

Meoow
Aya~

_________________
Aya
I aim for my endless dreams and I know they will come true!
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mo 12.02.07 12:36 
Wollte ich auch zuerst, aber so bekommt er die Zeile, in der das Wort gefunden wurde.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Aya
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1964
Erhaltene Danke: 15

MacOSX 10.6.7
Xcode / C++
BeitragVerfasst: Mo 12.02.07 12:47 
Mhh.. ich sollte wieder schlafen gehen... :? Ich hab dein I für ne 1 gehalten..

Aya~

PS: Mach da mal Code-Blöcke-dingens da um deinen code

_________________
Aya
I aim for my endless dreams and I know they will come true!
magic87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45


Delphi 7, PHP
BeitragVerfasst: Mo 12.02.07 13:20 
Danke an euch! Hat super geklappt! Habe es jetzt so in der Art wie GTA-Place gemacht aber wie Aya meinte mit nem Boolean! War dann gar nicht so schwer! Vielen dank nochmal! Aber ein kleines Problem gibt es! Er gibt mir immer die gleiche Zeile aus! Immer die letzte. Irgendwie komisch. Versuche das noch rauszufinden. Hier nochmal mein geänderter Quellcode:

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:
var
  Auto     : TAuto;
  suchwort : string;
  i        : integer;
  found    : boolean;  

begin
  found := false;
  suchwort := edSuche.Text;
  for i:=1 to lsbBestand.items.count do
  begin
    Auto := TAuto(lsbBestand.Items.Objects[i-1]);
    if Auto.marke = edSuche.Text then
    begin
    found := true;
    end;
  end;
    if found = true then
    begin
      showmessage('gefunden in Zeile ');
    end else showmessage('nichts gefunden!');
end;
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mo 12.02.07 13:31 
Wenn du die Zeile haben willst, musst du meine Methode nehmen, oder 2 Variablen (Boolean und Integer).

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
magic87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45


Delphi 7, PHP
BeitragVerfasst: Mo 12.02.07 14:25 
muss aber doch auch irgendwie mit meinem jetztigen Quellcode gehen! Bräuchte ja auch nur einen Tipp. Will nur gerne den Quellcode von dir nehmen. Hoffe, das es für meinen jetzigen Quellcode eine Lösung gibt. Wenn jemand was weiß, bitte sofort posten!
Vielen Dank und Liebe Grüße!
Dennis
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mo 12.02.07 16:15 
Einfach wieder Integer statt Boolean benutzen, genau wie in meinem Source oben.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Mo 12.02.07 16:28 
oder so:

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:
var
  Auto     : TAuto;
  suchwort : string;
  i        : integer;
  found    : boolean;  

begin
  found := false;
  suchwort := edSuche.Text;
  for i:=1 to lsbBestand.items.count do
  begin
    Auto := TAuto(lsbBestand.Items.Objects[i-1]);
    if Auto.marke = edSuche.Text then
    begin
    found := true;
    break; //Schleifenabbruch
    end;
  end;
    if found = true then
    begin
      showmessage('gefunden in Zeile ');
    end else showmessage('nichts gefunden!');
end;


es ginge auch so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
 suchwort := edSuche.Text;
 i:=0;
 found:=false;
 while (found=false) and (i<lsbBestand.items.count) do
 begin
  inc(i);
  Auto := TAuto(lsbBestand.Items.Objects[i-1]);
  if Auto.marke = edSuche.Text then
   found:=true;
 end;
 
 if found = true then
  showmessage('gefunden in Zeile ');
 else showmessage('nichts gefunden!');
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mo 12.02.07 16:39 
Bei deiner 1. Methode weiß er immer noch nicht in welcher Zeile es gefunden wurde. Bei deiner 2. Methode würde es gehen.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)