How do I show a visually inherited form?

R

rsine

I just started working with inherited forms and am being befuddled with
how to show the inherited form. As a test, I designed Form1 with a
single button on it and added an inherited form that inherits from
Form1. When my application starts, I would like to have both forms
show at the same time. How can this be done? I tried placing the
following code in the Form1_Load event:

Dim form2 As New InheritedForm
form2.Show()

but this gives me the following error:

An unhandled exception of type 'System.ComponentModel.Win32Exception'
occurred in system.windows.forms.dll

Additional information: Error creating window handle.

What am I doing incorrectly that is generating this error?

-Thanks
 
C

Cerebrus

Hi Rsine,

Double click on your child form and then look at the code that is
generated :
------------------------------------------------------------------------------------------
Private Sub ChildForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
^^^^^^^^^^^^^^^^

End Sub
------------------------------------------------------------------------------------------
The underlined part indicates that the Load event of this inherited
form will actually handle the Load event of it's Parent class (which is
the Parent Form). Seems to me the reason why you're getting that
exception. (Error creating window handle)

When I tried out this experiment, I experienced some very wierd
behaviour, like hundreds of forms popping up. (even before running the
project !) (Rolling eyes...)

My suggestion is to load the Child form in some other procedure, or as
a response to user interaction, not automatically. Or, I hope someone
else comes up with a better way.

HTH,

Regards,

Cerebrus.
 
R

rsine

Cerebrus,

I did not notice on my initial test but when I opened the project
again, I got 41 forms popping up! I wonder why?

To clarify your one point, the inherited form also inherits the load
event of the parent form? So, whenever the inherited form loads, it
will execute the same form load code of the parent form? If this is
the case, which it seems to be from a test I ran, is my error stemming
from the fact that the handle already exists and thus cannot be created
again when the inherited form is trying to load? I am really
struggling with inheritance and am trying to get a good understanding
of what is going on. So, any help you can offer is appreciated.

-Thanks
 
R

RYoung

Private Sub ChildForm_Load(ByVal sender As System.Object, ByVal e As
Take that line of code out and add this:

Protected Overrides Sub OnLoad(ByVal e As EventArgs)
' MyBase.OnLoad(e)
End Sub

Note the call to MyBase.OnLoad is commented out to prevent the invalid
handle issue. To prevent 40 instances of VS.NET popping up, add this to
Form1_Load:

Private Sub Form1_Load(...)
If Not DesignMode Then
Dim frm As New Form2()
frm.Show()
End If
End Sub
 
R

RYoung

Anyways, theres somthing cicular going on there.

If you comment out this:
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
' MyBase.OnLoad(e)
End Sub

and put a breakpoint on Form1_Load, you'll see that method is called
repeatedly - hover the (sender As Object) parameter and you'll see the first
call is from Form1, the subsequent calls are from Form2. Overriding the
OnLoad method in Form2 and not calling MyBase.Load from it will prevent that
series of calls to the base form.

Ron
 
C

Cerebrus

RYoung and RSine,

I think your point is valid and should work. I haven't had the chance
to test it out yet, so I hope RSine will try it out and let us know if
it works.

As for the explanation as to why this happens :

1. 41 forms popping up even when in Design mode. (Is the Load event
triggered even in Design mode ??? I was under the impression that only
the Paint method is called.)
2. Why is the Load event of the Inherited Form called repeatedly ?

I leave these questions to the more experienced among us... ;-)

Regards,

Cerebrus.
 

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