UserControl inheritance and the designer

B

Bill E.

I've created a base class from which many usercontrols will inherit.
The base class contains declarations for controls and event handlers.
A derived user control contains the actual form controls (labels,
textboxes, etc.). Everything seems to be working fine at run time but
at at design time, the designer cannot display the derived control
unless I place declarations for the various form controls in the
designer code. See the code below for an example. Note that if I
remove the comment from the line

'Friend Shadows Label1 As Label

in the derived control, the designer is happy and works fine but of
course this effectively breaks the link between the controls and their
handlers in the base class. If I retain the comment, the designer
gives the error

"The variable 'Label1' is either undeclared or was never assigned."

How do I overcome this? Is there a way that I can fool the designer
without commenting/uncommenting code? Am I simply doing this all
wrong?

Bill E.
Hollywood, FL


'--------- Base Class ---------------
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class basecontrol1
Inherits UserControl

'Required by the Windows Form Designer
Protected components As System.ComponentModel.IContainer
Protected WithEvents Label1 As System.Windows.Forms.Label

Public Sub New()
'Instantiate
End Sub

Protected Overridable Sub Form_Load(ByVal obj As Object, ByVal e
As EventArgs) Handles Me.Load
If Not (Label1 Is Nothing) Then
Label1.Text = Me.Name
End If
End Sub

Protected Overridable Sub Label1_Click(ByVal obj As Object, ByVal
e As EventArgs) Handles Label1.Click
MsgBox("Hello")
End Sub

End Class


'---------- Derived UserControl -----------
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class control1
Inherits basecontrol1

'Friend Shadows Label1 As Label

<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.SuspendLayout()
'
'Label1
'
Me.Label1 = New System.Windows.Forms.Label
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(66, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "This is a test"
Me.Controls.Add(Me.Label1)

End Sub

End Class


Public Class control1
'Inherits basecontrol1

Protected Overrides Sub Label1_Click(ByVal obj As Object, ByVal e
As EventArgs)
MsgBox("I've been overridden")
End Sub


End Class
 
B

Bill E.

I have found a workaround to this. I have defined a new configuration
called "Design" using the Configuration Manager. I then placed the
following conditional compilation code into the designer of the class
that inherits from the base class:

#If CONFIG = "Design" Then
Friend WithEvents Label1 As Label = MyBase.Label1
#End If

Now, if I want to see the user control in the designer, I simply
change the configuration to "Design". To run the application, I use
the other configurations. This is certainly an added inconvenience
but is all I have for now. It would be nice if I could use a
conditional compilation statement that checks if I'm in design mode.
In that case, I wouldn't need to keep switching between
configurations.


Bill E.
 

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