Reputation: 1264
I like to create an user control that (among other controls) contains a panel. Fellow developers should be able to add other controls at design time to the panel of my user control.
Unfortunately all the examples I found on the internet don't seem to work properly (or I do something wrong, what may be more likely).
For all my code snippets I created a new winforms project and added a user control that contains just a panel and nothing else. The panel fills the whole user control.
The first solution I found on the net is to attribute my control like this:
<Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", GetType(IDesigner))> _
Public Class MyUserControl
...
When I compile my program and add my user control to a form, I'm able to add other controls to my user control, but I don't see these controls, neither at design time nor runtime. I only see the added controls in my control while dragging my control with the mouse on the form.
The second solution I found is to create my own ControlDesigner
. For this I exposed the panel of my user control to the public, ...
Public Class MyUserControl
Public ReadOnly Property Panel As Panel
Get
Return Me.InnerPanel
End Get
End Property
End Class
... created my very own ControlDesigner
...
Public Class MyUserControlDesigner
Inherits ParentControlDesigner
Public Overrides Sub Initialize(ByVal component As IComponent)
MyBase.Initialize(component)
If (TypeOf MyBase.Control Is MyUserControl) Then
MyBase.EnableDesignMode(DirectCast(MyBase.Control, MyUserControl).Panel, "Panel")
End If
End Sub
End Class
... and changed the attribute of my user control:
<Designer(GetType(MyUserControlDesigner))> _
Public Class MyUserControl
...
Now I am able to add controls to my user control at design time and I even see these added controls, at least until I start the program.
After that I can't see the added controls anymore because the designer generated code doesn't add these controls to the Controls
collection of my panel.
Do you have any hint, why it's not working like I want it to work?
Since I like to use the custom ControlDesigner
anyway, I'd prefer a hint or a solution for the second way.
And I know, that for this example I didn't need to use an user control, I could've inherited from Panel
. But this is just a simplified example of the control I like to create, so inheriting from Panel
isn't an option in the end.
Thank you for your help!
Upvotes: 1
Views: 2065
Reputation: 85
I was facing the same problem. When I inherited a Class form the Control i created, the different sub-Controls on it (like a GroupBox, or a Button) were disabled in the designer; they cannot be moved and their properties cannot be edited.
The solution was to declare the sub-Controls as Public. First the were Private with a Getter/Setter-Methode making them exposed to the outer world. This worked fine just if the Control was added in the designer on a existing Form or on another Control. But when I opened an inherited class from this Control in the designer, the described problem occurred. Changing the sub-Controls to be Public was a solution to this.
Upvotes: 0
Reputation: 21
Just came across this question. The question is quite old, however, if you haven't found any solution, pls try the below:
Download the source code from the below link:
http://addressof.com/articles/transcontrols_pt1.zip
Open up TransPanel.vb and change the
Inherits System.Windows.Forms.Panel
to
Inherits System.Windows.Forms.UserControl
Build the solution. Now add some random controls to this TransPanel.vb like textboxes or labels.
In the Form1, you can see the earlier TransPanel panel present. Delete it replace with a normal Panel- Panel1.
Set the background color of panel1 to Transparent.
Load the modified 'TransPanel' usercontrol into your Form1's panel1:
Dim f As New TransControls.TransPanel
Panel1.Controls.Add(f)
f.Show()
Execute program. You can see that the panel containing the Usercontrol is semi transparent.. With controls!
Upvotes: 0