Edit UserControl subclass in Windows Form Designer?
In VS2010, targeting Framework 4.0, I have a UserControl which contains a TableLayoutPanel with an empty second row.
Is it possible to add items to this second row using the Windows Forms Designer, or do I have to do it all via code?
EDIT: In reply to Hans Passant's comment, the linked question references a UserControl being used inside another control. I am asking a question about inherited controls.
I have created the following test code, where TestControl is a UserControl containing a TableLayoutPanel named "TableLayoutPanel1":
Imports System.ComponentModel
Imports System.Windows.Forms.Design
<Designer(GetType(testDesigner))>
Public Class TestControl
End Class
Public Class testDesigner
Inherits ControlDesigner
Public Overrides Sub Initialize(component As System.ComponentModel.IComponent)
MyBase.Initialize(component)
Dim X As TestControl = CType(component, TestControl)
EnableDesignMode(X.TableLayoutPanel1, "TableLayoutPanelX")
End Sub
End Class
Creating the following class in a new file:
Public Class TestInheritedControl
Inherits TestControl
End Class
c开发者_Python百科reates a control which, when viewed in the designer, contained a TableLayoutPanel named "TableLayoutPanel1" which is not designer-editable. This is the situation described in my original question, which is not solved.
Creating a new UserControl, not inheriting from either of the previous UserControls, and then placing a TestControl into it creates an instance of TestControl with a Designer-editable TableLayoutPanel named "TestControl1.TableLayoutPanelX".
Is there any way to make the TestInheritedControl
class, shown above, Designer-editable?
I just had to do something similar today. The way I worked around it is to use un-instantiated references in the base class, but do the actual instantiation on the inheriting class.
You have to add a couple of steps to make everything fully plug-in, but it gives you the control you want. You will have to drop the TableLayoutPanel onto each inheriting control type, and in the constructor, set the base class' version to the current instantiated version (after calling initializecomponents).
This way, you can "design" it in the inheriting control, but use it programmaticly from the base control.
精彩评论