Save Dialog when User Cancel

Abbas1999

I need to save a file from different locations in my application, so I created a sub for that; everything work just fine, except when a user clicks cancel when the save dialog shows up; if user clicks "Cancel", the form will close; I tried the TWO Options shown in the code below but both did not work; any suggestion will be appreciated:

   Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    SaveFileDialog()
End Sub

Sub SaveFileDialog()
    SaveFileDialog1.Filter = "TXT Files (*.txt)|*.txt"
    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        'If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        Dim MekdamSaveFile = SaveFileDialog1.FileName
        System.IO.File.WriteAllText(MekdamSaveFile, "")
        My.Computer.FileSystem.WriteAllText(MekdamSaveFile, RichTextBox2.Text, True)
    End If
End Sub


Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    Dim result = MessageBox.Show("The File:  has been changed, do you want to save it? ", _
    "Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
    If result = DialogResult.Cancel Then
        e.Cancel = True
    ElseIf result = DialogResult.No Then
        'PROCEED...
    ElseIf result = DialogResult.Yes Then
        SaveFileDialog()
    End If
End Sub

End Class

Mark Hall

Make your SaveFileDialog Subroutine into a Function, then return False if anything other than OK was done, then test it in your FormClosing EventHandler and stop the Close.

Modified SaveFileDialog

Function SaveFileDialog() As Boolean
    SaveFileDialog1.Filter = "TXT Files (*.txt)|*.txt"
    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        'If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        Dim MekdamSaveFile = SaveFileDialog1.FileName
        System.IO.File.WriteAllText(MekdamSaveFile, "")
        My.Computer.FileSystem.WriteAllText(MekdamSaveFile, RichTextBox2.Text, True)
        Return True 'Return True if Ok is clicked
    Else
        Return False 'return false this will give you something to conditionaly test
    End If
End Function

Modified FormClosing EventHandler

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    Dim result = MessageBox.Show("The File:  has been changed, do you want to save it? ", _
"Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
    If result = DialogResult.Cancel Then
        e.Cancel = True
    ElseIf result = DialogResult.No Then
        'PROCEED...
    ElseIf result = DialogResult.Yes Then
        If Not SaveFileDialog() Then e.Cancel = True 'this will abort the close
    End If

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

override jqueryUI dialog buttons (save, cancel and etc to user's choice) text dynamically

From Dev

Select folder to save without macro when user cancels dialog box

From Dev

determine if user selects 'cancel' in dialog box

From Dev

Unexpected "}" when pressing 'Cancel' in a linux dialog script

From Dev

How to do something if cancel button on save file dialog was clicked?

From Dev

How To Detect User Cancel AccountPicker dialog Android Eclipse

From Dev

How to save user input from Alert Dialog?

From Dev

how to cancel a download when user clicked in android

From Dev

Prompting user to save file using a 'Save-as' dialog?

From Dev

Prompting user to save file using a 'Save-as' dialog?

From Dev

How to finish activity when date picker dialog on cancel button is clicked

From Dev

Cancel password reset in Laravel if $user->save() fails

From Dev

Why JS confirm() won't Cancel the submit action when I hit Cancel in confirm dialog?

From Dev

Dialog windows with Yes/No/Cancel

From Dev

Add a cancel button to a dialog

From Dev

Cancel saving model when using pre_save in django

From Dev

Get a callback when user dismisses an Intent dialog

From Dev

Get a callback when user dismisses an Intent dialog

From Dev

Dismiss Dialog when user is at top and scrolled down

From Dev

How do I prompt a user with file "Save As" dialog using a macro?

From Dev

How to cancel angularjs $timeout when user navigate away from the page

From Dev

Keeping a java application open when a user selects "Cancel"?

From Dev

How to exit MsgBox when user press's cancel

From Dev

Tkinter askopenfilename not returning as expected when user clicks "Cancel."

From Dev

Android: Dialog fragments and retaining the values selected the first time the dialog is opened when the user opens the dialog for a second time

From Dev

Populating filename in Save As window when using Electron Dialog module and .showSaveDialog

From Dev

iTextSharp: Display "Open/Save" dialog box when PDF file is created

From Dev

Photoshop always prompts me with a Save As dialog when saving

From Dev

How to Press OK on a save as dialog when downloading a PDF from a website

Related Related

  1. 1

    override jqueryUI dialog buttons (save, cancel and etc to user's choice) text dynamically

  2. 2

    Select folder to save without macro when user cancels dialog box

  3. 3

    determine if user selects 'cancel' in dialog box

  4. 4

    Unexpected "}" when pressing 'Cancel' in a linux dialog script

  5. 5

    How to do something if cancel button on save file dialog was clicked?

  6. 6

    How To Detect User Cancel AccountPicker dialog Android Eclipse

  7. 7

    How to save user input from Alert Dialog?

  8. 8

    how to cancel a download when user clicked in android

  9. 9

    Prompting user to save file using a 'Save-as' dialog?

  10. 10

    Prompting user to save file using a 'Save-as' dialog?

  11. 11

    How to finish activity when date picker dialog on cancel button is clicked

  12. 12

    Cancel password reset in Laravel if $user->save() fails

  13. 13

    Why JS confirm() won't Cancel the submit action when I hit Cancel in confirm dialog?

  14. 14

    Dialog windows with Yes/No/Cancel

  15. 15

    Add a cancel button to a dialog

  16. 16

    Cancel saving model when using pre_save in django

  17. 17

    Get a callback when user dismisses an Intent dialog

  18. 18

    Get a callback when user dismisses an Intent dialog

  19. 19

    Dismiss Dialog when user is at top and scrolled down

  20. 20

    How do I prompt a user with file "Save As" dialog using a macro?

  21. 21

    How to cancel angularjs $timeout when user navigate away from the page

  22. 22

    Keeping a java application open when a user selects "Cancel"?

  23. 23

    How to exit MsgBox when user press's cancel

  24. 24

    Tkinter askopenfilename not returning as expected when user clicks "Cancel."

  25. 25

    Android: Dialog fragments and retaining the values selected the first time the dialog is opened when the user opens the dialog for a second time

  26. 26

    Populating filename in Save As window when using Electron Dialog module and .showSaveDialog

  27. 27

    iTextSharp: Display "Open/Save" dialog box when PDF file is created

  28. 28

    Photoshop always prompts me with a Save As dialog when saving

  29. 29

    How to Press OK on a save as dialog when downloading a PDF from a website

HotTag

Archive