Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Custom Control with design time handlers for DragEnter and DragDrop - How to make them conditionally use default behavior
 

Custom Control with design time handlers for DragEnter and DragDrop - How to make them conditionally use default behavior

I have a custom control (container) that handles the DragEnter and DragDrop events in design time. The DragEnter code is as follows:

If Me.DesignMode Then

If e.Data.GetDataPresent("System.String") Then

lblStatus.Text =

"DragDropEffects.Copy"

e.Effect = DragDropEffects.Copy

Else

lblStatus.Text =

"DragDropEffects.None"

e.Effect = DragDropEffects.None

End If

End If

The Drag Drop event handler simply takes the data dropped onto the user control and casts it to the appropriate object and draws a control dyhnamically onto the user control. This works fine. However, now when I try dropping any other type of regular control (button, label, etc.) onto the container and moving them with the mouse, I cannot do so. When I click and try moving a newly drawn standard button on the user control it becomes invisible and doesn't work.

Does anyone have any ideas for me? I am very new to this technology (modifying events and items in the IDE at design time).

Raney  Tuesday, June 30, 2009 5:45 PM
I am sorry ... I forgot to add in the original question that I believe I need to tell the Site or Designer to use default behavior in some cases but I do not know how to do so.
Raney  Tuesday, June 30, 2009 5:46 PM

Hi Raney,

From your description, you created a custom control which inherits UserControl. You consider the control as a container and want to drop a regular control, such as button, onto the control, but the results is not correct. Please feel free to tell me if I misunderstand you.

At first, we need to be clear why the behavior is not what we wanted. The default designer of UserControl does not consider UserControl as a container, we cannot design UserControl when it is dropped onto a form. The container control inherits UserControl, so its designer keeps the same. When we drop a button onto the container control, the button is indeed added to the form. So while we are moving the container control, the button becomes invisible and is not moved together.

Mysolution is to modify the designer of the container control. The new designer is a container designer and support dropping controls onto it. This is my code snippet:


Imports System.ComponentModel

Imports System.ComponentModel.Design

<Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", GetType(IDesigner))> _

Public Class MyContainer

Inherits UserControl

End Class

You can get more information about custom designer from
http://msdn.microsoft.com/en-us/magazine/cc163758.aspx.

Best regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, July 03, 2009 8:40 AM

My ultimate goal is to have a user control that allows me to drag drop normal controls like buttons and labels onto it, but also I want to be able to drage items (serialized objects) from an external exe over to my control and have it's drag enter and drag drop events react accordingly and draw controls dynamically. I have over 1361 fields to draw with a lot of detail for each. I had the drag enter and drag drop working at design time for my control and it drew controls dynamically, but when I added the Designer attribute, that all stopped working. However, I could then drop controls onto the user control just fine with normal components like buttons, etc. However, with the Designer attribute, my drag enter and drag drop events no longer fired at design time.

Here is the code for my Class ... it has been hacked up a bit but I hope you can tell when I am trying to do:


Imports

System.ComponentModel

Imports

System.ComponentModel.Design

<Designer(

"System.Windows.Forms.Design.ParentControlDesigner, System.Design", GetType(IDesigner))> _

Public

Class PolicyGroupCtrl

#If

DEBUG Then

Private m_changeService As IComponentChangeService

Private m_host As IDesignerHost

#End

If

#If

DEBUG Then

Private Sub PolicyGroupCtrl_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop

'An Object was dropped

If Me.DesignMode Then

Try

lblStatus.Text =

"DragDrop"

Dim binaryFile As String = DirectCast(e.Data.GetData("System.String"), String)

Dim elem As PolicyObject.PolicyElement = Nothing

Dim grp As PolicyObject.PolicyGroup = Nothing

elem =

DirectCast(PolicyObject.Utils.BinaryDeserialize(binaryFile), PolicyObject.PolicyElement)

If Not elem Is Nothing Then

'A policy element was dropped

'===================================================================================================

'CODE: Add a new component

Dim trx As DesignerTransaction = m_host.CreateTransaction()

Dim newName As String = "ctl" & elem.Key.Replace("/", "_")

Dim newObj As PolicyObjectCtrls.PolicyElementCtrl = DirectCast(m_host.CreateComponent(Type.GetType("PolicyObjectCtrls.PolicyElementCtrl"), newName), PolicyObjectCtrls.PolicyElementCtrl)

newObj.Location =

Me.PointToClient(New Point(e.X, e.Y))

newObj.Height = 20

newObj.Width = 100

newObj.Text =

"Programmatically Added Control"

newObj.Parent =

DirectCast(Me, System.Windows.Forms.Control)

'newObj.Parent = DirectCast(m_host.RootComponent, System.Windows.Forms.Control)

'Me.Parent.Controls.Add(newObj)

trx.Commit()

'TODO: Figure out if I even need to do this

'Use ComponentChangeService to announce changing of the Containers' Controls collection

'm_changeService.OnComponentChanging(m_host.RootComponent,TypeDescriptor.GetProperties(m_host.RootComponent)("Controls")

'===================================================================================================

Else

'An unknown object type was dropped so DO NOTHING

MessageBox.Show(

"Error", "Unknown Item Dropped")

End If

Catch ex As Exception

MessageBox.Show(ex.Message,

"DragDrop Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try

End If 'If Me.DesignMode Then

End Sub

Private Sub PolicyGroupCtrl_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter

If Me.DesignMode Then

If e.Data.GetDataPresent("System.String") Then

lblStatus.Text =

"DragDropEffects.Copy"

e.Effect = DragDropEffects.Copy

Else

lblStatus.Text =

"DragDropEffects.None"

e.Effect = DragDropEffects.None

End If

End If

End Sub

#End

If

#If

DEBUG Then

Public Overrides Property Site() As ISite

Get

Return MyBase.Site

End Get

Set(ByVal Value As ISite)

' Clear any component change event handlers.

'ClearChangeNotifications()

' Set the new Site value.

MyBase.Site = Value

If Me.DesignMode Then

' Requests an IComponentChangeService from the design time environment using Component.Site.GetService()

m_changeService =

CType(GetService(GetType(IComponentChangeService)), IComponentChangeService)

' Requests an IDesignerHost service from the design time environment using Component.Site.GetService()

m_host =

CType(GetService(GetType(IDesignerHost)), IDesignerHost)

End If

' Register event handlers for component change events.

'RegisterChangeNotifications()

End Set

End Property

#End

If

Private Sub PolicyGroupCtrl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

#If

DEBUG Then

Me.AllowDrop = Me.DesignMode

Me.lblStatus.Visible = Me.DesignMode

Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle

#Else

Me.BorderStyle = Windows.Forms.BorderStyle.None

#End

If

End Sub

End

Class

Raney  Tuesday, July 07, 2009 2:36 PM

You can use google to search for other answers

Custom Search

More Threads

• MouseHover Event for Form Buttons (help please)
• How can I bind a toolbar to a form and unbind it to another form (like vs.net)
• Resources For Custom Controls
• How does PropertyGrid display ContextMenuStrip items?
• Customizing DesignSurface to allow resizing of UserControl child controls
• Smart Panel not refreshing
• How to add and read the data in a ListBox of MDI Windows Form project with 1 "Parent" & 2 "Child" Forms with 2 Classes?
• TableLayoutPanel troubles -- adding runtime controls
• ArrayEditor and Panel Control
• How to change disabled textbox fonts color ?