How can I access the delphi control from outside the form module?

I'm trying to call Enabled property of the timer of the procedure defined in the following way: procedure Slide(Form: TForm; Show: Boolean);instead of a fixed form name (for example: Form2.Timer...)

After putting the form block in the use list, it works: Form2.Timer1.Enabled := True; but the following does not work: Form.Timer1.Enabled := True;(where Form is the form passed as the procedure parameter.

How to access the Timer component in my form?

Thanks in advance.

+3
source share
7 answers

, TForm, TForm Timer1. :

uses
  Unit2; //unit with your form

procedure Slide(Form: TForm2; Show: Boolean); //change TForm2 to the classname you use
begin
  Form.Timer1.Enabled := True;
end;

Edit:

, :

procedure Slide(Form: TForm; const aTimerName: string; Show: Boolean);
var
  lComponent: TComponent;
begin
  lComponent := Form.FindComponent(aTimerName);
  if Assigned(lComponent) and (lComponent is TTimer) then
    TTimer(lComponent).Enabled := True;
end;

:

procedure TForm2.Button1Click(Sender: TObject);
begin
  Slide(Self, 'Timer1', False);
end;

. , .

+4

, , "Timer1", FindComponent, :

procedure Slide(Form: TForm; Show: Boolean);
var
  TimerObj: TComponent;
  Timer: TTimer;
begin
  TimerObj := Form.FindComponent('Timer1');
  Assert(Assigned(TimerObj), 'Form has no Timer1');
  Assert(TimerObj is TTimer, 'Form.Timer1 is not a TTimer');
  Timer := TTimer(TimerObj);
  // Continue using Form and Timer
end;

. , ( ). , , . , TTimer Timer1, Slide. , , Slide , , .

, , SlideTimer. Timer1 , TTimer, , , . IDE.


- . Slide, TForm.

type
  TSlidableForm = class(TForm)
    Timer1: TTimer;
  end;

procedure Slide(Form: TSlidableForm; Show: Boolean);

, Form2 Slide, , , . , , Slide . , , . TSlidableForm ; Slide TForm2 . TForm2, TSlidableForm.


The_Fox - , . :

procedure Slide(Form: TForm; Timer: TTimer; Show: Boolean);

FindComponent ; . , . :

Slide(Form2, Form2.Timer1, True);
Slide(AnotherForm, AnotherForm.SlideTimer, False);

, , . Slide, - (, ). Slide, :

procedure Slide(Form: TForm; Show: Boolean);
var
  Timer: TTimer;
begin
  Timer := TTimer.Create(nil);
  try
    Timer.OnTimer := ...;
    Timer.Interval := 500;
    Timer.Enabled := True;

    // Put your normal Slide stuff here
  finally
    Timer.Free;
  end;
end;
+6

.

...
implementation

uses Unit2;

{$R *.dfm}
...
+4

Form2 .

:

TForm, TTimer, TForm no fields or properties as TTimer.

, TForm1 as the parameter.

say Form1, , procedure Slide(Form: TForm1; Show: Boolean);

, . . .

Form1

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Unit3;

procedure TForm1.Button1Click(Sender: TObject);
begin
Slide(Form1, True)
end;

2

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses Unit3;

procedure TForm2.Button1Click(Sender: TObject);
begin
Slide(Form2, True)
end;

,

unit Unit3;

interface

uses Forms, ExtCtrls, Unit1, Unit2;

procedure Slide(Form: TForm1; Show: Boolean) overload;
procedure Slide(Form: TForm2; Show: Boolean) overload;

implementation


procedure Slide(Form: TForm2; Show: Boolean);
begin
  Form.Timer1.Enabled := True;
end;


procedure Slide(Form: TForm1; Show: Boolean);
begin
  Form.Timer1.Enabled := True;
end;

end.
+2

.

Delphi . .pas.

:

unit Name;

interface
uses
  // The definition of these units can be used both in the 
  // interface as in the implementation section.
  unit1, unit2;  

// Public interface, visible to other units that use this unit.

implementation
uses
  // The definition of these units can be used only in the 
  // implementation section.
  unit3, unit4;

// Private implementation, not visible outside.

initialization
  // code to initialize the unit, run only once
finalization
  // code to cleanup the unit, run only once
end.

, , , .

, :

unit1;
interface
type
  TTest = Integer;
// ...

unit2;
interface
type
  TTest = Boolean;
// ...

unit3;
interface
uses
  unit1, unit2;
var
  a: TTest;  // Which TTest?
// ...

, :

unit3;
interface
uses
  unit1, unit2;
var
  a: unit1.TTest;  // TTest from unit1
  b: unit2.TTest;  // TTest from unit2
// ...

, 2.

, datamodule ( , , , ). datamodule.

Edit

:

procedure Slide(Form: TForm; Show: Boolean); 

TForm Timer1.

:

procedure Slide(Form: TForm2; Show: Boolean); 

TForm2 - , .

+2

- :

procedure Slide(Form: TForm; Show: Boolean);
var
  Timer: TTimer;
begin
  Timer := Form.FindComponent('Timer1');
  if Assigned(Timer) then
    Timer.Enabled := True;
end;

;-), " " :

procedure Slide(Form: TForm; Show: Boolean);
var
  i:  Integer;
begin
  // Could use a TComponent and for..in instead. This works in
  // all Delphi versions, though.
  for i := 0 to Form.ComponentCount - 1 do
    if Form.Components[i] is TTimer then
      TTimer(Form.Components[i]).Enabled := True;
end;♦♠
+1

( ) .

ISLideable, SlideTimer ( getter setter), Slide,

Slide(const Target: ISlideable; Show: Boolean);

And every form that needs to be transmitted says that it is biased

MyFormN = class(TForm, ISlideable)
...
0
source

Source: https://habr.com/ru/post/1780000/


All Articles