Recent

Author Topic: Add event to objects  (Read 986 times)

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Add event to objects
« on: May 22, 2020, 12:19:27 pm »
Hello!

I declared my TImages and I want to add Mouse click/move/up events (to move them).

Code: Pascal  [Select][+][-]
  1. var target:array[1..50] of TImage;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4.  var i:integer;
  5. begin
  6.   if RadioButton1.enabled then begin
  7.     i:=0;
  8.     repeat
  9.       i:=i+1;
  10.     until (i=maxtarget) or (created[i]=false);
  11.     if created[i]=true then full:=true;
  12.     if full=false then begin
  13.       targetcount:=targetcount+1;
  14.       created[i]:=true;
  15.       target[i]:=TImage.Create(Form2);
  16.       target[i].Parent:=Form2;
  17.       target[i].Left:=30;
  18.       target[i].Top:=30;
  19.       target[i].Width:=32;
  20.       target[i].Height:=32;
  21.       target[i].Picture.LoadFromFile(homefolder+'\Texture\targetskin.bmp');
  22.       target[i].Stretch:=true;
  23.       target[i].Visible:=true;
  24.       target[i].Enabled:=true;
  25.       target[i].OnMouseDown:=;   //And now?
  26.     end;
  27.   end;
  28. end;
  29.  

Thanks for answers!
Check my ProjectShare site and share your content: FPCShare

lucamar

  • Hero Member
  • *****
  • Posts: 2850
Re: Add event to objects
« Reply #1 on: May 22, 2020, 12:35:57 pm »
Code: Pascal  [Select][+][-]
  1.       target[i].OnMouseDown:=;   //And now?
Thanks for answers!

Code: Pascal  [Select][+][-]
  1.       { ... }
  2.       target[i].OnMouseDown:= @TargetMouseDown;   //And now *this*
  3.       { ... }
  4.  
  5. {... elsewhere on your source ...}
  6. procedure TForm1.TargetMouseDown((Sender: TObject;
  7.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  8. begin
  9.   {... whatever you want to do here ...}
  10. end;

Of course, remember to declare TargetMouseDown() in the declaration of TForm1, as the IDE does with the other event handlers.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #2 on: May 22, 2020, 12:44:54 pm »
Thanks!!!!
Check my ProjectShare site and share your content: FPCShare

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #3 on: May 22, 2020, 01:04:02 pm »
And how can I do it for array?

So I have to can grab all of (array of TImage) and move them.

So how can I test in event, which is the grabbed?

(I think there is an easier mode than create 50x3 event...)
Check my ProjectShare site and share your content: FPCShare

BrunoK

  • Sr. Member
  • ****
  • Posts: 250
  • Retired programmer
Re: Add event to objects
« Reply #4 on: May 22, 2020, 01:42:29 pm »
Absolutely untested ...
Code: Pascal  [Select][+][-]
  1. {... elsewhere on your source ...}
  2. procedure TForm1.TargetMouseDown(Sender: TObject; Button: TMouseButton;
  3.   Shift: TShiftState; X, Y: integer);
  4. var
  5.   i: integer;
  6. begin
  7.   {... whatever you want to do here ...}
  8.   if assigned(Sender) then begin
  9.     for i := 0 to High(target) do
  10.       if Sender = target[i] then;
  11.         // here you have the index to the target
  12.         {... whatever you want to do here ...}
  13.   end;
  14. end;

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #5 on: May 22, 2020, 01:55:34 pm »
That's works!!!!!


Thanks!!
Check my ProjectShare site and share your content: FPCShare

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #6 on: May 22, 2020, 02:18:35 pm »
And.. How can I create an Pointer to my objects?


for example:

Code: Pascal  [Select][+][-]
  1. procedure ontargetclick();
  2. begin
  3. selectedobject:=target[i];
  4. end;
  5.  
  6. procedure SpinEditChange();
  7. begin
  8. selectedobject.Left:=SpinEdit1.Value;
  9. end;
  10.  
Check my ProjectShare site and share your content: FPCShare

lucamar

  • Hero Member
  • *****
  • Posts: 2850
Re: Add event to objects
« Reply #7 on: May 22, 2020, 02:40:17 pm »
Exactly like that, always supposing selectedobject is properly declared and in the scope. Though I'd do it like:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SpinEditChange();
  2. begin
  3.   if Assigned(selectedobject) then
  4.     selectedobject.Left:=SpinEdit1.Value;
  5. end;

And more correct for the OnClick handler would be:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ontargetclick(Sender: TObject);
  2. begin
  3.   if Sender.InheritsFrom(TheObjectClass) then
  4.     selectedobject := TheObjectClass(Sender);
  5. end;
where TheObjectClass is the class of the objects in the target[] array and selectedobject
« Last Edit: May 22, 2020, 02:51:10 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #8 on: May 22, 2020, 02:56:36 pm »
So with that:

Code: Pascal  [Select][+][-]
  1. selectedobject.destroy;
  2.  

I'm destroying the target and unassign the selectedobject?

EDIT:
I see, no.

But how can I clear the pointer?
I have to use a dummy?
« Last Edit: May 22, 2020, 02:59:09 pm by Jake012345 »
Check my ProjectShare site and share your content: FPCShare

lucamar

  • Hero Member
  • *****
  • Posts: 2850
Re: Add event to objects
« Reply #9 on: May 22, 2020, 03:10:44 pm »
Code: Pascal  [Select][+][-]
  1. FreeAndNil(selectedobject);
but note that that will also free the corresponding Target[x], so you'll now have to add tests for that everywhere you use it, like in:
Code: Pascal  [Select][+][-]
  1. if Assigned(target[i]) then ...

If you only want to let selectedobject pointing to nothing just do: selectedobject := Nil; and let the objects destruction to the destructor of the array/list object or to the OnClose event handler of the form or whatever.

do I have to use a dummy?

Well, that's basically what FreeAndNil() does. Nothing wrong with that ;D



BTW, instead of Object.Destroy you should get used to calling Object.Free. That's the (more) conventional way to dispose of an object, if for nothing else than because it checks whether the instance is assigned before itself calling destroy.
« Last Edit: May 22, 2020, 03:20:38 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #10 on: May 22, 2020, 09:54:10 pm »
So If I use this:

Code: Pascal  [Select][+][-]
  1. FreeAndNil(selecteditem);
  2.  

than the target[n] will be dastroyed, 'killed'?
And I can create it again with the command, what I wrote for the first creating?
Check my ProjectShare site and share your content: FPCShare

lucamar

  • Hero Member
  • *****
  • Posts: 2850
Re: Add event to objects
« Reply #11 on: May 22, 2020, 11:20:40 pm »
So If I use this:

Code: Pascal  [Select][+][-]
  1. FreeAndNil(selecteditem);
  2.  

than the target[n] will be dastroyed, 'killed'?
And I can create it again with the command, what I wrote for the first creating?

Yes, if selecteditem is set with something like:
Code: Pascal  [Select][+][-]
  1. selecteditem := target[x];
then selecteditem is just a reference to target[x] (think of it as equating two pointers, if that's easier for you), so the net result of FreeAndNil(selecteditem) is almost the same as doing:
Code: Pascal  [Select][+][-]
  1. target[x].free;
  2. selectemitem := Nil;

Note that target[x] is NOT set to Nil but it's left pointing (now) to the limbo; it shouldn't matter much but you should be careful with how you acccess the members of the array.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #12 on: May 23, 2020, 07:07:19 am »
What means the @ sign?

And how can I add KeyboardShortcuts to both of 2 form with one component? (I think, the actionlist better for me)

So I must add access for the 2nd form.
« Last Edit: May 23, 2020, 07:10:25 am by Jake012345 »
Check my ProjectShare site and share your content: FPCShare

TRon

  • Jr. Member
  • **
  • Posts: 99
Re: Add event to objects
« Reply #13 on: May 23, 2020, 08:40:22 am »

lucamar

  • Hero Member
  • *****
  • Posts: 2850
Re: Add event to objects
« Reply #14 on: May 23, 2020, 10:37:07 am »
And how can I add KeyboardShortcuts to both of 2 form with one component? (I think, the actionlist better for me)

Yeah, I alwas use actions for form-global shortcuts, but for more than one form you'll need an action list for each form.

Another way to add form shortcuts is to set KeyPreview to True and respond to OnKeyDown and/or OnKeyUp events but I've found that using actions is normally better.

There are "tricks" to use a single ActionList for several forms but from your questions I think you shouldn't try them yet; use an ActionList (and action handlers) on each form and you're set ;)
« Last Edit: May 23, 2020, 10:40:10 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018