Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi ein objekt von mehreren forms benutzen.. (https://www.delphipraxis.net/173962-ein-objekt-von-mehreren-forms-benutzen.html)

DeddyH 25. Aug 2013 09:30

AW: ein objekt von mehreren forms benutzen..
 
Liste der Anhänge anzeigen (Anzahl: 1)
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.


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:58 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz