Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I close one form from another form? 1

Status
Not open for further replies.

Goodall

MIS
Aug 4, 2004
18
US
I am running VB .NET 2003 Standard. I have a project that has several forms and I need to move from form to form in a natural flow. When I want to open a new form I use the code:

Dim oForm As frmNewForm1
oForm = New frmNewForm1
oForm.Show()
oForm = Nothing

When that is executed the new form (frmNewForm1) will be shown and there will be two open forms. If I want to open another form I go through the same routine for frmNewForm2, then there will be three open forms.

The only way I know how to close any of those forms is through the me.close method while I am "in" a form.

My question is, "How can I close a form from another form?"

Thanks.

 
There may be a better way but try:

create a module as below and change the application start to sub main()

Module MyModule
Public MyForm1 As New Form1()
Public MyForm2 As New Form2()
Public MyForm3 As New Form3()
Public MyForm4 As New Form4()

Sub main()
MyForm1.ShowDialog()
End Sub
End Module

Then you can access any of the forms for opening and closing from anywhere within your project as below

MyForm2.show()
MyForm1.close()
 
Shatch,

Thanks for the suggestion. It helped me to achieve my objective and I have a few observations as well.

1) By declaring the form objects up front in the module the application will carry the overhead of those form objects throughout the life of the application. This may affect performance if there are many forms.
2) If I eventually close a form and want to show it again I have to recreate that object instance. I wasn't sure how to do recreate the object again on a public basis like you suggested in the beginning of the module. Thus, I play a cat and mouse game by showing and hiding the forms I want the user to see.
3) When I used the MyForm1.ShowDialog code for the initial form I was unable to hide that form subsequently. When I ran the code

myForm2.Show()
myForm1.Hide()

the application treated the .hide like a .close and ended the application. So, I tried to change the myForm1.visible property to false and got the same "closing" result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top