I want to inherit from a form. When I try this I find that I can add controls to the inheriting class easily through use of the Toolbox and that class's designer, and all looks OK. The code for the new controls appears in an InitializeComponent method for the inheriting class. But when I want to create an instance of the inheriting class at run time, nothing that I have done to its designer is apparent. It seems that the InitializeComponent sub of the inheriting class is not happening. An example might make this clearer:
Make sure that Form1 and Class1 are in different modules in the Explorer window so that the designer of each can be seen. If you look at Class1 in its designer you will see a checkbox and its title is "C1". But when you run the project it looks exactly like Form1.
So should I simply remove all the code from Class1's InitializeComponent method and put it into the Class1_Load event?
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Class1
f.Show()
End Sub
End Class
Code:
Public Class Class1
Inherits Form1
Private Sub InitializeComponent()
Me.CheckBox1 = New System.Windows.Forms.CheckBox
Me.SuspendLayout()
'
'CheckBox1
'
Me.CheckBox1.AutoSize = True
Me.CheckBox1.Location = New System.Drawing.Point(69, 173)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(81, 17)
Me.CheckBox1.TabIndex = 1
Me.CheckBox1.Text = "CheckBox1"
Me.CheckBox1.UseVisualStyleBackColor = True
'
'Class1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.CheckBox1)
Me.Name = "Class1"
Me.Text = "C1"
Me.Controls.SetChildIndex(Me.CheckBox1, 0)
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
End Class
Make sure that Form1 and Class1 are in different modules in the Explorer window so that the designer of each can be seen. If you look at Class1 in its designer you will see a checkbox and its title is "C1". But when you run the project it looks exactly like Form1.
So should I simply remove all the code from Class1's InitializeComponent method and put it into the Class1_Load event?