Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2006

Tip: Looking for answers? Try searching our database.

How do I show a visually inherited form?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rsine - 26 Feb 2006 17:21 GMT
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
Cerebrus - 26 Feb 2006 18:31 GMT
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.
rsine - 26 Feb 2006 19:05 GMT
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

> Hi Rsine,
>
[quoted text clipped - 25 lines]
>
> Cerebrus.
RYoung - 26 Feb 2006 21:03 GMT
>> Private Sub ChildForm_Load(ByVal sender As System.Object, ByVal e As
>> System.EventArgs) Handles MyBase.Load
>>                                           ^^^^^^^^^^^^^^^^
>>
>> End Sub

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

> Cerebrus,

> To clarify your one point, the inherited form also inherits the load
> event of the parent form?  So, whenever the inherited form loads, it
[quoted text clipped - 36 lines]
>>
>> Cerebrus.
RYoung - 26 Feb 2006 22:26 GMT
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

>>> Private Sub ChildForm_Load(ByVal sender As System.Object, ByVal e As
>>> System.EventArgs) Handles MyBase.Load
[quoted text clipped - 61 lines]
>>>
>>> Cerebrus.
Cerebrus - 27 Feb 2006 06:48 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.