AGB  ·  Datenschutz  ·  Impressum  







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

Access variable from one form to another

Ein Thema von question · begonnen am 20. Aug 2013 · letzter Beitrag vom 26. Aug 2013
 
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
 

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

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 09:10 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