* * *

Author Topic: [REOPENED] Cloning contents from a tabsheet into another one (not the hard way!)  (Read 3836 times)

Raul_ES

  • New member
  • *
  • Posts: 37
How do I call an event handler from different controls?

I mean that I want to, in example, attach two different controls to the same event:

Button1 ------> procedure TForm1.BitBtn1Click(Sender: TObject);
MenuItem  ---------> has to connect with the same procedure BitBtn1Click(Sender: TObject);

thanks
« Last Edit: September 04, 2017, 10:49:26 pm by Raul_ES »
Raúl
Student of Computer Science - UOC

cpicanco

  • Sr. Member
  • ****
  • Posts: 364
    • My Portfolio
Do you mean something like this:

Code: Pascal  [Select]
  1. Button2.OnClick := @Button2Click;
  2. Button1.OnClick := @SharedClickEvent;
  3. MenuItem.OnClick := @SharedClickEvent;

Handoko

  • Hero Member
  • *****
  • Posts: 1514
  • My goal: build my own game engine using Lazarus
@Raul_ES

I can't open your project. I got this error:

Raul_ES

  • New member
  • *
  • Posts: 37
Thanks, you can remove the uses entry that makes reference to this package, and also the control in the frame2. I am using CodeTyphon,so probably it's one of it's packages.

I have updated the 7zip file in the post.
Raúl
Student of Computer Science - UOC

Raul_ES

  • New member
  • *
  • Posts: 37
Do you mean something like this:

Code: Pascal  [Select]
  1. Button2.OnClick := @Button2Click;
  2. Button1.OnClick := @SharedClickEvent;
  3. MenuItem.OnClick := @SharedClickEvent;

Thanks cpicanco, I think that yes but excuse my dummy question: where do you place this code?
Raúl
Student of Computer Science - UOC

cpicanco

  • Sr. Member
  • ****
  • Posts: 364
    • My Portfolio
Ok, the minimum requirement is a class. I am not sure if it should be private, public, published, but this way it will work and will be available in the Events tab of the object inspector (OnClick):

Code: Pascal  [Select]
  1. TForm1 = class(TForm)
  2. Button1 : TButton;
  3. Button2: TButton;
  4. MenuItem1: TMenuItem;
  5. procedure SharedClickEvent(Sender : TObject);
  6. private
  7.  
  8. public
  9.  
  10. end;  

Handoko

  • Hero Member
  • *****
  • Posts: 1514
  • My goal: build my own game engine using Lazarus
@Raul_ES

Your screenshot looks good. But I'm afraid you will disappointed to know it doesn't look good running on Linux (Ubuntu Mate 16.10).

I saw you asked question "Best practices for Multiplatform GUI portability". I think the most important thing to do is having different machines with different OSes for testing. If you can't afford them, you should at least have VirtualBox. I use Linux 64-bit, it has Wine for performing basic tests for Windows applications and I have an old pentium laptop with WinXP installed.

I guess your building an awesome program. May I ask what is the program you're currently developing?

Raul_ES

  • New member
  • *
  • Posts: 37
Thanks Handoko,

with your permission I'll use your screenshot in my other post. I run CodeTyphon under Windows, Linux, FreeBSD and OpenSolaris, on real machines. I wanna try also Raspberry Pi  :) when possible.

Porting GUIs between these systems is a pain in the neck. It's not Lazarus fault obviously, it's my lack of knowledge and experience. Even when changing from BSD/Gtk to Linux/GTK or Solaris/Gtk the results use to be painfull. I need to improve
my habilities in design. In this second example I developed the gui quickly under Windows without any considerations to portablility, so it's not really a surpraise what
you told me.


I currently develop 2 projects:

One is a molecule-analysis-design program. The other project is a pharmacy management software (complex indeed) intended for independent pharmacies and corporations. I am pharmacist, and I am currently graduating in computer science, so I would like to specialise in healthcare-scientifical IT, I think it's a very interestig field!
Right now it's just a hobby to fill my spare time but who knows if some day I will be able to run my own business.

cheers,
Raúl
Student of Computer Science - UOC

Raul_ES

  • New member
  • *
  • Posts: 37
Hi friends,

new problem.

I have a button.

It's event handler button.onClick() calls a procedure with a variable as argument:

createNewPOSFrame( type_of_frame : Integer)


Depending on it's value: 1, 2 ,3 ,4....

the procedure creates the frame with one skin/theme or another. (every Skin is a frame) But here comes the problem:

Code: Pascal  [Select]
  1. procedure TmainScreen.createNewFramePOS(tabsheetPOStheme: Integer);
  2.  
  3. const
  4.   POS_A = 1; // first skin, first type of frame
  5.   POS_B = 2;
  6.   POS_C = 3;
  7.   POS_D = 4;
  8.   POS_ ....... what ever;
  9.  
  10. var
  11.  
  12. tabsheetCounter: integer;
  13. newtabsheet: TTabSheet;
  14.  
  15. // I cannot do this!
  16. newframePOS: TFrame_POS_C;
  17. newframePOS: TFrame_POS_B;
  18. newframePOS: TFrame_POS_A;
  19.  
  20. // I can't also do: newframePOS: nil;
  21.  
  22. begin
  23. tabsheetCounter := PageControl_mainScreen.PageCount + 1;
  24. if (tabsheetCounter <= MAX_POS_TABS) then {can add a new tabsheet to pagecontrol}
  25. begin
  26.  
  27.     newtabsheet := PageControl_mainScreen.AddTabSheet;
  28.         with newtabsheet do
  29.           begin
  30.             Name := 'tabsheetPOS' + IntToStr(tabsheetCounter);
  31.             Caption := 'POS [' + IntToStr(tabsheetCounter)+']';
  32.           end;
  33.  
  34.         // I need to do something like this, but I can't:
  35.         if (tabsheetPOStheme = POS_A) then
  36.             newframePOS := TFrame_POS_A.Create(newtabsheet);
  37.         if (tabsheetKind = POS_B) then
  38.             newframePOS := TFrame_POS_B.Create(newtabsheet);
  39.         if (tabsheetKind = POS_C) then
  40.             newframePOS := TFrame_POS_C.Create(newtabsheet);
  41.  
  42.         with newframePOS do
  43.           begin
  44.             Name := 'frame' + IntToStr(tabsheetCounter);
  45.             Parent := newtabsheet;
  46.             Align := alClient;
  47.           end;
  48.         // Set the new tabsheet as the active one
  49.         PageControl_mainScreen.ActivePage := newtabsheet;
  50.  
  51. .... more stuff
  52. end;
  53. end;
  54.  

How can you manage this kind of problem? Probably is a basic one but I don't seem
to see it.


thank you,




Raúl
Student of Computer Science - UOC

Handoko

  • Hero Member
  • *****
  • Posts: 1514
  • My goal: build my own game engine using Lazarus
TFrame_POS_C, TFrame_POS_B and TFrame_POS_A are descendants of TFrame right? Is yes, then you should do:

Code: Pascal  [Select]
  1. var
  2.   newframePOS: TFrame;

Raul_ES

  • New member
  • *
  • Posts: 37
Thank you very much Handoko.



Just another point: Why the following doesn't work?

newframePOS : TObject;  (something more generic, let's say that I don't want to specify what
kind of control I will use)


What if I want a variable to be for some reason one or another kind of object depending on
some other option? Example.


newinput: somegenericalobject;


newinput := TComboBox.Create(....

or

newinput := TEdit.Create(... whatever


thanks
Raúl
Student of Computer Science - UOC

Handoko

  • Hero Member
  • *****
  • Posts: 1514
  • My goal: build my own game engine using Lazarus
Just another point: Why the following doesn't work?

newframePOS : TObject;  (something more generic, let's say that I don't want to specify what
kind of control I will use)

Honestly, I'm not an expert of Object Oriented Programming. I know a bit more than you just because I ever wrote some components using the OOP way.

You asked why it doesn't work. Actually it can compile (although I haven't tried) and it should work. But that newframePOS cannot used as a 'normal' frame. Because on the TObject level, it does not have any features or to be exactly, it does not offer properties and methods for a frame (example: Width, Height, BorderStyle, Font, Color, etc).

If you use TObject for newframePOS and you want to use it as a TFrame, you can use type casting.
http://etutorials.org/Programming/mastering+delphi+7/Part+I+Foundations/Chapter+2+The+Delphi+Programming+Language/Type-Safe+Down-Casting/

newinput: somegenericalobject;

newinput := TComboBox.Create(....
or
newinput := TEdit.Create(... whatever

For this case, you should:
Code: Pascal  [Select]
  1. var
  2.   newinput: TWinControl;
« Last Edit: September 12, 2017, 07:48:02 pm by Handoko »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus