I am using Visual Studio 2010

I have multiple forms and when I hit close button(red X, top-right) from any form, i want the project to close entirely.

Now when I do this, this does not happen by default. I always have to click on the 'stop' button in VS for it to close.

Any quick solution or a piece of code to be inserted in every form will do.

Thank you :)

Sorry I forgot to mention that If i click on close in the 'startup form', then it closes just fine.
My problem is only about the forms that will open 2nd and later.

See if this helps to terminate the application from any Form.

Public Class Form1
#Region "----===----===----===----- EVENT HANDLERS -----===----===----===----"
    Private Sub _myCoolForms_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
        Application.Exit()
    End Sub
#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each frm In New Form() {Me, Form2, Form3} '// your Forms.
            '// add an Event Handler to all Forms for FormClosing.
            AddHandler frm.FormClosing, AddressOf _myCoolForms_FormClosing
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Show()
        Form3.Show()
    End Sub
End Class

Hey Codeorder thanks a lot for helping me.
The FormClosing in your code reminded me of form events, i am now entering the 'end' statement in each forms 'FormClosing' event.

You solution looks pretty good but i couldn't get line no. 11, sorry i am not familiar with handlers

anyways thx a lot :)

>>...i am now entering the 'end' statement in each forms 'FormClosing' event.
By using End , you will not close the application properly and it will skip the FormClosing event entirely. Using Application.Exit closes the app. normally.

>>...i couldn't get line no. 11...
Are you not understanding how it works or does it not respond as needed?

Edited 5 Years Ago by codeorder: n/a

I've never learnt about handlers so i didn't try the above code

And i've replaced all my 'End' with 'Application.Exit()'

Thx :)

Edited 5 Years Ago by lmali92: n/a

The code I posted takes care of "ALL" Forms from Form1. You just have to add/remove Forms from For Each frm In New Form() [B]{Me, Form2, Form3}[/B] and done. :) To get a better understanding about "Handlers" and adding Event Handlers to Controls/etc., see if this helps.
New Project, 1 Button
Double click Button1 and you should get this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub

Remove the Handles Button1.Click from the end and you can even .Rename Button1_Click to something more appealing to your project.
This Not only works for a Button.Click, it works for all Events, for all Controls.:)

Private Sub _myCoolBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

Now that you have the Handler for your Button.Click, it just has nothing to Handle since we .Removed the Handles Button1.Click , thus we have to assign a Control/etc. to Handle.

Placing this in Form1_Load or anywhere, you can add a Handler to your Button1.

AddHandler Button1.Click, AddressOf _myCoolBtn_Click

Adding some sort of code to your new Event.Handler, will access that code if done properly.

Private Sub _myCoolBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        With CType(sender, Button)
            MsgBox(.Name & " - " & .Text)
        End With
    End Sub

Here is the entire New Project's code w/1 Button on Form1.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler Button1.Click, AddressOf _myCoolBtn_Click
    End Sub

    Private Sub _myCoolBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        With CType(sender, Button)
            MsgBox(.Name & " - " & .Text)
        End With
    End Sub
End Class

>>Thx...
Glad I could help.:)

Edited 5 Years Ago by codeorder: n/a

This question has already been answered. Start a new discussion instead.