Results 1 to 8 of 8

Thread: Closing a form from another form

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    13

    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.

  2. #2
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    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:
    1. Public Class Form2
    2.     Inherits System.Windows.Forms.Form
    3.  
    4.     Private fOwner As Form
    5.  
    6.     Public Sub New(ownerForm As Form)
    7.         My Base.New
    8.         '
    9.         '
    10.         '
    11.         Me.fOwner = ownerForm
    12.     End Sub
    13.  
    14.     Private Sub CloseForm1()
    15.         Me.fOwner.Close
    16.  
    17.     End Sub
    18. End Class
    19.  
    20.  
    21.     'In form1 you have
    22.     Private Sub ShowForm2()
    23.         Dim f As Form2
    24.  
    25.         f = New Form2(Me)
    26.         f.Show()
    27.     End Sub

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963
    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.

  4. #4
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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:
    1. 'in the form1
    2.  
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim f As New Form2(Me)
    5.         f.Show()
    6. End Sub
    7.  
    8. 'in the form2
    9. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.    Me.f.Close()
    11. End Sub
    12. 'to the constructor of the form2
    13.  Dim f As Form
    14.     Public Sub New(ByVal f1 As Form1)
    15.         MyBase.New()
    16.  
    17.         'This call is required by the Windows Form Designer.
    18.         InitializeComponent()
    19.         Me.f1 = f
    20.         'Add any initialization after the InitializeComponent() call
    21.  
    22.     End Sub
    23.  
    24. 'to the module...make it as a startup object
    25. Sub main()
    26.         Dim f As New Form1()
    27.         f.Show()
    28.         Application.Run()
    29.     End Sub
    Last edited by mar_zim; Sep 24th, 2004 at 08:14 PM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    13
    Thanks for your suggestions. I'll give them a try.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963
    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    13
    No, I only wanted to close the first (calling form).

    I have since implemented the suggestion by Robymix and this works perfectly. Problem solved!

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963
    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
  •  



Click Here to Expand Forum to Full Width