Forum > General

Are control events implemented in other units?

(1/2) > >>

yazigegeda:

Unit1:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;     procedure FormCreate(Sender: TObject);  private   public     procedure Button1Click(Sender: TObject);   end; var  Form1: TForm1; implementation uses  Unit2;   {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin end;   end. 

Unit2:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit2; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type   { TForm2 }   TForm2 = class(TForm)    procedure FormCreate(Sender: TObject);  private   public   end; var  Form2: TForm2; implementation uses  Unit1; {$R *.lfm} { TForm2 } procedure TForm2.FormCreate(Sender: TObject);begin end;  procedure TForm1.Button1Click(Sender: TObject);begin    // TForm1 button click event again implementedend; end. 


I want to put the implementation of the button click event of Form1 in other units, I don't know if it is feasible, help me :'( :'( :'(

Handoko:
No.

The correct solution is, write a new unit, put the code that needed to be shared in that unit. And then in both the first and second units' uses clauses put the name of the new unit there.

VisualLab:
Example of solution in the attachment.

Code of Unit1:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  {TForm1}  TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Label1: TLabel;    procedure Button2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation uses  Unit2; {$R *.lfm} {TForm1} procedure TForm1.FormCreate(Sender: TObject);begin  Label1.Caption := 'Form 1 created!';  Button1.OnClick := @Form2.Button1Click;end; procedure TForm1.Button2Click(Sender: TObject);begin  Application.CreateForm(TForm2, Form2);  Form2.Show;end; end. 
Code of Unit2:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit2; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  {TForm2}  TForm2 = class(TForm)    Label1: TLabel;    procedure FormCreate(Sender: TObject);  public    procedure Button1Click(Sender: TObject);  end; var  Form2: TForm2; implementation {$R *.lfm} {TForm2} procedure TForm2.FormCreate(Sender: TObject);begin  Label1.Caption := 'Form 2 created!';end; procedure TForm2.Button1Click(Sender: TObject);var  LName: String;begin  {click event handled by button on the TForm1}  LName := '';  if Sender is TButton   then LName := (Sender as TButton).Name;  ShowMessage('Clicked: ' + LName);end; end. 
However, I do not recommend this approach. With more event handlers it will get messy. A better approach is (your choice):

- calling functions defined in a separate library module (own library) from event handling procedures, or
- "connecting" library procedures to events.


EDIT:

Handoko posted his answer right before me :) Yes, his answer is the best way to approach this issue.

What I provided is only to show that it can be done. But that doesn't mean you should do it!

Handoko:
You've just taught the TS how to write spaghetti code.

https://en.wikipedia.org/wiki/Spaghetti_code

VisualLab:

--- Quote from: Handoko on May 14, 2024, 01:51:10 pm ---You've just taught the TS how to write spaghetti code.

https://en.wikipedia.org/wiki/Spaghetti_code

--- End quote ---

I would say that it is rather a "tangled ball of ropes" (so, probably an even worse solution than "spagetti"). But yes, I completely agree that this solution should not be used. The example was intended to show the questioner that:

- you can do something similar to what he wants,
- you shouldn't create code this way.

Perhaps it would be worse if the questioner came to a working solution on his own. But then no one would explain to him why this was a bad idea.

Navigation

[0] Message Index

[#] Next page

Go to full version