How do you disable a container control, like a panel control, from allowing the addition of other child
controls to it?
Thanks
Printable View
How do you disable a container control, like a panel control, from allowing the addition of other child
controls to it?
Thanks
You have to create a custom Designer for the Panel, which inherits the ParentControlDesigner class.
Then you can override the CanParent method to restrict the controls that can
be added to the Panel via the designer.
To prevent runtime additions, you'll have to create a custom ControlsCollection
and override the Add method to throw an exception when an invalid control is added, i.e.Regards,VB Code:
Imports System.ComponentModel Imports System.Windows.Forms.Design ' Assign the Designer for this Control <Designer(GetType(MyPanelDesigner))> _ Public Class MyPanel Inherits Panel ' Override the CreateControlsInstance to return our custom ControlsCollection Protected Overrides Function CreateControlsInstance() As System.Windows.Forms.Control.ControlCollection Return New MyPanelControlCollection(Me) End Function End Class ' Custom ControlsCollection, that prevents anything ' but Labels from being added Public Class MyPanelControlCollection Inherits Control.ControlCollection Public Sub New(ByVal owner As Control) MyBase.New(owner) End Sub Public Overrides Sub Add(ByVal value As System.Windows.Forms.Control) If Not value.GetType() Is GetType(Label) Then Throw New ArgumentException("Only Labels Allowed!") End If MyBase.Add(value) End Sub End Class ' ParentControlDesigner that prevents anything but ' labels from being added to the control in the Designer Public Class MyPanelDesigner Inherits ParentControlDesigner Public Overloads Overrides Function CanParent(ByVal control As System.Windows.Forms.Control) As Boolean If control.GetType() Is GetType(Label) Then Return True End If Return False End Function End Class
- Aaron.
:thumb:'s Aaron.
One other question is if the LinkLabels are required to be public class then the user could add a LL manually to the subpanel? :(
There will be a duplicate "design structure" of the Panels for the LinkLabels too.
Ps, I already finished the property updates to both the UC and the SubPanels. Working on adding the
LinkLabel Headers and changing the PanelHeaderClick events over to the LL Headers. ;)
It depends on what you "want" it to do, if you don't want to allow the user to manually add "Link Labels",
Then you could derive your own class from the LinkLabel and only allow that and not make it publicly available.
Regards,
- Aaron.
Yes, I have a class that is Inheriting from the LinkLabel control. Now this inherited class will also have the
related collection class. In order for the UITypeEditor to display the LLs they need to be Public :( so if preventing
the user from adding other controls works with the code you posted then the LL class will also show since its Public
and they can add those to the control manually which is what I dont want.
Like the Tab control, you can not add tabs from the toolbox nor see them, but through the designer you can
add them. This is what I need to replicate.
Thanks,
Robert
Ok, I got the code implemented but I made a modification so I can only allow two types of controls.
One, the SubPanels and two, the about label.
The code is working great!VB Code:
Public Class MyMainPanelControlCollection Public Overrides Sub Add(ByVal value As System.Windows.Forms.Control) If Not value.GetType() Is GetType(MySubPanel) And Not value.GetType() Is GetType(MyUC.About.AboutLabel) Then Throw New ArgumentException("Only MySubPanels Allowed!") End If MyBase.Add(value) End Sub
Thanks,
Robert
RobDog888 do you have a finished example for this project? I am deperately trying to do something similar in VS 2005. Scoured the web and this site with not much luck.
Thanks,
Christian