I previously didn't submit this under the MSDN account sign-on. Here is the
question again.
I have a UserControl that is acting as a container for other controls. This
UserControl has a custom designer inheriting from ParentControlDesigner.
During design time I want to detect that a control has been dropped on it
and redimension the control based on the parent control size. In the custom
designer I override the OnDragDrop method like so:
Public Class MyUserControlDesigner
Inherits ParentControlDesigner
...
Protected Overloads Overrides Sub OnDragDrop(ByVal de as DragEventArgs)
If de.Data.GetDataPresent(".NET Toolbox Item") then
MyBase.OnDragDrop(de)
End If
Dim selectionService as ISelectionService =
Ctype(Me.GetService(GetType(ISelectionService)), ISelectionService)
Dim selectedComponents as ICollection =
selectionService.GetSelectedComponents()
If selectedComponents.Count = 1 Then
Dim enumerator as IEnumerator = selectedComponents.GetEnumerator()
If enumerator.MoveNext AndAlso TypeOf enumerator.Current is
MyUserControl Then
Dim myControl as MyUserControl = CType(enumerator.Current,
MyUserControl)
If myControl.Parent.Equals(Me.Control) Then
'Do the redimensioning of myControl
End If
End If
End If
End Sub
End Class
My first issue is a needed clarification. I found that there are three
different kinds of drag/drop events that happen:
1) Drag & Drop of a ToolBoxItem
I found that in order for everything to work correctly, when a
ToolBoxItem is dropped that I need to call MyBase.OnDragDrop otherwise the
currently selected component from ISelectionService is actually Me.Control
NOT the control being dropped. On the other hand I found that I absolutely
MUST NOT call MyBase.OnDragDrop if the control being dropped is NOT a
ToolBoxItem. Why is this? Is there a better way to handle this than what I
did?
2) Drag & Drop of a control that is currently a child of Me.Control &
NOT a ToolBoxItem
As long as I don't call MyBase.OnDragDrop this code works fine. Why
does calling MyBase.OnDragDrop cause ISelectionService to return Me.Control
rather than the control being dropped?
3) Drag & Drop of a control that is not currently a child of Me.Control
& NOT a ToolBoxItem (Both parent controls are of the same type)
This I couldn't get to work correctly. Notice that I specifically
don't do anything if the parent of the dropped control isn't Me.Control.
When I detected this I tried changing the parent of the control using
TypeDescriptor.SetValue to Me.Control and this actually causes the control
to move but after the control is moved it is no longer selectable until I
close and reopen the design surface. I tried calling the
ControlDesigner.HookChildControls (even though the documentation doesn't
actually say what it does it looked promising) but that didn't help. What
is the correct way to move a component from one parent control to another
parent control?

Signature
Robin Sanner
Roger - 09 Mar 2005 15:29 GMT
Instead of monitoring the Drag & Drop events to detect when components
have been added, subscribe to the ComponentAdded event of
IComponentChangeService.