AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein Access variable from one form to another
Thema durchsuchen
Ansicht
Themen-Optionen

Access variable from one form to another

Ein Thema von question · begonnen am 20. Aug 2013 · letzter Beitrag vom 26. Aug 2013
Antwort Antwort
Seite 1 von 2  1 2      
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#1

Access variable from one form to another

  Alt 20. Aug 2013, 15:25
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
  Mit Zitat antworten Zitat
Benutzerbild von p80286
p80286

Registriert seit: 28. Apr 2008
Ort: Stolberg (Rhl)
6.659 Beiträge
 
FreePascal / Lazarus
 
#2

AW: Access variable from one form to another

  Alt 20. Aug 2013, 15:36
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:
Unit1
interface

uses unit3;

..
  OldId := OldId+1;
greetings
K-H
Programme gehorchen nicht Deinen Absichten sondern Deinen Anweisungen
R.E.D retired error detector
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.540 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: Access variable from one form to another

  Alt 20. Aug 2013, 15:44
Alternatively you can e.g. declare a private field and a corresponding property of TForm1.
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
  Mit Zitat antworten Zitat
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#4

AW: Access variable from one form to another

  Alt 20. Aug 2013, 23:12
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 ?
  Mit Zitat antworten Zitat
Namenloser

Registriert seit: 7. Jun 2006
Ort: Karlsruhe
3.724 Beiträge
 
FreePascal / Lazarus
 
#5

AW: Access variable from one form to another

  Alt 20. Aug 2013, 23:57
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:
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

{...}
I generally wouldn't recommend doing this though because it leads to spaghetti code.

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;
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#6

AW: Access variable from one form to another

  Alt 21. Aug 2013, 07:02
I generally wouldn't recommend doing this though because it leads to spaghetti code.
You are being unfair to both Spaghetti and Spaghetti Code.

'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.
  Mit Zitat antworten Zitat
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#7

AW: Access variable from one form to another

  Alt 21. Aug 2013, 08:04
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
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.540 Beiträge
 
Delphi 11 Alexandria
 
#8

AW: Access variable from one form to another

  Alt 21. Aug 2013, 08:13
Assuming the ID really belongs to Form1:
Delphi-Quellcode:
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;
Adding Unit1 to the uses-clause of Unit2, you can get the current value simply by calling Form1.ID.
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
  Mit Zitat antworten Zitat
Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.429 Beiträge
 
Delphi 10.4 Sydney
 
#9

AW: Access variable from one form to another

  Alt 21. Aug 2013, 08:33
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;
  Mit Zitat antworten Zitat
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#10

AW: Access variable from one form to another

  Alt 21. Aug 2013, 09:18
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;
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:36 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz