Visual Inheritance - Very Temperamental
I seem to be having a problem with Visual inheritance.
I have created a Mustinherit form that gets inherited and used by another
class. Only one big problem seems to Occur. It does open in design mode.
Runs fine just doesn't open in design.
I get the following message.
An Exception occured while trying to load an instance of ReG.frminsuredInterest
The Exception was "Constructor on type ReG.frmInsuredInterest not found"
FormInsuredInterest is the form being inherited
I have tried changing the constructor, removing the mustinherit, changing
the mustoverrrieds to overridable and still no.
Any help is greatly appreciated
it only seems to happen with about 50% of the visual Inheritance i do .
Some work some don't it's like a lottery
Cheers
Steve
Here is the Code
---Inherited Form
Public MustInherit Class frmInsuredInterest
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New(ByVal TypeID As BLL.DataStructures.TypeEnum)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
'Me.Type = TypeID
Me.chkHasGroup.Checked = False
Me.cboGroup.Enabled = False
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents cmdClose As System.Windows.Forms.Button
Friend WithEvents lstInterests As System.Windows.Forms.ListBox
Friend WithEvents cmdEditCancel As System.Windows.Forms.Button
Friend WithEvents cmdNewSave As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents lblComments As System.Windows.Forms.Label
Friend WithEvents lblName As System.Windows.Forms.Label
Friend WithEvents cboGroup As System.Windows.Forms.ComboBox
Friend WithEvents lblGroup As System.Windows.Forms.Label
Friend WithEvents chkHasGroup As System.Windows.Forms.CheckBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lstInterests = New System.Windows.Forms.ListBox()
Me.cmdClose = New System.Windows.Forms.Button()
Me.cmdEditCancel = New System.Windows.Forms.Button()
Me.cmdNewSave = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.lblComments = New System.Windows.Forms.Label()
Me.lblName = New System.Windows.Forms.Label()
Me.cboGroup = New System.Windows.Forms.ComboBox()
Me.lblGroup = New System.Windows.Forms.Label()
Me.chkHasGroup = New System.Windows.Forms.CheckBox()
Me.SuspendLayout()
'
'lstInterests
'
Me.lstInterests.Name = "lstInterests"
Me.lstInterests.Size = New System.Drawing.Size(128, 212)
Me.lstInterests.TabIndex = 0
'
'cmdClose
'
Me.cmdClose.Location = New System.Drawing.Point(360, 192)
Me.cmdClose.Name = "cmdClose"
Me.cmdClose.Size = New System.Drawing.Size(56, 24)
Me.cmdClose.TabIndex = 1
Me.cmdClose.Text = "&Close"
'
'cmdEditCancel
'
Me.cmdEditCancel.Location = New System.Drawing.Point(296, 192)
Me.cmdEditCancel.Name = "cmdEditCancel"
Me.cmdEditCancel.Size = New System.Drawing.Size(56, 24)
Me.cmdEditCancel.TabIndex = 2
Me.cmdEditCancel.Tag = "Edit"
Me.cmdEditCancel.Text = "&Edit"
'
'cmdNewSave
'
Me.cmdNewSave.Location = New System.Drawing.Point(232, 192)
Me.cmdNewSave.Name = "cmdNewSave"
Me.cmdNewSave.Size = New System.Drawing.Size(56, 24)
Me.cmdNewSave.TabIndex = 3
Me.cmdNewSave.Tag = "New"
Me.cmdNewSave.Text = "&New"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(184, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(232, 20)
Me.TextBox1.TabIndex = 4
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(136, 96)
Me.TextBox2.Multiline = True
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(280, 88)
Me.TextBox2.TabIndex = 5
Me.TextBox2.Text = ""
'
'lblComments
'
Me.lblComments.Location = New System.Drawing.Point(136, 80)
Me.lblComments.Name = "lblComments"
Me.lblComments.Size = New System.Drawing.Size(80, 16)
Me.lblComments.TabIndex = 6
Me.lblComments.Text = "Comments"
'
'lblName
'
Me.lblName.Location = New System.Drawing.Point(136, 8)
Me.lblName.Name = "lblName"
Me.lblName.Size = New System.Drawing.Size(40, 16)
Me.lblName.TabIndex = 7
Me.lblName.Text = "Name"
'
'cboGroup
'
Me.cboGroup.Location = New System.Drawing.Point(208, 32)
Me.cboGroup.Name = "cboGroup"
Me.cboGroup.Size = New System.Drawing.Size(208, 21)
Me.cboGroup.TabIndex = 8
'
'lblGroup
'
Me.lblGroup.Location = New System.Drawing.Point(136, 32)
Me.lblGroup.Name = "lblGroup"
Me.lblGroup.Size = New System.Drawing.Size(40, 16)
Me.lblGroup.TabIndex = 9
Me.lblGroup.Text = "Group"
'
'chkHasGroup
'
Me.chkHasGroup.Location = New System.Drawing.Point(184, 32)
Me.chkHasGroup.Name = "chkHasGroup"
Me.chkHasGroup.Size = New System.Drawing.Size(16, 16)
Me.chkHasGroup.TabIndex = 10
'
'frmInsuredInterest
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(424, 221)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkHasGroup,
Me.lblGroup, Me.cboGroup, Me.lblName, Me.lblComments, Me.TextBox2, Me.TextBox1,
Me.cmdNewSave, Me.cmdEditCancel, Me.cmdClose, Me.lstInterests})
Me.Name = "frmInsuredInterest"
Me.Text = "InsuredInterest"
Me.ResumeLayout(False)
End Sub
#End Region
#Region "Properties Used by the Form"
Protected Type As BLL.DataStructures.TypeEnum
Protected bSaved As Boolean = True
Private iloadcount As Int32
Protected Overridable Property sName() As String
Get
End Get
Set(ByVal Value As String)
End Set
End Property
Protected Overridable Property iGroupID() As Int32
Get
End Get
Set(ByVal Value As Int32)
End Set
End Property
Protected Overridable Property sComments() As String
Get
End Get
Set(ByVal Value As String)
End Set
End Property
#End Region
#Region "Event Handling Functions"
Private Sub DisposeOfForm(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles cmdClose.Click
If bSaved = False Then
If MessageBox.Show("Data Is Not Saved are you sure you want to
Close?", "Confirm Close", MessageBoxButtons.YesNo) = DialogResult.No Then
Exit Sub
End If
End If
Me.Dispose()
End Sub
Private Sub HandleButtons(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles cmdNewSave.Click, cmdEditCancel.Click
'Convert the Sender Object to a Button object
Dim oCmd As Button = CType(sender, Button)
'Process The Event Depending on the Button Tag
'MessageBox.Show(oCmd.Tag.ToString)
Select Case oCmd.Tag
Case "New"
oCmd.Tag = "Save"
Case "Save"
oCmd.Tag = "New"
Case "Edit"
oCmd.Tag = "Cancel"
Case "Cancel"
oCmd.Tag = "Edit"
End Select
'Set The Caption of the relevant Control
oCmd.Text = "&" & oCmd.Tag.ToString
End Sub
#End Region
Private Sub lstInterests_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles lstInterests.SelectedIndexChanged
iloadcount += 1
'temporary measure
If iloadcount > 5 Then
MessageBox.Show(Me.lstInterests.SelectedValue.ToString)
End If
End Sub
Private Sub LoadForm(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
'Bind data to the listbox
Dim Dt As DataTable = BLL.DataAccess.Lists.GetInsuredInterests
'Me.lstInterests.DataSource = Dt
'Me.lstInterests.DisplayMember = "InterestName"
'Me.lstInterests.ValueMember = "InterestID"
BindListBoxData(Dt)
'Bind Data to ComboBox
Dt = BLL.DataAccess.Lists.GetInsuredInterestGroups
Me.cboGroup.DataSource = Dt
Me.cboGroup.DisplayMember = "InterestName"
Me.cboGroup.ValueMember = "InterestID"
End Sub
Private Sub chkHasGroup_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles chkHasGroup.CheckedChanged
If Me.chkHasGroup.Checked = True Then
Me.cboGroup.Enabled = True
Else
Me.cboGroup.Enabled = False
End If
End Sub
Protected Overridable Sub BindListBoxData(ByVal Dt As DataTable)
'Assign the datatable to the list box
Me.lstInterests.DataSource = Dt
Me.lstInterests.DisplayMember = Dt.Columns(1).Caption
Me.lstInterests.ValueMember = Dt.Columns(0).Caption
End Sub
End Class
---Form to Create
Public Class frmPoliticalRisk
Inherits ReG.frmInsuredInterest
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New(BLL.DataStructures.TypeEnum.PoliticalRisk)
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Private oInterest As BLL.InsuredInterests.PoliticalRisk
Protected Overrides Property sName() As String
Get
Return oInterest.Name
End Get
Set(ByVal Value As String)
oInterest.Name = Value
End Set
End Property
Protected Overrides Property iGroupID() As Int32
Get
Return oInterest.GroupID
End Get
Set(ByVal Value As Int32)
oInterest.GroupID = Value
End Set
End Property
Protected Overrides Property sComments() As String
Get
Return oInterest.Comments
End Get
Set(ByVal Value As String)
oInterest.Comments = Value
End Set
End Property
End Class
Re: Visual Inheritance - Very Temperamental
Seems to work OK if the class is not abstract and you don't put any parameters
in the constructor. but you have to rebuild the project to get it to work.
if anyone knows of a way of inheriting an abstract form and getting it to
load into the designer i would appreciate it
Steve
Re: Visual Inheritance - Very Temperamental
Steve-
The issue is that anything displayed by the designer (e.g. textbox, label,
etc) is instantiated. The special cases are items which you are actually
designing (e.g. Form, UserControl). Because it isn't safe to instantiate
the item which you are designing, it instantiates the Base Class. So, if
you are desgining Form1, and it derives from Form, then it instantiates
Form, instead of Form1. Since your base class is Abstract, it can't
instantiate it. The trick is to do something like the following for the
base form:
#If DEBUG Then
Public Class MyForm
#Else
Public MustInherit Class MyForm
#End If
...
#If DEBUG Then
Public Overridable Sub Foo()
Throw New NotImplementedException()
End Sub
#Else
Public MustOverride Sub Foo()
#End If
End Class
--
Jacob Grass
Microsoft .NET MVP
"Steve Hall" <steve.hall@gerling.co.uk> wrote in message
news:3cab11aa$1@10.1.10.29...
>
> Seems to work OK if the class is not abstract and you don't put any
parameters
> in the constructor. but you have to rebuild the project to get it to work.
> if anyone knows of a way of inheriting an abstract form and getting it to
> load into the designer i would appreciate it
>
> Steve
Re: Visual Inheritance - Very Temperamental
Jacob,
If your explanation is correct (it seems to me), then I would call this a
feature bug: It works as intended, but not as expected.
The compiler should at least issue a warning when MustInherit is used. The
designer should ignore the MustInherit if it's possible, or create a mediate
inherited form to use. Or perhaps MustInherit should be forbidden on forms?
Using all those #If ... Then are workarounds from the good old Classic days.
We should really not have to do this in a new product.
..Thomas
"Jacob Grass" <JGrass@AbilitiSolutions.com> wrote in message
news:3cab1853$1@10.1.10.29...
> The issue is that anything displayed by the designer (e.g. textbox, label,
> etc) is instantiated. The special cases are items which you are actually
> designing (e.g. Form, UserControl). Because it isn't safe to instantiate
> the item which you are designing, it instantiates the Base Class. So, if
> you are desgining Form1, and it derives from Form, then it instantiates
> Form, instead of Form1. Since your base class is Abstract, it can't
> instantiate it.
Re: Visual Inheritance - Very Temperamental