Share via


In Visual Basic 2010, How Can I write code for the close button at the top Right of the form

Question

Sunday, April 10, 2011 10:52 PM

Hi Everyone,

I have a form in Visual Basic 2010 in Design view, and at the top of the form there is a close button along with the minimize and Maximize Buttons. is what I require is when that close button is pressed/clicked I want to close the application

Can anyone help

Kind Regards

Gary

Gary Simpson

All replies (4)

Sunday, April 10, 2011 11:32 PM ✅Answered | 1 vote

Hi Gary,

Try this:>>

Public Class Form1

  Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Dim result As DialogResult
    result = MessageBox.Show("Are you sure you wish to close the program?", "Close program?", MessageBoxButtons.YesNo)

    If result = Windows.Forms.DialogResult.Yes Then
      e.Cancel = False
    Else
      e.Cancel = True
    End If

  End Sub
End Class
Regards, John

Click this link to see how to insert a picture into a forum post.

Installing VB6 on Windows 7


Sunday, April 10, 2011 11:39 PM ✅Answered | 1 vote

Hi again Gary,

If you only have a single Form then the application will close anyway when it is run.

If you want to close the application from say Form2 use:>>

Public Class Form2

  Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Application.Exit()

  End Sub
End Class
Regards, John

Click this link to see how to insert a picture into a forum post.

Installing VB6 on Windows 7


Monday, April 11, 2011 12:03 AM

Thank's John you have helped me out once again,

I Know It will sink in but when I dont know

Best Regards

Gary

Gary Simpson


Monday, April 11, 2011 12:06 AM

Hi Gary,

Here is my little "BIG" tip:>>

Select the item from the top-left-combobox above your code window such as;

  • Button1
  • Form1 events

 

or whatever.

Then you can select the associated event that you want from the one on the right-hand-side.  :-)    ;-)    :-D

 

Tip 2:

Select the last item in the PROJECT menu.

In the window that appears you can change

Shutdown mode:

between

When startup Form closes

'or

When last Form closes

 

when you have the APPLICATION tab selected on the left hand side of the window,

SHUTDOWN MODE

is on there somewhere.  :-)

 

Regards, John

Click this link to see how to insert a picture into a forum post.

Installing VB6 on Windows 7