Hello,
i have a user contol called container_control. The three other controls kalk_container, preis_container, ... inherit from container_control. In a Panel i have overwritten the OnDragDrop Event. Within this Event i want to change the Location of the Controls dragged in. But i just want to allow controls inherited from container_control to be dragged. My code now is like this:
protected override void OnDragDrop(DragEventArgs drgevent)
{
base.OnDragDrop (drgevent);
container_control dragItem = null;
if(drgevent.Data.GetDataPresent(typeof(Kalkulation.Steuerelemente.preis_control)))
{
dragItem = drgevent.Data.GetData(typeof(preis_control)) as preis_control;
}
else if(drgevent.Data.GetDataPresent(typeof(objekt_control)))
{
dragItem = drgevent.Data.GetData(typeof(objekt_control)) as objekt_control;
}
else if(drgevent.Data.GetDataPresent(typeof(bedingung_control)))
{
dragItem = drgevent.Data.GetData(typeof(bedingung_control)) as bedingung_control;
}
if (dragItem != null)
{
dragItem.Location
= this.PointToClient(new Point(drgevent.X - dragItem.Width / 2, drgevent.Y - dragItem.Height / 2));
this.Invalidate();
}
}
Now i don't want to write an elseif part for every control that is inherited from container_control i just want to ask "are you a container_control" and then change it's location and redraw it.
Does anybody know how i can do this

Drag & Drop
vien11
Since this will only be applicaple at designtime, I would recommend attaching a ParentControlDesigner class for the custom panel. Attach a handler for the ChildControlAdded event of the control being designed (Panel), then test for the type of the control being added using either:
1. IS operator (to test if the control is or a derivative of your base control class)
2. Type.IsSubClassOf method to test if the added control's type is a subclass of your basecontrol class.
amc
One simple solution would be to use a little wrapper class to copy things to the clipboard. The wrapper would receive a instace of the base type in its constructor. Then you could test for an instance of the wrapper in GetDataPresent.
public class ClipboardWrapper
{
container_control _control;
public ClipboardWrapper(container_control control)
{
_control = control;
}
public container_control Control
{
get{ return _control; }
}
}
// set the object with an instance derived from container_control
ClipBoard.SetDataObject(new ClipboardWrapper(preis_control_instance));
// in OnDragDrop
if(drgevent.Data.GetDataPresent(typeof(ClipboardWrapper))
{
dragItem = ((ClipboardWrapper)drgevent.GetData(typeof(ClipbaordWrapper))).Control;
// set location
}