Solved

OnDragDrop event

Posted on 1998-12-29
6
242 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now