Solved

OnDragDrop event

Posted on 1998-12-29
6
246 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 327
code issue 8 156
Delphi Form ownership 4 114
I want to use librsync in my Delphi backup application. 3 59
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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