Solved

OnDragDrop event

Posted on 1998-12-29
6
245 Views
Last Modified: 2010-04-04
Hello,
  I'm writing a TCalendar component based on TPanel. Each day is an object witch ancestor is TCustomPanel.
In Order to perform a period selection i use each days's Drag/Drop events with something like this :
If source is TCustomPanel then
begin
  // code for painting the selected period
end;

All of this works nice.
Here is my problem :
 In case of the source is not TCustomPanel I want to raise an event from the TCalendar, with those parameters :
  Sender : Tobject ; TheDate:TDate

So :
  1) How do i create my own event on the TCalendar
  2) How does each TJour could raise this event with the parameters

PS : Each day "knows" witch date is represents

Thanks in advance
0
Comment
Question by:helpme020897
  • 3
  • 2
6 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 200 total points
ID: 1353584
Hi...

try this:

type
  TDayDragEvent = procedure(Sender: TObject; theDate: TDate) of object;
  TCalendar = class(TCustomPanel)
    ...
  private
    ...
    FOnDayDrag: TDayDragEvent;
    procedure SetOnDayDrag(proc: TDayDragEvent);
    ...
  public
    ...
  published
    ...
    property OnDayDrag: TDayDragEvent read FDayDrag write SetOnDayDrag;
    ...
  end;

procedure TCalendar.SetOnDayDrag(proc: TDayDragEvent);
begin
  if @FOnDayDrag<>@proc then begin
    FOnDayDrag:=proc;
    ...
  end;
end;

To fire this event you should write something like this:

  if Assigned(FOnDayDrag) then
    FOnDayDrag(self,FMyDate);

Regards, Madshi.

P.S: Any more questions? Please ask...
0
 

Author Comment

by:helpme020897
ID: 1353585
I m testing Madshi's help ....

0
 

Author Comment

by:helpme020897
ID: 1353586
WONDERFULL !!   :-)   :-)  :-)
It works very fine ! except just one very little mistake :

Property OnDayDrag:TDragEvent read fOnDayDrag write SetOnDayDrag
                                    ^^
I write this because of if someone else would like starting with event creatin and read this question later. :-)

Madshi :could you explain step by step exactly what you wrote ? This, because i wrote all, but did not understand everything.

Greatests thanks.

HelpMe
                                     
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 20

Expert Comment

by:Madshi
ID: 1353587
It's quite easy.
First of all you have to define an event procedure/function type. You did that in your question already.
I only wrote it in Delphi syntax:
type
  TDayDragEvent = procedure(Sender: TObject; theDate: TDate) of object;

You must add this "of object" because the event is part of the object. The event handler will look like
procedure TCalender1.DayDrag(Sender: TObject; theDate: TDate);

The "of object" has two effects: There must be an object in front of the procedure ("TCalender1.") and there will be a hidden "self" parameter that you can use inside the procedure.

Now when the user of your component now clicks into the object inspector to write a handler procedure for your OnDayDrag event, Delphi will automatically write an empty new procedure with the right parameters like you know it from OnCreate.
Now your component has to save the address of this new event handler procedure, since you want to jump to it later, when the event is about to be fired.
So you have to create a private variable. I called it "FOnDayDrag".
Ok, now you must add a new property, so that the object inspector displays your event.
This is done by the line
    property OnDayDrag: TDayDragEvent read FDayDrag write SetOnDayDrag;
You could have written
    property OnDayDrag: TDayDragEvent read FDayDrag write FDayDrag;
too. Then you would not need the SetOnDayDrag procedure. But then you can't execute any code if a handler is added. If that is ok for you, then you should delete the SetOnDayDrag function and change the property line.
Otherwise you'll have to write the simple SetOnDayDrag procedure. In this procedure I first checked if the address of the function changed ("@func1<>@func2"). If not, nothing needs to be done. If yes, we must save the new handler procedure address in our variable FOnDayDrag, then perhaps execute some additional code.
Finally, if you want the event to be fired, you just need to look if somebody wants this event (that means if FOnDayDrag is not nil). If it is not assigned (you can write "if Assigned(FOnDayDrag) then" or "if @FOnDayDrag<>nil then") then do nothing. Otherwise just jump to the handler procedure by writing "FOnDayDrag(self,FMyDate)".

Regards, Madshi.

Hmmm. What was wrong with my sources? I don't see any mistakes...
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1353588
Madshi: After your comment regarding using @ when assigning proc pointers you go and do this :-) :-) :-)

You don't need them when comparing event handlers.

Cheers,

Raymond.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353589
Hi Raymond!

You NEED it here...   :-)
If I write "if FOnDayDrag<>..." Delphi will say "parameter missing" because Delphi thinks I want to execute FOnDayDrag.

Regards, Madshi.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 90
Help on project with Soap 10 54
Delphi: Connect to running MS Outlook 4 88
tvirtualstringtree freeze when load too manny images 10 63
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question