Results 1 to 6 of 6

Thread: How to code the close button in the form?

  1. #1
    annir is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Sep 2008
    Posts
    32
    Reputation
    77

    How to code the close button in the form?

    Hi Guys,

    Can I ask if someone have an idea on how can I write a code in the close button in the form.

    Example, if the user click the close button (the red box with X) then a message box will appear if he clicks yes then it will end, if cancel the form will not close.

    Thanks!

    annir

  2. #2
    jacky.goy's Avatar
    jacky.goy is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2006
    Posts
    35
    Reputation
    101
    put this script to your close button (when you click on the close button in your form)

    Code:
     
    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
    Dim response As MsgBoxResult
    response = MsgBox("Do you want to close form?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
    If response = MsgBoxResult.Yes Then
    Me.Dispose()
    ElseIf response = MsgBoxResult.No Then
    Exit Sub
    End If
    End Sub
    put this script to your form closing (when you click on the X button on top)
    Code:
     
    Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim response As MsgBoxResult
    response = MsgBox("Do you want to close?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
    If response = MsgBoxResult.Yes Then
    Me.Dispose()
    ElseIf response = MsgBoxResult.No Then
    e.Cancel = True
    Exit Sub
    End If
    End Sub
    hope this helps you.
    -------------------------------------------------
    Best Regards,
    Jacky Goy

  3. #3
    annir is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Sep 2008
    Posts
    32
    Reputation
    77
    Hi,

    Thank you very much!

    annir

  4. #4
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    977
    Quote Originally Posted by jacky.goy View Post
    put this script to your close button (when you click on the close button in your form)

    Code:
     
    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
    Dim response As MsgBoxResult
    response = MsgBox("Do you want to close form?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
    If response = MsgBoxResult.Yes Then
    Me.Dispose()
    ElseIf response = MsgBoxResult.No Then
    Exit Sub
    End If
    End Sub
    put this script to your form closing (when you click on the X button on top)
    Code:
     
    Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim response As MsgBoxResult
    response = MsgBox("Do you want to close?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
    If response = MsgBoxResult.Yes Then
    Me.Dispose()
    ElseIf response = MsgBoxResult.No Then
    e.Cancel = True
    Exit Sub
    End If
    End Sub
    hope this helps you.
    Wow, just look at all of that un-needed redundancy...

    To close the form:
    Code:
     
    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Me.Close()
    End Sub
    For confirmation:
    Code:
     
    Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = (MessageBox.Show("Confirm close?", "Closing Window", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel)
    End Sub
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  5. #5
    geomatrix is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Oct 2009
    Posts
    1
    Reputation
    0

    Smile Hi

    Quote Originally Posted by annir View Post
    Hi Guys,

    Can I ask if someone have an idea on how can I write a code in the close button in the form.

    Example, if the user click the close button (the red box with X) then a message box will appear if he clicks yes then it will end, if cancel the form will not close.

    Thanks!

    annir

    This code i use in vb 2008
    '================================================= ==
    Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim response As MsgBoxResult
    response = MsgBox("Are you sure you want to close the application?", vbYesNo + vbExclamation + vbApplicationModal + vbDefaultButton2, "Close confirmation")
    If response = MsgBoxResult.Yes Then
    Me.Close()
    ElseIf response = MsgBoxResult.No Then
    e.Cancel = True
    Exit Sub
    End If
    End Sub
    '========================================

  6. #6
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    977
    Quote Originally Posted by geomatrix View Post

    This code i use in vb 2008
    '================================================= ==
    Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim response As MsgBoxResult
    response = MsgBox("Are you sure you want to close the application?", vbYesNo + vbExclamation + vbApplicationModal + vbDefaultButton2, "Close confirmation")
    If response = MsgBoxResult.Yes Then
    Me.Close()
    ElseIf response = MsgBoxResult.No Then
    e.Cancel = True
    Exit Sub
    End If
    End Sub
    '========================================
    Here's something a little cleaner:
    Code:
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            e.Cancel = MessageBox.Show("Are you sure you want to close the application?", "Close confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) <> Windows.Forms.DialogResult.OK
        End Sub
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking