close open form from another open form-VBForums
Results 1 to 8 of 8

Thread: close open form from another open form

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2017
    Location
    Ohio
    Posts
    35

    close open form from another open form

    is it possible to close a form from another form? if so how?

    both forms are on the screen.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,528

    Re: close open form from another open form

    Assuming the form you want to close is called Form1, you can do this:
    Code:
    Form1.Close()
    If this doesn't work for you then it is probably because you aren't using the default instance of the form, so we'll need info about how you are opening the forms.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2017
    Location
    Ohio
    Posts
    35

    Re: close open form from another open form

    Quote Originally Posted by si_the_geek View Post
    Assuming the form you want to close is called Form1, you can do this:
    Code:
    Form1.Close()
    If this doesn't work for you then it is probably because you aren't using the default instance of the form, so we'll need info about how you are opening the forms.
    form1.show() is how it is opened


    when I type form1.close it doesn't give me the option to close in the list.

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,464

    Re: close open form from another open form

    What is the list you are talking about?

    Use FormClosing if you want to cancel the form closing

    Code:
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            ' select another reason or don't use any, it is depend in what the form do. 
            If e.CloseReason = CloseReason.UserClosing Then
                Dim a As DialogResult = MessageBox.Show(Me, "Do you want to save changes?", My.Application.Info.Title, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                Select Case a
                    Case DialogResult.Yes
                        ' Save the changes
                        MessageBox.Show(Me, "TO DO: Call Save changes method", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Case DialogResult.No
                        ' Ignore the changes
                        MessageBox.Show(Me, "TO DO: no action required, continue closing the form", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Case DialogResult.Cancel
                        e.Cancel = True ' don't close the form
                End Select
            End If
        End Sub



  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2017
    Location
    Ohio
    Posts
    35

    Re: close open form from another open form

    Quote Originally Posted by 4x2y View Post
    What is the list you are talking about?

    Use FormClosing if you want to cancel the form closing

    Code:
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            ' select another reason or don't use any, it is depend in what the form do. 
            If e.CloseReason = CloseReason.UserClosing Then
                Dim a As DialogResult = MessageBox.Show(Me, "Do you want to save changes?", My.Application.Info.Title, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                Select Case a
                    Case DialogResult.Yes
                        ' Save the changes
                        MessageBox.Show(Me, "TO DO: Call Save changes method", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Case DialogResult.No
                        ' Ignore the changes
                        MessageBox.Show(Me, "TO DO: no action required, continue closing the form", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Case DialogResult.Cancel
                        e.Cancel = True ' don't close the form
                End Select
            End If
        End Sub


    this is not what I am talking about.

    I have two forms open and I want to close form1 from form2

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,337

    Re: close open form from another open form

    Are you saying that .Close is not an option in intellisense? If you just type Form1.Close, does that give you an error message, and if so, what is it?
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2017
    Location
    Ohio
    Posts
    35

    Re: close open form from another open form

    Quote Originally Posted by Shaggy Hiker View Post
    Are you saying that .Close is not an option in intellisense? If you just type Form1.Close, does that give you an error message, and if so, what is it?
    yes, not an option with Intellisense.

    no error messages

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,337

    Re: close open form from another open form

    Intellisense might just be getting confused, in that case. Form1 is a type AND an instance of a type. That confuses the heck out of coders, so why not have it confuse intellisense? As long as you are not getting any errors with the line, then give it a try. If it works, then it works.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.