Autor Beitrag
Jojojoxx
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 386



BeitragVerfasst: Do 20.09.07 20:09 
Hallo!

Ich möchte ein Stringgrid dynamisch erzeugen, allerdings erhalte ich immer den Fehler "Undefinierter bezeichner 'self'".
Hier ist mein Code:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure CreateGrid;

var i:Integer;
   myGrid:TStringgrid;
begin
 For i:=0 to Form1.ComboBox1.ItemIndex do
  begin
   mygrid:=TStringgrid.create(self); //Hier kommt der Fehler
   myGrid.RowCount:=9;
   myGrid.ColCount:=8;
   myGrid.Cells[0,0]:=Form1.Combobox1.items.Strings[i];
  end;
end;

Woran kann das liegen??
Danke für die Hilfe!
mfg

Jojo

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 20.09.07 20:17 
Du musst entweder die Prozedur als Methode deiner Form deklarieren, also so
procedure TForm1.CreateGrid; - natürlich dann auch oben die Prozedur eintragen; dann kannst du auf self zugreifen, oder das self durch eine Form ersetzen, also z.B.
ausblenden Delphi-Quelltext
1:
mygrid:=TStringgrid.create(Form1); // oder wie die Form halt heißt ;-)					


Und, auch noch wichtig: du musst auch das Parent setzen:
ausblenden Delphi-Quelltext
1:
mygrid.Parent := Form1; // Oder halt das Panel/Groupbox/... wo das Grid drauf soll					

_________________
We are, we were and will not be.
Jojojoxx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 386



BeitragVerfasst: Do 20.09.07 20:31 
Achso!!
ja jetzt klappt´s, gibt auch irgendwie Sinn! :D
Danke für die Hilfe!
mfg

Jojo
Jojojoxx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 386



BeitragVerfasst: Do 20.09.07 20:43 
Hi!

Jetzt hab ich doch noch eine Frage dazu, weshalb löst dieser Code (erst beim Schließen des Programms) einen Fehler aus?
Fehler: "Ungültige Zeigeroperation"

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:
unit Unit1;


interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  DynArray:Array of TStringgrid;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var myGrid:TStringgrid;
   i:Integer;
begin
setlength(DynArray,3);
for i:=0 to 3 do
 begin
  myGrid:=TStringgrid.create(Form1);
  myGrid.Cells[0,0]:='Test';
  DynArray[i]:=myGrid;
 end;

end;

end.

mfg

Jojo

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 20.09.07 20:56 
Wenn du die Länge des Array of 3 setzt, dann gibts nur die Elemente 0,1 und 2. ;-)

_________________
We are, we were and will not be.
Jojojoxx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 386



BeitragVerfasst: Do 20.09.07 20:59 
:D :D :D Immer diese kleinen Fehler!!
Danke! :wink:
mfg

Jojo