12 Replies - 730 Views - Last Post: 31 January 2013 - 09:35 AM Rate Topic: -----

#1 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Form1 Not closing after exit button has been clicked on form2

Posted 28 January 2013 - 07:51 AM

Hi all,
On Form2 I have
 Me.close()
coded for an exit button.
When I click on the exit button on form 2, form 1(main) stays open.
What can I do to not have this happening??
Is This A Good Question/Topic? 0
  • +

Replies To: Form1 Not closing after exit button has been clicked on form2

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8957
  • View blog
  • Posts: 33,584
  • Joined: 12-June 08

Re: Form1 Not closing after exit button has been clicked on form2

Posted 28 January 2013 - 08:05 AM

Why would you expect form1 to close when you are telling form2 that "*ME* (aka form2) please CLOSE()"? You would need to shoot an event or call a method in form1 to notify it you want that to close as well.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3248
  • View blog
  • Posts: 10,894
  • Joined: 12-December 12

Re: Form1 Not closing after exit button has been clicked on form2

Posted 28 January 2013 - 08:23 AM

Presumably there is a button, or other control, on the first form that opens the second?

When we open the second form we should subscribe to the FormClosed event of the 2nd form, so that when it is closed the first one will follow suit.

    Private Sub doThis()
        Me.Close()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim frm2 As New Form2
        AddHandler frm2.FormClosed, Sub() doThis()
        frm2.Show()
    End Sub


This is working but it isn't the full syntax. Someone will probably confirm this (or me in a while).

This post has been edited by andrewsw: 28 January 2013 - 08:24 AM

Was This Post Helpful? 1
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,413
  • Joined: 29-May 08

Re: Form1 Not closing after exit button has been clicked on form2

Posted 28 January 2013 - 08:34 AM

Me. refers to the this particular instance of a class or structure.

Since use of the class name eg Form1. indicates you are wanting ti access a shared method, or you referring to the default Form instance (bad practice).

This post has been edited by AdamSpeight2008: 28 January 2013 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3248
  • View blog
  • Posts: 10,894
  • Joined: 12-December 12

Re: Form1 Not closing after exit button has been clicked on form2

Posted 28 January 2013 - 08:42 AM

@Adam your sentences are unclear. Can you rephrase.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3248
  • View blog
  • Posts: 10,894
  • Joined: 12-December 12

Re: Form1 Not closing after exit button has been clicked on form2

Posted 28 January 2013 - 09:29 AM

The more formal way would be to create a class variable (field) - it cannot be within a Sub:

    Private WithEvents frm2 As Form2


In this same (Form1) module create:

    Private Sub Form2_Closed(sender As Object, e As Windows.Forms.FormClosedEventArgs) Handles frm2.FormClosed
        Me.Close()
    End Sub

then you can just instantiate Form2 and the above code will run automatically:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        frm2 = New Form2
        frm2.Show()

AddHandler could be used as outlined above to dynamically attach, and remove, event handlers.

This post has been edited by andrewsw: 28 January 2013 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#7 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Re: Form1 Not closing after exit button has been clicked on form2

Posted 28 January 2013 - 10:27 AM

View Postandrewsw, on 28 January 2013 - 09:29 AM, said:

The more formal way would be to create a class variable (field) - it cannot be within a Sub:

    Private WithEvents frm2 As Form2


In this same (Form1) module create:

    Private Sub Form2_Closed(sender As Object, e As Windows.Forms.FormClosedEventArgs) Handles frm2.FormClosed
        Me.Close()
    End Sub

then you can just instantiate Form2 and the above code will run automatically:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        frm2 = New Form2
        frm2.Show()

AddHandler could be used as outlined above to dynamically attach, and remove, event handlers.


Your code opens form2 at the end of the day doesnt it?
I want the EXIT menu item to exit the program completely. Like for example on VB2010, when you want to exit the program, because you are done working on it. I want that to happen to my program here...?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3248
  • View blog
  • Posts: 10,894
  • Joined: 12-December 12

Re: Form1 Not closing after exit button has been clicked on form2

Posted 28 January 2013 - 10:33 AM

How is Form2 opened if it isn't from Form1?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3248
  • View blog
  • Posts: 10,894
  • Joined: 12-December 12

Re: Form1 Not closing after exit button has been clicked on form2

Posted 28 January 2013 - 10:39 AM

Well, if you want to cheat :)

    Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed

        Application.Exit()
        ' or....

        For Each frm As Form In Application.OpenForms
            frm.Close()
        Next
    End Sub

Was This Post Helpful? 0
  • +
  • -

#10 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Re: Form1 Not closing after exit button has been clicked on form2

Posted 29 January 2013 - 07:25 AM

View Postandrewsw, on 28 January 2013 - 10:39 AM, said:

Well, if you want to cheat :)/>

    Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed

        Application.Exit()
        ' or....

        For Each frm As Form In Application.OpenForms
            frm.Close()
        Next
    End Sub


Do I just put in that code in for the exit menustrip clicked or not?
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3248
  • View blog
  • Posts: 10,894
  • Joined: 12-December 12

Re: Form1 Not closing after exit button has been clicked on form2

Posted 29 January 2013 - 07:36 AM

You can put the code wherever you want(?). Yes, in the menustrip, Exit click-event.

BTW You only need one of the two code snippets - they are (or..) alternatives.
Was This Post Helpful? 0
  • +
  • -

#12 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Re: Form1 Not closing after exit button has been clicked on form2

Posted 31 January 2013 - 09:28 AM

MafiaAndre,

Just put this code on Exit Button_Click event or Form2_closing Events
Application.exit()


MafiaAndre,

Just put this code on Exit Button_Click event or Form2_closing Events
Application.exit()


@Nilesh,,,
Was This Post Helpful? 0
  • +
  • -

#13 dacostahugo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 31-January 13

Re: Form1 Not closing after exit button has been clicked on form2

Posted 31 January 2013 - 09:35 AM

Why Don't you try to hide it instead of closing. Much simpler and it works
Me.Hide
Form2.Show


Hope it helped!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1