• We just launched and are currently in beta. Join us as we build and grow the community.

File Handling: Form Closing Event

love cool

Dynamic Player
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
64
Likes
95
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Tutorial is a continuation of our previous topic called “File Handling: Adding a Save As Command”. But this time we’re going to focus on how to use the form closing event in visual basic. Basically FormClosing event occurs when the user click the close button and the form is being closed. When the form is closed, it releases all the associated resources with the form. To start with this application lets open first our last project called “FileHandling”.

Then at the code view window, go to a class name and choose the (Form1 events) then FormClosing for Method name. And this looks like as shown below:

formclosing1.png


And inside the Form1_FormClosing sub procedure, add the following code:
  1. Dim

    Result As

    DialogResult
  2. 'if the current unsaved changes occur we will have to use this option
  3. If

    RichTextBox1.

    Text

    <>

    ""

    Then
  4. If

    savedoc =

    True

    Then
  5. Result =

    MessageBox.

    Show

    (

    "Would you like to save changes?"

    , "Confirm Exit"

    , MessageBoxButtons.

    YesNo

    , MessageBoxIcon.

    Question

    )
  6. If

    Result =

    Windows.

    Forms

    .

    DialogResult

    .

    Yes

    Then
  7. SaveFileDialog1.

    ShowDialog

    (

    )
  8. Dim

    myStreamWriter As

    System.

    IO

    .

    StreamWriter
  9. SaveFileDialog1.

    Filter

    =

    "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
  10. SaveFileDialog1.

    CheckPathExists

    =

    True
  11. SaveFileDialog1.

    Title

    =

    "Save File"
  12. SaveFileDialog1.

    ShowDialog

    (

    Me

    )
  13. Try
  14. myStreamWriter =

    System.

    IO

    .

    File

    .

    AppendText

    (

    SaveFileDialog1.

    FileName

    )
  15. myStreamWriter.

    Write

    (

    RichTextBox1.

    Text

    )
  16. myStreamWriter.

    Flush

    (

    )
  17. Catch

    ex As

    Exception
  18. MessageBox.

    Show

    (

    "There has been an error in the saving process"

    )
  19. End

    Try
  20. savedoc =

    True
  21. Application.

    Exit

    (

    )
  22. End

    If
  23. If

    Result =

    Windows.

    Forms

    .

    DialogResult

    .

    No

    Then
  24. Application.

    Exit

    (

    )
  25. End

    If
  26. Else

    : Application.

    Exit

    (

    )
  27. End

    If
  28. Else

    : Application.

    Exit

    (

    )
  29. End

    If

The code above will perform when the user click the close or the button with an X in the upper-right corner of the form causes to display a dialog box confirming the user to save the changes the current file. And it looks like as shown below:

formclosing2.png


When the user select “Yes” option, the program will process the saving same what we did on our last topic about “File Handling: Writing and Saving a Text File”. Else when the user click “No” it will terminate the application automatically.

And here’s all the code for this application:

  1. Public

    Class

    Form1
  2. 'set the savedoc variable value as true
  3. Dim

    savedoc As

    Boolean

    =

    True

  4. Private

    Sub

    menuOpen_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    menuOpen.

    Click

  5. 'declare open as new openFiledialog so that when the Open Sub menu is click,
  6. 'the OpenfileDialog will appear.
  7. Dim

    Open As

    New

    OpenFileDialog(

    )
  8. 'it is declare as System input and output Streamreader
  9. 'it reads characters from byte stream in a particular encoding
  10. Dim

    myStreamReader As

    System.

    IO

    .

    StreamReader
  11. 'in an open dialog box, it will give an opening filter for the current filenames,
  12. 'or the save file types.
  13. Open.

    Filter

    =

    "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
  14. 'it checks if the file is exist or not
  15. Open.

    CheckFileExists

    =

    True
  16. 'sets the openfile dialog name as "OpenFile"
  17. Open.

    Title

    =

    "OpenFile"
  18. Open.

    ShowDialog

    (

    Me

    )

  19. Try
  20. 'it opens the selected file by the user
  21. Open.

    OpenFile

    (

    )
  22. 'opens the existing file
  23. myStreamReader =

    System.

    IO

    .

    File

    .

    OpenText

    (

    Open.

    FileName

    )
  24. 'it reads the streams from current position to the end of position and display the result to RichTextBox as Text
  25. RichTextBox1.

    Text

    =

    myStreamReader.

    ReadToEnd

    (

    )
  26. Catch

    ex As

    Exception
  27. 'it will catch if any errors occurs
  28. MsgBox

    (

    ex.

    Message

    , MsgBoxStyle.

    Information

    )
  29. End

    Try
  30. End

    Sub

  31. Private

    Sub

    menuSave_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    menuSave.

    Click
  32. 'call the savefiledialog
  33. SaveFileDialog1.

    ShowDialog

    (

    )
  34. 'we use StreamWriter for writing characters to a stream
  35. Dim

    myStreamWriter As

    System.

    IO

    .

    StreamWriter
  36. 'we set default for savefiledialog filter
  37. SaveFileDialog1.

    Filter

    =

    "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
  38. 'set the checkpath to true
  39. SaveFileDialog1.

    CheckPathExists

    =

    True
  40. 'set the Savefiledialog title
  41. SaveFileDialog1.

    Title

    =

    "Save File"

  42. Try
  43. 'gets a string containing the filename selected in the dialog box
  44. myStreamWriter =

    System.

    IO

    .

    File

    .

    AppendText

    (

    SaveFileDialog1.

    FileName

    )
  45. 'it writes the string to stream
  46. myStreamWriter.

    Write

    (

    RichTextBox1.

    Text

    )
  47. 'clears all buffers for the current writer and causes any
  48. 'buffered data to be written to the underlying stream
  49. myStreamWriter.

    Flush

    (

    )
  50. Catch

    ex As

    Exception
  51. 'catch unnecesary errors
  52. MsgBox

    (

    ex.

    Message

    )

  53. End

    Try
  54. 'shows we have saved the most recent changes
  55. savedoc =

    True


  56. End

    Sub

  57. Private

    Sub

    menuSaveAs_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    menuSaveAs.

    Click
  58. 'show the savefiledialog
  59. SaveFileDialog1.

    ShowDialog

    (

    )
  60. 'if the user encode the file and click save
  61. If

    SaveFileDialog1.

    ShowDialog

    =

    DialogResult.

    OK

    Then
  62. 'it create a streamwriter to write in to the file
  63. Dim

    writer As

    New

    System.

    IO

    .

    StreamWriter

    (

    SaveFileDialog1.

    FileName

    , False

    )
  64. 'get the content of Richtextbox and write to a stream
  65. writer.

    Write

    (

    RichTextBox1.

    Text

    )
  66. 'close the writer
  67. writer.

    Close

    (

    )
  68. 'it gets the data contains by the control and save the filename for later use.
  69. RichTextBox1.

    Tag

    =

    SaveFileDialog1.

    FileName

  70. End

    If
  71. End

    Sub

  72. Private

    Sub

    Form1_FormClosing(

    ByVal

    sender As

    Object

    , ByVal

    e As

    System.

    Windows

    .

    Forms

    .

    FormClosingEventArgs

    )

    Handles

    Me

    .

    FormClosing

  73. Dim

    Result As

    DialogResult
  74. 'if the current unsaved changes occur we will have to use this option
  75. If

    RichTextBox1.

    Text

    <>

    ""

    Then
  76. If

    savedoc =

    True

    Then
  77. Result =

    MessageBox.

    Show

    (

    "Would you like to save changes?"

    , "Confirm Exit"

    , MessageBoxButtons.

    YesNo

    , MessageBoxIcon.

    Question

    )
  78. If

    Result =

    Windows.

    Forms

    .

    DialogResult

    .

    Yes

    Then
  79. SaveFileDialog1.

    ShowDialog

    (

    )
  80. Dim

    myStreamWriter As

    System.

    IO

    .

    StreamWriter
  81. SaveFileDialog1.

    Filter

    =

    "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
  82. SaveFileDialog1.

    CheckPathExists

    =

    True
  83. SaveFileDialog1.

    Title

    =

    "Save File"
  84. SaveFileDialog1.

    ShowDialog

    (

    Me

    )
  85. Try
  86. myStreamWriter =

    System.

    IO

    .

    File

    .

    AppendText

    (

    SaveFileDialog1.

    FileName

    )
  87. myStreamWriter.

    Write

    (

    RichTextBox1.

    Text

    )
  88. myStreamWriter.

    Flush

    (

    )
  89. Catch

    ex As

    Exception
  90. MessageBox.

    Show

    (

    "There has been an error in the saving process"

    )
  91. End

    Try
  92. savedoc =

    True
  93. Application.

    Exit

    (

    )
  94. End

    If
  95. If

    Result =

    Windows.

    Forms

    .

    DialogResult

    .

    No

    Then
  96. Application.

    Exit

    (

    )
  97. End

    If
  98. Else

    : Application.

    Exit

    (

    )
  99. End

    If
  100. Else

    : Application.

    Exit

    (

    )
  101. End

    If

  102. End

    Sub
  103. End

    Class

Book traversal links for File Handling: Form Closing Event

  • ‹ File Handling: Adding a Save As Command
  • Up
  • File Handling: How to Open a Text File in Visual Basic.NET ›

 

440,010

316,559

316,568

Top