Solved

OnDragDrop event

Posted on 1998-12-29
6
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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