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
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.659 Beiträge
 
Delphi 12 Athens
 
#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
Antwort Antwort


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 04:21 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