Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.624 Beiträge
Delphi 12 Athens
|
AW: ein objekt von mehreren forms benutzen..
25. Aug 2013, 10:30
This is the same problem, but a completely different question and situation than yesterday. It is quite hard to post a solution if you don' t know what the problem is. I wrote a short example in Delphi 7. It is not good at all because both units reference each other (you should avoid this if possible), but it works in this simple case.
Unit1:
Delphi-Quellcode:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TCalculation = class(TForm)
btnSetValue: TButton;
btnShowAccounts: TButton;
procedure btnSetValueClick(Sender: TObject);
procedure btnShowAccountsClick(Sender: TObject);
private
{ Private-Deklarationen }
FValue: integer;
function GetValue: integer;
procedure SetValue( const NewValue: integer);
public
{ Public-Deklarationen }
property Value: integer read GetValue write SetValue;
end;
var
Calculation: TCalculation;
implementation
{$R *.dfm}
uses Unit2; //Added Unit2
{ TCalculation }
function TCalculation.GetValue: integer;
begin
Result := FValue;
end;
procedure TCalculation.SetValue( const NewValue: integer);
begin
FValue := NewValue;
end;
procedure TCalculation.btnSetValueClick(Sender: TObject);
begin
Value := Value + 1;
end;
procedure TCalculation.btnShowAccountsClick(Sender: TObject);
var
Accounts: TAccounts; //locale variable, I deleted the global one in Unit2
begin
Accounts := TAccounts.Create( nil);
try
Accounts.ShowModal;
finally
Accounts.Free;
end;
end;
end.
Unit2:
Delphi-Quellcode:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit1; //Added Unit1
type
TAccounts = class(TForm)
btnGetValue: TButton;
edtValue: TEdit;
procedure btnGetValueClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
implementation
{$R *.dfm}
procedure TAccounts.btnGetValueClick(Sender: TObject);
begin
edtValue.Text := IntToStr(Calculation.Value);
end;
end.
The prefix "btn" stands for TButton, "edt" for TEdit.
Detlef "Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
Geändert von DeddyH (25. Aug 2013 um 10:34 Uhr)
|
|
Zitat
|