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
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.659 Beiträge
 
Delphi 12 Athens
 
#1

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

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
 
#3

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
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.659 Beiträge
 
Delphi 12 Athens
 
#4

AW: Access variable from one form to another

  Alt 21. Aug 2013, 09:22
I think we need the code of the "Person1"-method. Maybe you want to do a call by reference but actually do a call by value?
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.487 Beiträge
 
Delphi 12 Athens
 
#5

AW: Access variable from one form to another

  Alt 21. Aug 2013, 13:21
The PersonID can alternatively be determined from a query:
Delphi-Quellcode:
unit unit1;

interface

type
  TForm1 = class(TForm)
    {...}
  private // <- for use self
    function GetMyPersonID: Integer;
    procedure SetMyPersonID(AValue: Integer);
  public // <- for use to other
    property MyPersonID: Integer read GetMyPersonID write SetPersonID;
  end;

var
  Form1: TForm1; // <- Instance of TForm1

implementation

procedure TForm1.SetMyPersonID(AValue: Integer);
begin
  FQuery.FieldByName('MyPersonID').AsInteger := AValue;
end;

procedure TForm1.GetMyPersonID: Integer;
begin
  Result := FQuery.FieldByName('MyPersonID').AsInteger;
end;
  Mit Zitat antworten Zitat
question

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

AW: Access variable from one form to another

  Alt 26. Aug 2013, 17:33
Hi,
i have followed what you have suggested but i still got the value '0' .
Now, i wanna do the work in same Unit mot from another
Code:
Procedure TForm1.AddDateTime(Sender:Tobject; AEvent: TcxSchedulerEvent);
begin

Query.Close();
    QueryKalender.SQL.Text:= 'INSERT INTO Table'
      '(Name`, `NEWID` ..) ' +
    'VALUES ( ' :Name, :NEWID);';

Query.ParamByName('NEWID').AsInteger:= AEventID;
    Query.ExecSQL();
    Query.Close();

  end;

Procedure TForm1.OnDragDrop(Sender, Source: TObject; X,
  Y: Integer);

begin
//here i would like to use the value of AEventID;
//i cannnot call the procedure AddDateTime(Sender,TcxSchedulerEvent);
//becasue the parameter is different, how can i overcome it ?
end;

Geändert von question (26. Aug 2013 um 17:50 Uhr)
  Mit Zitat antworten Zitat
question

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

AW: Access variable from one form to another

  Alt 26. Aug 2013, 22:19
Hi, now its working fine i have written the follwoing
Code:
procedure TForm1.SetMyPersonID(AValue: Integer);
begin
  FQuery.FieldByName('MyPersonID').AsInteger := AValue;
end;

procedure TForm1.GetMyPersonID: Integer;
begin
  Result := FQuery.FieldByName('MyPersonID').AsInteger;
end;
is that ok to write like this, i mean normally in sql query need to query.sql.add/query.sql.open/close... but in this case it is working fine should i need to write the whole query or is that also okay just to write only
Code:
Result := FQuery.FieldByName('MyPersonID').AsInteger;
  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