Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created two forms

1. form1
2. form2

i made form1 's ismdicontainer is true
i created a button in form1 and in that button i wrote
following codes to open form2

dim x as new form2
x.showDialog()

In form2 I have designed a calculator and i have put a new button
in that button (new calculation) i have written to create a new instance of the calculator
as follows

dim x as new form2
x.showDialog()

it is working fine but it is not closing the old instance.
if i do it more than 10 or 15 times it shows overload error.
what i want is that if i click on new calculation button it should close the old instance and start the new instance

What I have tried:

i have tried following codes but it is not closing old instance

me.close()
dim x as new form2
x.showDialog()

also i have tried following codes still it is not closing the old instance


dim x as new form2
x.showDialog()
me.close()
Posted
Updated 30-Nov-20 16:26pm

You cannot do what you want. Windows are opened in a hierarchy, not a chain.

The first form shown is the top of the window tree. Closing it closes your entire application.

The parent window can HIDE itself. It cannot close itself if there are child windows.
GrandParent Window
        |
        |
  Parent Window
        |
        |
   Child Window


If you want the new child window to replace the parent. The parent window must not create the child window. The GRANDPARENT window should create the new window to replace the parent window.
       GrandParent Window
               |
     ----------+------------------
     |                           |
Parent Window          New Parent Window


Oh! And any form you show with ShowDialog() MUST have Dispose() called on it before its reference goes out of scope.
Dim x As New Form2
result = x.ShowDialog()
x.Dispose()
 
Share this answer
 
v2
Comments
Maciej Los 15-Jan-18 11:59am    
5ed!
insted of me.close()
try
Me.Hide()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900