Hi,
I want to have a dialog result to appear when i click on the form's cancel button. It will ask "Do you want to quit?" when the cancel button is clicked. How do i go about doing that?

Dani AI

Generated

Short version for : the approach depends on whether you’re building a WinForms app, a WPF window, or a web page. ’ InputBox is for text input (not a confirmation), and ’s JavaScript tip is only for web apps. was right to suggest asking the user first — but the sample mixes WinForms and WPF patterns (WinForms uses the DialogResult enum; WPF’s Window.DialogResult is a Nullable(Of Boolean)). Pick the API that matches your project.

A robust, recommended WinForms pattern is to centralize confirmation in the FormClosing event so every close path (X button, Alt+F4, Cancel button, etc.) is handled the same way:

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    If e.CloseReason = CloseReason.UserClosing Then
        Dim answer As DialogResult = MessageBox.Show("Do you want to quit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If answer = DialogResult.No Then
            e.Cancel = True
        End If
    End If
End Sub

If you prefer handling only the Cancel button, don’t set that button’s DialogResult in the designer (or clear it), and confirm in the Click handler. For a modal dialog use DialogResult; for the main app form call Close or Application.Exit:

Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
    Dim r As DialogResult = MessageBox.Show("Do you want to quit?", "Confirm", MessageBoxButtons.YesNo)
    If r = DialogResult.Yes Then
        ' If this form was shown with ShowDialog(), set a dialog result:
        Me.DialogResult = DialogResult.Cancel
        ' Otherwise, to close the application use:
        ' Application.Exit()
    End If
End Sub

For WPF, use MessageBoxResult and set Window.DialogResult (true/false) when shown with ShowDialog(). Final notes: if a Button has DialogResult/IsCancel set in the designer it may close the window automatically, so remove that if you need an interactive confirmation. Avoid InputBox for confirms, and use client-side confirm only for web pages.

Recommended Answers

All 3 Replies

Look into the inputbox function and write the code accordingly. :)

Get the user's choice from a message box and only set the dialog's DialogResult property to true if they click 'yes'. :)

Private Sub ButtonCancel_Click( ByVal sender As Object, ByVal e As EventArgs ) _
  Handles ButtonCancel.Click

  ' Verify the cancel
  Dim result As DialogResult = MessageBox.Show( "Do you want to quit?", "Confirm Operation", MessageBoxButtons.YesNo )

  If result = DialogResult.Yes Then
    Me.DialogResult = True
  End If
End Sub

use this javascript when ur trying web-applications(vb.net,vs-2003 or more..)

btndelete.Attributes.Add("onclick", "if(confirm('Are you sure ,want to delete the record?')){}else{me.server.transfer ('" & "webform1.aspx" & "') } ")

happy coding....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.