Recent

Author Topic: How should an Event Handler look like ?  (Read 2084 times)

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 298
How should an Event Handler look like ?
« on: September 20, 2021, 01:31:36 pm »
Hello Guys :),

Here my Question: I have an Event Handler looking like this:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TCLASS.Combo1Change(Sender: TObject);
  3. begin
  4.    //Calling Something
  5.    Label1.Caption:= Multiply(3, 4);
  6.  
  7.    //Doing Something Else
  8.    SomeLabel.Visible:= False;
  9.    SomeButton.Enabled:= False;
  10. end;
  11.  
  12.  

Whats the "best" way to Solve following:

When Form initializes i want to Reuse this Handler to make certain Things invisible / Enable certain Things, but WITHOUT Doing "Label1.Caption:= Multiply(3, 4);". How should the Handler be Structured to achiev this ?

I thought of doing following:
   create Variable and just skip those Functions wich i dont wanna Execute (not so nice solution)
   create another Function where i make certain Things invisible and call this in event Handler
   

Please give me some advice. would be very grateful for it :)
« Last Edit: September 20, 2021, 01:39:08 pm by Weitentaaal »
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: How should an Event Handler look like ?
« Reply #1 on: September 20, 2021, 01:38:35 pm »
   create another Function where i make certain Things invisible and call this in event Handler (lot of work)
+1
It's not a lot of work and you can customize what to do via parameters in the procedure call.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

PascalDragon

  • Hero Member
  • *****
  • Posts: 3406
  • Compiler Developer
Re: How should an Event Handler look like ?
« Reply #2 on: September 20, 2021, 01:39:09 pm »
   create another Function where i make certain Things invisible and call this in event Handler (lot of work)

That is indeed the way to do it most of the time.

MarkMLl

  • Hero Member
  • *****
  • Posts: 3253
Re: How should an Event Handler look like ?
« Reply #3 on: September 20, 2021, 01:39:50 pm »
Use multiple functions and document properly why you've done so.

Those might obviously call common functions where appropriate, possibly with an enumerated (etc.) parameter. So for example, you might have a common function with a boolean parameter which enables/disabled some (but not all) of the controls on a form or in a groupbox as conditions change.

Finally, don't try to be clever. Or if you really do have to be clever, document your intention and implementation exhaustively. Remember that debugging is more difficult than coding, so if coding an operation stretches your ability getting it to work reliably is likely to be beyond you.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 298
Re: How should an Event Handler look like ?
« Reply #4 on: September 20, 2021, 01:41:33 pm »
So it should look somehow like this? :

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TCLASS.Combo1Visibility();
  3. begin
  4.    SomeLabel.Visible:= False;
  5.    SomeButton.Enabled:= False;
  6. end;
  7.  
  8. procedure TCLASS.Combo1Calculations();
  9. begin
  10.    //Calling Something
  11.    Label1.Caption:= Multiply(3, 4);
  12. end;
  13.  
  14. procedure TCLASS.Combo1Change(Sender: TObject);
  15. begin
  16.    //Calling Something
  17.    Combo1Calculations
  18.  
  19.    //Doing Something Else
  20.    Combo1Visibility;
  21. end;
  22.  
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 298
Re: How should an Event Handler look like ?
« Reply #5 on: September 20, 2021, 01:44:39 pm »
Use multiple functions and document properly why you've done so.

Those might obviously call common functions where appropriate, possibly with an enumerated (etc.) parameter. So for example, you might have a common function with a boolean parameter which enables/disabled some (but not all) of the controls on a form or in a groupbox as conditions change.

Finally, don't try to be clever. Or if you really do have to be clever, document your intention and implementation exhaustively. Remember that debugging is more difficult than coding, so if coding an operation stretches your ability getting it to work reliably is likely to be beyond you.

MarkMLl

Thanks, i will keep that in mind :) !!
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 298
Re: How should an Event Handler look like ?
« Reply #6 on: September 20, 2021, 02:10:22 pm »
Would anyone else have some tips on how the methods should be named ? :)
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 3253
Re: How should an Event Handler look like ?
« Reply #7 on: September 22, 2021, 09:19:54 pm »
Would anyone else have some tips on how the methods should be named ? :)

Since nobody else has said anything: my practice is to change the object name from e.g. Button1 to ButtonStart, and then to let Lazarus do the event naming.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

linnemann

  • New Member
  • *
  • Posts: 33
Re: How should an Event Handler look like ?
« Reply #8 on: September 23, 2021, 10:55:51 am »
Could you not use this as a base?

Then you can pass in the button or other component you want as the AControl.

Stolen from https://stackoverflow.com/questions/414928/is-there-any-way-to-get-all-the-controls-on-a-container-control

Code: Pascal  [Select][+][-]
  1. procedure TForm6.ModifyControl(const AControl: TControl; const value: Boolean);
  2. var
  3.   i: Integer;
  4. begin
  5.   if AControl=nil then Exit;
  6.   if AControl is TWinControl then begin
  7.     for i := 0 to TWinControl(AControl).ControlCount-1 do
  8.       ModifyControl(TWinControl(AControl).Controls[i], value);
  9.   end;
  10.   Acontrol.Enabled := value;
  11. end;

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 298
Re: How should an Event Handler look like ?
« Reply #9 on: September 27, 2021, 02:15:37 pm »
Would anyone else have some tips on how the methods should be named ? :)

Since nobody else has said anything: my practice is to change the object name from e.g. Button1 to ButtonStart, and then to let Lazarus do the event naming.

MarkMLl


Sounds good ! i will try to do so :)

Could you not use this as a base?

Then you can pass in the button or other component you want as the AControl.

Stolen from https://stackoverflow.com/questions/414928/is-there-any-way-to-get-all-the-controls-on-a-container-control

Code: Pascal  [Select][+][-]
  1. procedure TForm6.ModifyControl(const AControl: TControl; const value: Boolean);
  2. var
  3.   i: Integer;
  4. begin
  5.   if AControl=nil then Exit;
  6.   if AControl is TWinControl then begin
  7.     for i := 0 to TWinControl(AControl).ControlCount-1 do
  8.       ModifyControl(TWinControl(AControl).Controls[i], value);
  9.   end;
  10.   Acontrol.Enabled := value;
  11. end;

Looks good but unfortunately I just wanted to have a structured /organized setup. Do not have any logic problems at the moment. Thanks anyway !:)
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

 

TinyPortal © 2005-2018