-
Sep 24th, 2004, 12:47 PM
#1
Thread Starter
New Member
Closing a form from another form
I have a windows form (form1) that opens another form (form2). In certain circumstances I want form1 to be closed from form2. I have added the following code:
Dim FS As New form1()
FS.Close()
This is obviously not the way to do it as it doesn't close the form. Any help would be appreciated.
-
Sep 24th, 2004, 02:28 PM
#2
Addicted Member
Going by your code, Dim FS As New form1() creates another instance of Form1 which you're actually closing.
For you to close form1 from form2, you need to close the running instance of form1. To do this you can pass a reference of form1 to form2, then close it using this reference.
The code below should give you an idea:
VB Code:
Public Class Form2
Inherits System.Windows.Forms.Form
Private fOwner As Form
Public Sub New(ownerForm As Form)
My Base.New
'
'
'
Me.fOwner = ownerForm
End Sub
Private Sub CloseForm1()
Me.fOwner.Close
End Sub
End Class
'In form1 you have
Private Sub ShowForm2()
Dim f As Form2
f = New Form2(Me)
f.Show()
End Sub
-
Sep 24th, 2004, 03:03 PM
#3
PowerPoster
Hi Croydon,
Are you saying you want to close both form1 & form2 from form2 or do you want to keep form2 open?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Sep 24th, 2004, 08:11 PM
#4
Re: Closing a form from another form
Originally posted by Croydon
I have a windows form (form1) that opens another form (form2). In certain circumstances I want form1 to be closed from form2. I have added the following code:
Dim FS As New form1()
FS.Close()
This is obviously not the way to do it as it doesn't close the form. Any help would be appreciated.
VB Code:
'in the form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2(Me)
f.Show()
End Sub
'in the form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.f.Close()
End Sub
'to the constructor of the form2
Dim f As Form
Public Sub New(ByVal f1 As Form1)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
Me.f1 = f
'Add any initialization after the InitializeComponent() call
End Sub
'to the module...make it as a startup object
Sub main()
Dim f As New Form1()
f.Show()
Application.Run()
End Sub
Last edited by mar_zim; Sep 24th, 2004 at 08:14 PM.
-
Sep 25th, 2004, 06:07 AM
#5
Thread Starter
New Member
Thanks for your suggestions. I'll give them a try.
-
Sep 25th, 2004, 09:00 AM
#6
PowerPoster
Hi,
The code you have been shown will close both form1 and form2. If you use the Module Sub Main as shown, you will close down the project. If that is what you want to do then simply use
Application.Exit
anywhere you wish.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Sep 25th, 2004, 03:58 PM
#7
Thread Starter
New Member
No, I only wanted to close the first (calling form).
I have since implemented the suggestion by Robymix and this works perfectly. Problem solved!
-
Sep 25th, 2004, 06:36 PM
#8
PowerPoster
Originally posted by Croydon
No, I only wanted to close the first (calling form).
I have since implemented the suggestion by Robymix and this works perfectly. Problem solved!
Right.
So you must be using another form for your startup?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|