Autor Beitrag
schitho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 288

XP Home SP2
D2005 Prof
BeitragVerfasst: Mi 10.09.03 21:59 
Hab eine typische Anfängerfrage:

Ich möchte aus der Unit1 die Form2 der Unit2 aufrufen und in der Unit2 eine Funktion aus Unit1 verwenden.

Geht das?

Wenn ja: Wie?

Verwende folgenden Code, der nicht funktioniert (Funktion Test kennt er nicht)

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

interface

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

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

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

function test : string;
begin
  result:='Hallo';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Form2.show;
end;

end.


Und Unit2:

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

interface

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

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

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
 label1.caption:=form1.test;
end;

end.


Wie muss ich die Funktion Test in Unit2 einbinden?

_________________
(Sorry! Leider ewiger Delphi-Neuling)
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: Mi 10.09.03 22:53 
hallo tom!

deine function 'test' ist nur in der unit1 sichtbar und ansprechbar.
schreib die deklaration in die public-section von Form1, dann kannst sie auch aus unit2 aufrufen

mfg, stefan
PS: natürlich musst sie dann als functionTForm1.test:string; anlegen. eh klar! :wink:
obbschtkuche
Gast
Erhaltene Danke: 1



BeitragVerfasst: Do 11.09.03 18:58 
unit1.test
sollte helfen.
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: Fr 12.09.03 15:51 
wenn du test nicht als methode von Form1 haben willst, dann deklariere die funktion im interface-abschnitt der unit1

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
interface
.
.
function test : string
.
.
implementation


somit kannst du sie auch problemlos von unit2 aus aufrufen.

mfg, stefan