Einzelnen Beitrag anzeigen

strom

Registriert seit: 23. Aug 2008
Ort: Keine Ergebnisse gefunden
290 Beiträge
 
#14

AW: StringGrid aus Form2 in Form1 abspeichern

  Alt 7. Sep 2016, 06:42
Hallo,

jetzt nochmal zurück zu meiner Frage.
Habe eine Unit Ereignisprotokoll mit dem Type "StringGrid1: TStringGrid"

Habe aber auch ein Unit Hauptformular, wenn ich jetzt zum Beispiel über das "MainMenu1 Hauptformular" im StringGrid was suchen möchte!
Wie binde ich TStrinGrid im Hauptformular mit ein???


Richtig wieder ein Quelltext aus Delphi Treff
Code:
function SeekRecord(Grid: TStringGrid; ColOrder: array of integer;
  StrList: array of String): Boolean;
var
  I, J : Integer;
begin
  result := false;
  for I := 1 to Grid.RowCount - 1 Do
  begin
    for J := low(ColOrder) to high(ColOrder) do
    begin
      if (Grid.Cells[ColOrder[J],I]=StrList[J]) then
         result := true
      else
         result := false;
      if result = false then break;
    end;
    if result = true then break;
  end;

  if result = true then
     Grid.Row := I
  else
     ShowMessage('Datensatz nicht gefunden!' +#13#13+ 'Record not Found!');
end;
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  //Suche Zeile nach Column 0 mit Suchbegriff
  SeekRecord(Grid,[0],['Suchbegriff']);
  //Suche Zeile nach Column 0 und 1 mit Suchbegriffen 1 und 2
  SeekRecord(Grid,[0,1],['Suchbegriff1','Suchbegriff2']);
  //Suche Zeile nach Column 1, 5 und 2 mit Suchbegriffen 1, 2 und 3
  SeekRecord(Grid,[1,5,2],['Suchbegriff1','Suchbegriff2','Suchbegriff3']);
  //usw.
end;

Delphi-Quellcode:
unit Ereignisprotokoll;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
  Vcl.Grids;

type
  TForm2 = class(TForm)
    StringGrid1: TStringGrid;
    Panel1: TPanel;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;
implementation

{$R *.dfm}

uses Hauptformular;

procedure SaveStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
var
 f: TextFile;
 i, k: Integer;
begin
  AssignFile(f, FileName);
  Rewrite(f);
 with StringGrid do
  begin
   Writeln(f, ColCount);
   Writeln(f, RowCount);
    for i := 0 to ColCount - 1 do
      for k := 0 to RowCount - 1 do
    Writeln(F, Trim(Cells[i, k]));
  end;
  CloseFile(F);
end;

procedure LoadStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
var
 f: TextFile;
 iTmp,i,k: Integer;
 strTemp: String;
begin
  AssignFile(f, FileName);
  Reset(f);
 with StringGrid do
  begin
    Readln(f, iTmp);
    ColCount := iTmp;
    Readln(f, iTmp);
    RowCount := iTmp;
     for i := 0 to ColCount - 1 do
      for k := 0 to RowCount - 1 do
      begin
        Readln(f, strTemp);
        Cells[i, k] := strTemp;
      end;
  end;
  CloseFile(f);
end;

procedure TForm2.BitBtn1Click(Sender: TObject);
begin
 Form2.Visible := false;
 Form1.Bearbeiten1.Enabled := false;
end;

procedure TForm2.FormCreate(Sender: TObject);
var
 i, j :integer;
begin
 Stringgrid1.ColWidths[0] := 5;
 Stringgrid1.ColWidths[1] := 25;
 Stringgrid1.ColWidths[2] := 70;
 Stringgrid1.ColWidths[3] := 70;
 Stringgrid1.ColWidths[4] := 100;
 StringGrid1.ColWidths[5] := 1000;
 StringGrid1.Cells [1,0] := ' ';
 StringGrid1.Cells [2,0] := 'Date';
 StringGrid1.Cells [3,0] := 'Time';
 StringGrid1.Cells [4,0] := 'ID';
 StringGrid1.Cells [5,0] := 'Event';
 for i := StringGrid1.FixedCols to StringGrid1.ColCount - 1 do
  begin
   for j :=StringGrid1.FixedRows to StringGrid1.RowCount - 1 do
    begin
     StringGrid1.Cells[i,j] := '';
    end;
   end;
    LoadStringGrid(StringGrid1, ExtractFilePath(ParamStr(0)) + 'Ereignisprotokoll.dat');
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
    DeleteFile(ExtractFilePath(ParamStr(0)) + 'Ereignisprotokoll.dat');
     SaveStringGrid(StringGrid1, ExtractFilePath(ParamStr(0)) + 'Ereignisprotokoll.dat');
end;

end
  Mit Zitat antworten Zitat