JayBeeOH

Quick Note - Exception Handling Demo

Aug 29th, 2014
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.86 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2014. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   QuickNote
  13. ' Author:         Joseph L. Bolen
  14. ' Date Created:   Sep 2014
  15. '
  16. ' Description:    The program is a very simple TextBox editor. The data is saved and
  17. '                 retrieved from the hardcode PathFile variable of "\QuickNote".
  18. '                 The purpose of program is to demonstrate Exception handling with the
  19. '                 Try/Catch/Finally/End Try block.
  20. '
  21. '                 Documentation is at:
  22. '                   App's screen capture image is at http://imgur.com/MYee4oc
  23. '                   App's Visual Basic .NET code is at http://pastebin.com/0y8TC2cZ
  24. '                   App's video tutorial is at http://www.youtube.com/user/bolenpresents
  25.  
  26. '                 For additonal information regarding error/exception handling, see MSDN's
  27. '                   article, "Try...Catch...Finally Statement (Visual Basic)" at
  28. '                   http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx .
  29. '
  30. '                 Control properties set at design time:
  31. '                   MyTextBox.Font = Calibri, 12pt
  32. '                   MyTextBox.Scroll = Vertical
  33. '                   MyTextBox.Achor = Top,Bottom,Left,Right
  34. '                   SaveFileButton.Enabled = False
  35. '                   *Buttons.Achor = Bottom,Right
  36. '                  
  37. '-------------------------------------------------------------------------------------------
  38. Option Strict On
  39.  
  40. Imports System.IO
  41. Imports System.Security
  42.  
  43. Public Class MainForm
  44.  
  45. #Region " Public Properties"
  46.     Private PathFile As String = "\QuickNote.txt"
  47. #End Region
  48.  
  49. #Region " Form Events"
  50.  
  51.     ' Inital intialization of form and settings.
  52.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  53.         Handles MyBase.Load
  54.  
  55.         PathFile = My.Computer.FileSystem.SpecialDirectories.MyDocuments & PathFile
  56.         SaveFileButton.Enabled = False
  57.     End Sub
  58.  
  59. #End Region
  60.  
  61. #Region " Control Events"
  62.  
  63.     ' Read into the Textbox data from a file using My.Computer.FilesSystem.
  64.     Private Sub OpenFileButton_Click(sender As Object, e As EventArgs) _
  65.         Handles OpenFileButton.Click
  66.  
  67.         Try
  68.             With MyTextBox
  69.                 .Text = My.Computer.FileSystem.ReadAllText(PathFile)
  70.                 .Modified = False
  71.                 .Select(.Text.Length, 0)
  72.                 .ScrollToCaret()
  73.                 .Focus()
  74.             End With
  75.         Catch ex As IOException
  76.             MessageBox.Show(ex.Message, "File IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  77.         Catch ex As SecurityException
  78.             MessageBox.Show(ex.Message, "File Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  79.         Catch ex As Exception
  80.             MessageBox.Show(ex.GetType.ToString & ControlChars.NewLine & ControlChars.NewLine & ex.Message,
  81.                             "File IO Error",
  82.                             MessageBoxButtons.OK,
  83.                             MessageBoxIcon.Error)
  84.         End Try
  85.     End Sub
  86.  
  87.     ' Write from the Textbox data to a file using My.Computer.FilesSystem.
  88.     Private Sub SaveFileButton_Click(sender As Object, e As EventArgs) _
  89.         Handles SaveFileButton.Click
  90.  
  91.         If MyTextBox.Modified Then
  92.             Try
  93.                 With MyTextBox
  94.                     My.Computer.FileSystem.WriteAllText(PathFile, .Text, False)
  95.                     MessageBox.Show("File successfully saved.", "File Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
  96.                     .Modified = False
  97.                     .Select(.Text.Length, 0)
  98.                     .ScrollToCaret()
  99.                     .Focus()
  100.                 End With
  101.             Catch ex As IOException
  102.                 MessageBox.Show(ex.Message, "File IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  103.             Catch ex As SecurityException
  104.                 MessageBox.Show(ex.Message, "File Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  105.             Catch ex As Exception
  106.                 MessageBox.Show(ex.GetType.ToString & ControlChars.NewLine & ControlChars.NewLine & ex.Message,
  107.                                 "File IO Error",
  108.                                 MessageBoxButtons.OK,
  109.                                 MessageBoxIcon.Error)
  110.             End Try
  111.         End If
  112.     End Sub
  113.  
  114.     ' Clear the textbox.
  115.     Private Sub ClearTextButton_Click(sender As Object, e As EventArgs) _
  116.         Handles ClearTextButton.Click
  117.  
  118.         MyTextBox.Clear()    ' Modified property is automatically set to False.
  119.         MyTextBox.Focus()
  120.     End Sub
  121.  
  122.     ' Toggle the SaveFileButton based on the state of the Textbox's Modified property.
  123.     Private Sub MyTextBox_ModifiedChanged(sender As Object, e As EventArgs) _
  124.         Handles MyTextBox.ModifiedChanged
  125.  
  126.         SaveFileButton.Enabled = MyTextBox.Modified
  127.     End Sub
  128.  
  129. #End Region
  130.  
  131. End Class
  132.  
  133. '===============================================================================================
  134. ' Starting Point
  135. '===============================================================================================
  136. '------------------------------------------------------------------------------------------
  137. '           Notice of My Copyright and Intellectual Property Rights
  138. '
  139. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  140. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  141. ' publish or provide such intellectual property to any other person or entity for any
  142. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  143. '
  144. '                 Copyright © 2014. All rights reserved.
  145. '        All trademarks remain the property of their respective owners.
  146. '-------------------------------------------------------------------------------------------
  147. ' Program Name:   QuickNote
  148. ' Author:         Joseph L. Bolen
  149. ' Date Created:   Sep 2014
  150. '
  151. ' Description:    The program is a very simple TextBox editor. The data is saved and
  152. '                 retrieved from the hardcode PathFile variable of "\QuickNote".
  153. '                 The purpose of program is to demonstrate Exception handling with the
  154. '                 Try/Catch/Finally/End Try block.
  155. '
  156. '                 Documentation is at:
  157. '                   App's screen capture image is at http://imgur.com/MYee4oc
  158. '                   App's Visual Basic .NET code is at http://pastebin.com/0y8TC2cZ
  159. '                   App's video tutorial is at http://www.youtube.com/user/bolenpresents
  160.  
  161. '                 For additonal information regarding error/exception handling, see MSDN's
  162. '                   article, "Try...Catch...Finally Statement (Visual Basic)" at
  163. '                   http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx .
  164. '
  165. '                 Control properties set at design time:
  166. '                   MyTextBox.Font = Calibri, 12pt
  167. '                   MyTextBox.Scroll = Vertical
  168. '                   MyTextBox.Achor = Top,Bottom,Left,Right
  169. '                   SaveFileButton.Enabled = False
  170. '                   *Buttons.Achor = Bottom,Right
  171. '                  
  172. '-------------------------------------------------------------------------------------------
  173. Option Strict On
  174.  
  175. Public Class MainForm
  176.  
  177. #Region " Public Properties"
  178.     Const PathFile As String = "D:\QuickNote.txt"
  179. #End Region
  180.  
  181. #Region " Form Events"
  182.  
  183.     ' Inital intialization of form and settings.
  184.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  185.         Handles MyBase.Load
  186.  
  187.         SaveFileButton.Enabled = False
  188.     End Sub
  189.  
  190. #End Region
  191.  
  192. #Region " Control Events"
  193.  
  194.     ' Read into the Textbox data from a file using My.Computer.FilesSystem.
  195.     Private Sub OpenFileButton_Click(sender As Object, e As EventArgs) _
  196.         Handles OpenFileButton.Click
  197.  
  198.  
  199.         With MyTextBox
  200.             .Text = My.Computer.FileSystem.ReadAllText(PathFile)
  201.             .Modified = False
  202.             .Select(.Text.Length, 0)
  203.             .ScrollToCaret()
  204.             .Focus()
  205.         End With
  206.  
  207.     End Sub
  208.  
  209.     ' Write from the Textbox data to a file using My.Computer.FilesSystem.
  210.     Private Sub SaveFileButton_Click(sender As Object, e As EventArgs) _
  211.         Handles SaveFileButton.Click
  212.  
  213.         If MyTextBox.Modified Then
  214.  
  215.             With MyTextBox
  216.                 My.Computer.FileSystem.WriteAllText(PathFile, .Text, False)
  217.                 MessageBox.Show("File successfully saved.", "File Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
  218.                 .Modified = False
  219.                 .Select(.Text.Length, 0)
  220.                 .ScrollToCaret()
  221.                 .Focus()
  222.             End With
  223.  
  224.         End If
  225.     End Sub
  226.  
  227.     ' Clear the textbox.
  228.     Private Sub ClearTextButton_Click(sender As Object, e As EventArgs) _
  229.         Handles ClearTextButton.Click
  230.  
  231.         MyTextBox.Clear()    ' Modified property is automatically set to False.
  232.         MyTextBox.Focus()
  233.     End Sub
  234.  
  235.     ' Toggle the SaveFileButton based on the state of the Textbox's Modified property.
  236.     Private Sub MyTextBox_ModifiedChanged(sender As Object, e As EventArgs) _
  237.         Handles MyTextBox.ModifiedChanged
  238.  
  239.         SaveFileButton.Enabled = MyTextBox.Modified
  240.     End Sub
  241.  
  242. #End Region
  243.  
  244. End Class
Advertisement
Add Comment
Please, Sign In to add comment