Custom control with panel...

T

topdawg147

I'm trying to build a custom control that includes a panel control on
it. However, in design-time when I drag a new control (e.g. a textbox,
etc) into the panel, it's not added in the panel--just the overall
control.

* I do not want the entire user control to be a panel/container
control. I just want the panel part of it to work as expected.

Any suggestions?

thanks,
Todd
 
M

Mick Doherty

You need to implement a Designer for your usercontrol and create the panel
in it's OnSetComponentDefaults() method.

Here's a basic example in VB.

note: you need to add a reference to System.Design.dll.

\\\
Imports System.ComponentModel
Imports System.ComponentModel.Design

<Designer(GetType(MyDesigner))> _
Public Class MyUserControl
Inherits System.Windows.Forms.UserControl

'Your code here

End Class

Friend Class MyDesigner
Inherits System.Windows.Forms.Design.ControlDesigner

Public ReadOnly Property DesignerHost() As IDesignerHost
Get
Return DirectCast(GetService(GetType(IDesignerHost)), _
IDesignerHost)
End Get
End Property

Public ReadOnly Property HostControl() As Control
Get
Return Me.Control
End Get
End Property

Public Overrides Sub OnSetComponentDefaults()
Dim p As Panel = DirectCast(Me.DesignerHost.CreateComponent _
(GetType(Panel)), Panel)
p.Location = New Point(10, 40)
p.Size = New Size(HostControl.Width - 20, HostControl.Height - 50)
p.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or _
AnchorStyles.Right Or AnchorStyles.Bottom
HostControl.Controls.Add(p)
End Sub

End Class
///
 
T

topdawg147

Thank you. That does work. However, if possible i would like to the the
panel on the custom control while designing the custom control. I have
other controls on it, and i'd like to see them all together without
having to compile the project and drag the control onto a form.

thank you,
todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top