![]() |
Access variable from one form to another
Hi,
How can pass the same variable in another form, for example, i have two forms, Form1 and Form2 TForm1.Calculation(OldID: Integer) OldID := 1+1; now i want to call the value of OldID from Form2. for example TForm2.btnClick(Sender:Tobject) Calculation();---> here how can i get the value of OldID from Form1? if i declare again OldID in Form2 then it shows the value 0 |
AW: Access variable from one form to another
there are several ways to get what you want. I prefer this;
Delphi-Quellcode:
Unit3
interface ... var OldId : integer;
Delphi-Quellcode:
Unit2
interface uses unit3; .. OldId := OldId+1;
Delphi-Quellcode:
greetings
Unit1
interface uses unit3; .. OldId := OldId+1; K-H |
AW: Access variable from one form to another
Alternatively you can e.g. declare a private field and a corresponding property of TForm1.
|
AW: Access variable from one form to another
Person1 First Form
-------------------- Private FOldID:Integer procedure SetOldID(Value: Integer); public property OldID: integer read FOldID write SetOldID; .. .. .. procedure TPerson1.SetOldID(Value: Integer); begin FOldID := Value; end; procedure TPerson1.ADD(OldID: Integer); begin OldID = OldID+s: end; ---------------------- Second Form called Person2 Now i want to use the value (OldID) from TPerson1.ADD(OldID: Integer) in Person2 Form,in following situation .. .. .. z: Tperson1 if bool = True then z.Add(z.OldID) i have tried with property but it does not work ,could you please give me suggestion ? |
AW: Access variable from one form to another
If you want to access a property of Form1 (Unit1) from within Form2 (Unit2), you have to add Unit1 to the uses clause in Unit2.
Delphi-Quellcode:
unit Unit1;
interface uses Windows, Classes, SysUtils, Forms, Controls, Graphics, StdCtrls; type TForm1 = class(TForm) {...} end; var Form1: TForm1; implementation uses Unit2; // <-- procedure TForm1.FormClick(Sender: TObject); begin ShowMessage(IntToStr(Form2.OldId)); end;
Delphi-Quellcode:
I generally wouldn't recommend doing this though because it leads to spaghetti code.
unit Unit2;
interface uses Windows, Classes, SysUtils, Forms, Controls, Graphics, StdCtrls; type TForm2 = class(TForm) {...} private FOldId: integer; public property OldId: integer read FOldId write FOldId end; var Form2: TForm2; implementation {...} Instead I would outsource the data and business logic to a separate class and then create all forms that operate on a specific object on demand:
Delphi-Quellcode:
unit MyData;
type TMyData = class private FOldId: integer; public OldId: integer read FOldId write FOldId; end; {...}
Delphi-Quellcode:
unit Main;
uses Windows, Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, MyData; type TFrmMain = class(TForm) { ... } FMyData: TMyData; end; var frmMain: TFrmMain; implementation procedure TForm1.FormClick(Sender: TObject); var DialogForm: TFrmDialog; begin DialogForm := TFrmDialog.Create(FMyData); DialogForm.Show; // Or perhaps DialogForm.ShowModal end;
Delphi-Quellcode:
unit FrmDialog;
uses Windows, Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, MyData; type TFrmDialog = class(TForm) { ... } FMyData: TMyData; constructor Create(MyData: TMyData); end; // You should dispose of this global variable. You'll also have to edit the // project's source file for this //var // FrmDialog: TFrmDialog ; implementation constructor TFrmDialog.Create(MyData: TMyData); begin inherited Create; FMyData := MyData; end; procedure TFrmDialog.FormClick(Sender: TObject); begin FMyData.OldId := 1; // change the data object end; |
AW: Access variable from one form to another
Zitat:
'Spaghetti Code' comes from good old BASIC code, having loads of GOTO's and GOSUB's. If you would print the code and connect all program lines in the order they would be executed, you would end up in something looking like a bowl of spaghetti (without the source sauce though). What you want to express is simply very ugly code which is not reusable. This is cannot be compared to spaghetti code, which has some sort of built in protection against successful modification. Please show some respect for Spaghetti ;-) Regarding the separation from front-end and business logic you are completely right. :thumb: |
AW: Access variable from one form to another
Thanks for response
Actually, i would like to do the following: Procedure TForm1.Person1(ID:Integer);<--- i want to use this value of ID in another Form begin // end; Procedure TForm2.Button1Click(Sender: TObject); begin //here i want to use the value of ID from TForm1 *how can i get the value of ID from Tform1 to here end; Thanks for your response |
AW: Access variable from one form to another
Assuming the ID really belongs to Form1:
Delphi-Quellcode:
Adding Unit1 to the uses-clause of Unit2, you can get the current value simply by calling Form1.ID.
type
TForm1 := class(TForm) ... private FID: integer; public procedure Person1(NewID: integer); property ID: integer read FID; ... end; ... (* Looks like a setter, but I am not sure if other interesting things happen here, too *) procedure TForm1.Person1(NewID:Integer); begin FID := NewID; end; |
AW: Access variable from one form to another
The solution has already been posted, again in detail:
Delphi-Quellcode:
unit unit1;
interface type TForm1 = class(TForm) {...} private // <- for use self FMyPersonID: Integer; procedure Person1(ID:Integer); public // <- for use to other property MyPersonID: Integer read FMyPersonID write FMyPersonID; end; var Form1: TForm1; // <- Instance of TForm1 implementation Procedure TForm1.Person1(ID:Integer);<--- i want to use this value of ID in another Form begin FMyPersonID := ID; end; unit unit2; interface type TForm2 = class(TForm) {...} procedure Button1Click(Sender: TObject); end; implementation uses unit1; // <- knowhow about TForm1, MyPersonID and Form1 Procedure TForm2.Button1Click(Sender: TObject); begin //here i want to use the value of ID from TForm1 Form1.MyPersonID := Form1.MyPersonID + 1; end; |
AW: Access variable from one form to another
I have followed your method, i have clarify my situtaion again, please need some more hints
Procedure TForm1.Person1(ID:Integer); begin //The value of this ID come from database with sql query for example, after query here is the value of ID = 120; end; Procedure TForm2.Button1Click(Sender: TObject); begin Form1.Person1(Form1.MyPersonID) //when i call here Form1.MyPersonID, then it passes the value '0' to TForm1.Person1(ID:Integer) // but , i want to call the value 120 end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:31 Uhr. |
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