Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey guys,

I am opening Form2 when a button is clicked on Form1. After the user finishes entering some data in Form2, on clicking a button I want to close both Form2 and Form1. I could close Form2 that was very easy. But I could not close Form1. I tried using 'this.ParentForm.Close();' on the button click event on Form2. But that doesn't work. Any ideas on how to solve this?

-Unbreakable.

Posted
If Form1 is the startup form and you want to close everything then use End. If it's not then from Form 2 use Form1.Dispose.

How and where do I use 'end'?. Also I tried using Form1.Dispose from Form2 but there is no Dispose property for Form1. Please explain a little more in detail if possible.

Posted

Well the way i handle this is to create a public variable in form1:

 

'I usually put these after the Windows Form Designer generated code 
'region but before any control events or other functions
Public Shared objForm1 As Form

 

In Form1's load event set the value to form 1:

 

objForm1 = Me

 

In form2 import form 1:

 

Import YOURPROJECTNAME.Form1

 

Then when you want to close form1, and then close form 2 after, use:

 

objForm1.Dispose()
Me.close()

Visit: VBSourceSeek - The VB.NET sourcecode library

 

 

"A mere friend will agree with you, but a real friend will argue."
  • *Gurus*
Posted
End is, however, not normally recommended as a good way of closing your application as it's good practice to clean up resources you are using explicitly. Closing the form your application is relying on for its message loop is generally a good idea instead.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • 2 weeks later...
Posted

Look into Events

 

Define an Event in Form2, e.g. Event CloseMe; use RaiseEvent CloseMe in Form2 when you are ready for Form1 to take control.

 

In Form1, Dim withevents _Form2 as new Form2

 

In Form1, establish a procedure to respond to _Form2.CloseMe; in that procedure, you can _Form2.close and then Me.Close.

 

That way, you are no required to use .ShowDialog.

 

Events are very handy; I wish I had I discovered them earlier in my VB.Net career.

 

 

 

 

 

Hey guys,

I am opening Form2 when a button is clicked on Form1. After the user finishes entering some data in Form2, on clicking a button I want to close both Form2 and Form1. I could close Form2 that was very easy. But I could not close Form1. I tried using 'this.ParentForm.Close();' on the button click event on Form2. But that doesn't work. Any ideas on how to solve this?

-Unbreakable.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...