Autor Beitrag
cromos
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: Sa 23.08.03 09:18 
Hallo

habe diesen Code im Internet gefunden. Er soll bewerkstelligen das man inerhalb eines ListView Suchen kann.

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:
34:
35:
36:
37:
38:
39:
40:
41:
42:
// Function to search items and select if found 
// Funktion, um Items zu suchen unda falls gefunden, sie danach markieren 

procedure LV_FindAndSelectItems(lv: TListView; const S: string; column: Integer); 
var 
  i: Integer; 
  found: Boolean; 
  lvItem: TListItem; 
begin 
  Assert(Assigned(lv)); 
  Assert((lv.ViewStyle = vsReport) or (column = 0)); 
  Assert(S <> ''); 
  for i := 0 to lv.Items.Count - 1 do 
  begin 
    lvItem := lv.Items[i]; 
    if column = 0 then 
      found := AnsiCompareText(lvItem.Caption, S) = 0 
    else if column > 0 then 
    begin 
      if lvItem.SubItems.Count >= Column then 
        found := AnsiCompareText(lvItem.SubItems[column - 1], S) = 0 
      else  
        found := False; 
    end 
    else 
      found := False; 
    if found then 
    begin 
      lv.Selected := lvItem; 
    end; 
  end; 
end; 

//Testaufruf:
procedure TForm1.Button1Click(Sender: TObject); 
var 
  lvItem: TListItem; 
begin 
  // in der Spalte subitem[0] den Text aus Edit1 suchen 
  LV_FindAndSelectItems(ListView1, Edit1.Text, 1); 
  ListView1.SetFocus; 
end;


Nur leider funktioniert er nicht. Habe ihn ausprobiert und er macht Garnichts. kein Fehler aber auch kein Ergebnis. Normalerweise müsste ja der gefundene Eintrag Markiert werden.

Weis jemand wieso?

Gruss
Cromos

Moderiert von user profile iconUGrohne: Code- durch Delphi-Tags ersetzt
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Sa 23.08.03 12:59 
Ich hab den Code mal grob überfolgen... sieht ganz ok aus. Hast du den Code mit dem Debugger durchlaufen?

Gruß
Tino
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: Sa 23.08.03 13:42 
Titel: nein
Zitat:

Hast du den Code mit dem Debugger durchlaufen?


Wie geht sowas. Bin aboluter Delphi neuling.
Setsuna Meio
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 70

Windows
Delphi
BeitragVerfasst: Sa 23.08.03 13:52 
Hallöchen,

also, vor jeder Zeile, kannst Du in dieser grauen Leiste hinklicken, dann wird so ein roter Punkt gemalt, das ist ein Breakpoint. Jetzt mache das mal und drücke dann F9 und dann wird Dein Programm dort stehen bleiben, danach gehst Du mit F7 und F8 durch den Code und hälst die Maus über Deine Variablen usw. drüber, dann wird so ein Hint angezeigt, einfach mal so durchschauen ob alles so geht wie es soll.

^^

_________________
Pluto, schicke das Feuer der Tiefe...
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: Sa 23.08.03 14:02 
Also kann nix ungewöhnliches entdecken.

hab den Code von swissdelphicenter.ch

Vieleicht hat ja jemand eine andere Lösung Parat.
Setsuna Meio
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 70

Windows
Delphi
BeitragVerfasst: Sa 23.08.03 14:08 
Huhu,

wenn du mir ein paar minuten gibst, dann bastel ich was, extra für dich. :D

^^

_________________
Pluto, schicke das Feuer der Tiefe...
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: Sa 23.08.03 14:11 
Zitat:

wenn du mir ein paar minuten gibst, dann bastel ich was, extra für dich


Echt? Das wäre ja Supi-Dupi. Danke
Setsuna Meio
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 70

Windows
Delphi
BeitragVerfasst: Sa 23.08.03 16:24 
Huhu,

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: Integer;
begin
  j := 0;
  for i := 0 to ListView1.Items.Count -1 do
  begin
    if ListView1.Items.Item[i].Caption = Edit1.Text then
    begin
      ShowMessage('Gefunden in der Spalte ' + IntToStr(j) + ', in Zeile: ' + IntToStr(i));
    end;
  end;
end;

Bei j musst man jetzt halt den Index der Spalte angeben, der mit 0 beginnt, dann funktioniert das schon. Ich glaube das ganze kannst Du nun auch slebst zu einer Prozedur oder Funktion mit Parametern umbauen. :D

^^

_________________
Pluto, schicke das Feuer der Tiefe...
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: Sa 23.08.03 18:19 
Titel: Danke
Hi Setsuna,

Tausend danke für den Code. habe jetzt nur noch eine Frage,
wie mache ich das das die Gefundene Zeile mit dem Scrollbalken Markiert wird?

Irgendwie hab ich das System von ListView noch nicht richtig verstanden.
Ich weiss es nicht.
Setsuna Meio
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 70

Windows
Delphi
BeitragVerfasst: Sa 23.08.03 19:25 
Huhu,

ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Button2Click(Sender: TObject);
begin
  ListView1.Items.Item[IndexDesGewünschtenItems].MakeVisible(True);
end;

Damit macht man es schonmal sichtbar, egal an welcher Position es gerade ist, d.h. es wird zu dem Items gescrollt, aber wie man jetzt auch noch ein Fokus setzt, weiss ich auf anhieb nicht. Vielleicht hilft mir Pluto weiter, ich schau noch ein bisschen.
Aber vielleicht hat auch noch jemand anders eine Idee.

^^

_________________
Pluto, schicke das Feuer der Tiefe...
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: So 24.08.03 10:44 
Guten morgen :)

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
j := 0;
  for i := 0 to ListView1.Items.Count -1 do
  begin
    if ListView1.Items.Item[i].Caption = txtSuch.Text then
    begin
      ListView1.Items.Item[i].MakeVisible(True);
     //ShowMessage('Gefunden in der Spalte ' + IntToStr(j) + ', in Zeile: ' + IntToStr(i));
    end;
  end;


Da sind wir genau da, wo nix mehr geht. Genau wie im ersten Code den ich gepostet habe.
Oder muss ich irgend eine Eigenschaft des ListView anders einstellen?
Das Suchen funkt wunderbar, aber er zeigt es nicht an. Jedenfalls nicht wenn man das gefundene Item markieren will.
Setsuna Meio
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 70

Windows
Delphi
BeitragVerfasst: So 24.08.03 10:51 
Huhu,

ich habe den Style auf vsReport. Du gibst aber schon columns an die Du vorher schon erzeugt hast?

^^

_________________
Pluto, schicke das Feuer der Tiefe...
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: So 24.08.03 11:25 
Hi,

Ja, die Coluns habe ich vorher erzeugt.
vsReport ist auch eingestellt.