Copyright Accelerated Ideas 2007 Legacy Blog Posts | Blog Home | .NET Components

AICalendar - How to respond to drag drop events


1/2/2007

Developers should respond to two events to complete a drag drop operation:

The first event raised by AICalendar is the DragOver event. This event is raised when a user drags and item over any of the AICalendar days. Developers should respond to the event by confirming which DragDropEffects should be allowed to take place:

DragDropEffects.All
DragDropEffects.Copy
DragDropEffects.Link
DragDropEffects.Move
DragDropEffects.None
DragDropEffects.Scroll


Our demo uses the following example:

private void aiCalendar1_AICalendarDragOver(object sender, DragEventArgs e, AICalendarEventArgs DayPanelArgs)
{
string aString = (string)(e.Data.GetData(typeof(string)));
if (aString != null)
{
e.Effect = DragDropEffects.Copy;
}
}


Users of the demo application can thus only drag items onto the AICalendar which match the drag drop ‘Copy’ effect.

The second event is raised after a user ‘drops’ an item onto a Calendar day. Developers should use our custom EventArgs class to access the Calendar day which was affected by the drag drop:

private void aiCalendar1_AICalendarDragDrop(object sender, DragEventArgs e, AICalendarEventArgs DayPanelArgs)
{
string aString = (string)(e.Data.GetData(typeof(string)));
if (aString != null)
{

if (DayPanelArgs.DayPanel.Description != \"\")
{
DayPanelArgs.DayPanel.Description += \", \";
}
else //If the day hasn't been occupied yet..
{
//A holiday must have a nice colour of course!
if (aString == Extras[2])
DayPanelArgs.DayPanel.BlendColor = Color.LawnGreen;

if (aString == Extras[4])
DayPanelArgs.DayPanel.BlendColor = Color.LightCoral;

DayPanelArgs.DayPanel.Invalidate();
}

DayPanelArgs.DayPanel.Description += aString;

}
}


Notice the use of the following important pieces of code:

string aString = (string)(e.Data.GetData(typeof(string)));


To retrieve the object data from the drag drop operation we must access the ‘Data’ from the DragEventArgs object. This must be further cast to the desired type, in our demo we cast to a string type (which represents a ListBox item).

The second more important piece of code is the reference to the Calendar day which was affected. This can be accessed from the custom event args class DayPanelArgs:

DayPanelArgs.DayPanel


The DayPanel property gives direct access to the DayPanel which was affected by the drag drop operation. Developers can from here, access BlendColor, Description, Date, Title etc..

For more information on the features of AICalendar refer to the readme.htm file included in the download


< Blog Home