Autor Beitrag
somme_ma
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mo 09.01.06 20:25 
Also meine Ausgangslage ist, dass ich einen StringGrid mit verschiedenen Datensätzen habe.

Nun möchte ich den Benutzer in diesem StringGrid am besten per ListBox nach einem bestimmten Stichwort innerhalb dieser Datensätze suchen lassen.
Dabei muss nur der erste Treffer markiert werden.


Zuletzt bearbeitet von somme_ma am Mo 09.01.06 22:15, insgesamt 1-mal bearbeitet
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 09.01.06 21:47 
Hallo,

dann versuch es mal damit,
ich denke das Du die Anbindung an die ListBox hinbekommst :wink:
Der Code durchsucht nicht die Fixed-Zeilen/Spalten!
Soll die Groß-/Kleinschreibung beachtet werden, dann nimm LowerCase aus der Funktion.
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:
function SuchInGrid(SG: TStringGrid;S: String;SelZeile: Boolean): String;
var c,r    : Integer;
    blAbbr : Boolean;
begin
  blAbbr := False;
  S := LowerCase(S);
  for c := SG.FixedCols to SG.ColCount-SG.FixedCols-1 do
    begin
    if blAbbr then
      break;
    for r := SG.FixedRows to SG.RowCount-SG.FixedRows-1 do
      if LowerCase(SG.Cells[c,r]) = S then
        begin
        if SelZeile then
          SG.Selection := TGridRect(Rect(SG.ColCount-1,r,SG.FixedCols,r))
          else
            SG.Selection := TGridRect(Rect(c,r,c,r));
        blAbbr := True;
        break;
        end;
    end;
  if blAbbr = False then
    Result := '"'+S+'"nicht im StringGrid gefunden';
end;

procedure TForm1.Button1Click(Sender: TObject);
var sErg : String;
begin
  //True ganze Zeile wird markiert, False nur die Zelle
  sErg := SuchInGrid(StringGrid1,'Suchwort',True);
  if sErg <> '' then
    showmessage(sErg);
end;


Ps.: hast Du Dein letztes Problem gelöst?

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
somme_ma Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mo 09.01.06 22:00 
Er muss nicht in den Fixed Zeilen suchen, sondern nur in den anderen. (Fixed sind doch die grau hinterlegten oder?)

Hm, also wie muss ich das jetzt einbinden??

Hat wieder dieselbe Fehlermeldung gebracht.

Ich kopier das immer ganz normal in den Code und oben bei public kopier ich nochmal den selben Funktionskopf rein, oder?
ausblenden Delphi-Quelltext
1:
function SuchInGrid(StringGrid1: TStringGrid;S: String;SelZeile: Boolean): String;					


Ungenügende Forward- oder External-Deklaration: 'TForm1.SuchInGrid'
Danniolo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 693



BeitragVerfasst: Mo 09.01.06 22:13 
Im "ganz normalen Code" musst du vor die Function noch ein "TForm1." setzten :roll:

also:
ausblenden Delphi-Quelltext
1:
function TForm1.SuchInGrid(StringGrid1: TStringGrid;S: String;SelZeile: Boolean): String;					
somme_ma Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mo 09.01.06 22:14 
Danke. Sehr gut.
somme_ma Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mi 11.01.06 18:11 
Wie kann ich denn das jetzt machen, dass er bei erneutem Drücken auf den Suchen-Button zum nächsten Treffer springt und diesen markiert?
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Do 12.01.06 14:19 
Hallo,

dann wirst Du die Funktion abändern müssen.
Mit Hilfe der Variable blAbbr wird nach der ersten gefundenen Zelle die Suche abgebrochen und die entsprechende Zelle selectiert.
Diesen Abbruch der Suche so ändern, das die Suche zwar weiterläuft,
aber die Selection bei der ersten Fundstelle selectiert wird.
Die dann im weiteren gefundenen Zelladressen in einem Array oder ähnliches speichern.
Das Array kannst Du dann zum selectieren der Fundstellen nutzen.

Ein Beispiel zum Anpassen oder Erweitern( z.B [Vorigen]-Schalter):
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:
43:
44:
45:
//...
Const CellsInGrid = 25;//Grid = 5 * 5

var
  SuchArray : Array [0..CellsInGrid-1,0..1] of Integer;
  iFound : Integer = -1;
  iSelect : Integer;
//...
function SuchInGrid(SG: TStringGrid;S: String): Boolean;
var c,r    : Integer;
begin
  Result := False;
  S := LowerCase(S);
  for c := SG.FixedCols to SG.ColCount-SG.FixedCols-1 do
    for r := SG.FixedRows to SG.RowCount-SG.FixedRows-1 do
      if LowerCase(SG.Cells[c,r]) = S then
        begin
        if iFound = -1 then
          SG.Selection := TGridRect(Rect(c,r,c,r));
        Inc(iFound);
        SuchArray[iFound,0] := c;
        SuchArray[iFound,1] := r;
        end;
  if iFound > -1 then
    Result := True;
end;

procedure TForm1.SuchenClick(Sender: TObject);
var s : String;
begin
  S := 'abc';
  If SuchInGrid(StringGrid1,'abc') then
    iSelect := 0
    else
      showmessage('"'+s+'" nicht gefunden');
end;

procedure TForm1.NaechstenClick(Sender: TObject);
begin
  inc(iSelect);
  if iSelect <= iFound then
    StringGrid1.Selection := TGridRect(Rect(SuchArray[iSelect,0],SuchArray[iSelect,1],SuchArray[iSelect,0],SuchArray[iSelect,1]))
    else
      showmessage('Keine weiteren Fundstellen');
end;


Die Option bzgl. Zell- oder Zeilenselectierung fehlt in der Funktion.
Aber ich Nehme an das Du dass bei Bedarf Anhand der beiden Funktionen Dir das selbst Coden kannst. :wink:

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )