Actually you would handle the Closing event, or FormClosing in VB 2005 (please use the radio buttons provided to specify your IDE/Framework version when creating a thread), and Cancel the action if they choose not to leave.
VB Code:
Private Sub Form1_Closing(...) Handles MyBase.Closing
If MessageBox.Show("Are you sure you want to exit?", _
"Vejeel Soft", _
MessageBoxButtons.OKCancel, _
MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
End Sub
Also, it is a subtle difference that many people seem not to notice but you should be using OK and Cancel on your buttons and not Yes and No.
(not meant to be taken the wrong way) Why is that?
Not taken the wrong way at all.
Throughout the Windows interface, when the user requests an action and a confirmation dialogue box is displayed, the OK, Cancel, Yes and No buttons have specific meanings. OK means "do what I asked", Cancel means "don't do what I asked", Yes means "do what I asked plus something extra", No means "do what I asked but nothing extra". Take note of the dialogues that Microsoft products display and you'll see that whenever you are asked a question like "Are you sure you want to..." the buttons on the dialogue will be OK and Cancel. An appropriate use of Yes and No would be if the question was "Do you want to save your changes before exiting?" where Yes would mean save and close (do what I asked plus something extra) and No means close without saving (do what I asked but nothing extra). The fact that No and Cancel mean different things is evidenced by the fact that there is a Yes, No, Cancel button combination. You could use it in the previous example where Cancel would mean don't close (don't do what I asked). That demonstrates the difference between No and Cancel very clearly.
Of course, there are other situations where Yes and No are appropriate, like if you were asking "Do you live in the United States?". I'm talking specifically about when the user requests an action and your message box is confirming that action.
Doing it yourself is the best way to learn. There are two ways to add event handlers in VB 2005. You can open the code file and select the variable for the drop down list at the top-left and the event from the top-right. Alternatively you can select the object in the designer, go to the Properties window and press the Events button (lightning bolt) and then double-click the event you want to add a handler for. Either way you would select the FormClosing event for the form itself and then paste the contents of the method I posted previously into your event handler.