Creating custom form control (VB)

Omid Golban

Member
Joined
Jun 3, 2003
Messages
6
Creating custom form control

Im trying to create a custom form (cEzForm) as a new custom control. Ive added a status bar and a main menu to this custom control. I then have a form (Form1) that inherits this custom form. When I try to go to the design mode of Form1, I get the following error:
An exception occured while trying to create an instance of EzFormControls.cEzForm.
The exception was "Object reference not set to an instance of an object."

So I went back to my customized forms code and put a bunch of msgbox() statements to find the line that dies & catch the exception, but Im still not sure whats happening. My customized forms New() gets called:
Public Sub New(ByRef gGlobals As EzClasses.cGlobals)
MyBase.New()
This call is required by the Windows Form Designer.
InitializeComponent()
....
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
MsgBox("cEzForm: Inside InitializeComponent()")
Me.StatusBar1 = New System.Windows.Forms.StatusBar()
menu item is instantiated here
Me.mnuEzMain = New System.Windows.Forms.MainMenu()
....
Last line that executes properly.
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.StatusBar1})
The following message box is the last thing that shows up
MsgBox("cEzForm: Me.Menu -- Now its going to die")
Me.Menu = Me.mnuEzMain =====> It dies here <=====
The following line blows up:
MsgBox("cEzForm: Me.Name")


System.NullReferenceException: Object reference not set to an instance of an object.
at EzFormControls.cEzForm.cEzForm_Resize(Object sender, EventArgs e) in C:\Omid\VB .NET\Ezware\EzFormControls\cEzForm.vb:line 203
at System.Windows.Forms.Control.OnResize(EventArgs e)
at System.Windows.Forms.Form.OnResize(EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height)
at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Form.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.SetBounds(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.set_Size(Size value)
at System.Windows.Forms.Control.SetClientSizeCore(Int32 x, Int32 y)
at System.Windows.Forms.Form.SetClientSizeCore(Int32 x, Int32 y)
at System.Windows.Forms.Control.set_ClientSize(Size value)
at System.Windows.Forms.Form.set_ClientSize(Size value)
at System.Windows.Forms.Form.set_Menu(MainMenu value)
at EzFormControls.cEzForm.InitializeComponent() in C:\Omid\VB .NET\Ezware\EzFormControls\cEzForm.vb:line 169


If the statement Me.StatusBar1 = New System.Windows.Forms.StatusBar() gets executed properly towards the beginning of this sub, then isnt the form control (Me) instantiated as well as the main menu (mnuEzmain)? So if both of these are instantiated, what is the problem?

Thank you for your help
 
As far as I know, you cant make a Form inherit from a UserControl.
Try designing the form as a Form object, and then making the second
form inherit from it.
 
Back
Top