ControlBox

How would I execute code when the user clicks the controlbox (little x in the upper-right corner). for an example, if i wanted to display a form that prompts im the user really wants to exit when the x is pressed; or if i wanted to end the program instead of just closing the form when its pressed

Answer this question

ControlBox

  • Etienne VSTS MVP

    Putting the following in the form closing event should give you what you need.

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

     If MsgBox("Are you sure you want to close ", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
      e.Cancel = True
     End If

    End Sub

    of course you can do anything you want such as displaying  a modal dialog form but ultimately if you want to abort the form close then the line e.Cancel = True will cancel the form close


  • ControlBox