Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2014. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: QuickNote
- ' Author: Joseph L. Bolen
- ' Date Created: Sep 2014
- '
- ' Description: The program is a very simple TextBox editor. The data is saved and
- ' retrieved from the hardcode PathFile variable of "\QuickNote".
- ' The purpose of program is to demonstrate Exception handling with the
- ' Try/Catch/Finally/End Try block.
- '
- ' Documentation is at:
- ' App's screen capture image is at http://imgur.com/MYee4oc
- ' App's Visual Basic .NET code is at http://pastebin.com/0y8TC2cZ
- ' App's video tutorial is at http://www.youtube.com/user/bolenpresents
- ' For additonal information regarding error/exception handling, see MSDN's
- ' article, "Try...Catch...Finally Statement (Visual Basic)" at
- ' http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx .
- '
- ' Control properties set at design time:
- ' MyTextBox.Font = Calibri, 12pt
- ' MyTextBox.Scroll = Vertical
- ' MyTextBox.Achor = Top,Bottom,Left,Right
- ' SaveFileButton.Enabled = False
- ' *Buttons.Achor = Bottom,Right
- '
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Imports System.IO
- Imports System.Security
- Public Class MainForm
- #Region " Public Properties"
- Private PathFile As String = "\QuickNote.txt"
- #End Region
- #Region " Form Events"
- ' Inital intialization of form and settings.
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- PathFile = My.Computer.FileSystem.SpecialDirectories.MyDocuments & PathFile
- SaveFileButton.Enabled = False
- End Sub
- #End Region
- #Region " Control Events"
- ' Read into the Textbox data from a file using My.Computer.FilesSystem.
- Private Sub OpenFileButton_Click(sender As Object, e As EventArgs) _
- Handles OpenFileButton.Click
- Try
- With MyTextBox
- .Text = My.Computer.FileSystem.ReadAllText(PathFile)
- .Modified = False
- .Select(.Text.Length, 0)
- .ScrollToCaret()
- .Focus()
- End With
- Catch ex As IOException
- MessageBox.Show(ex.Message, "File IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As SecurityException
- MessageBox.Show(ex.Message, "File Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As Exception
- MessageBox.Show(ex.GetType.ToString & ControlChars.NewLine & ControlChars.NewLine & ex.Message,
- "File IO Error",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- End Sub
- ' Write from the Textbox data to a file using My.Computer.FilesSystem.
- Private Sub SaveFileButton_Click(sender As Object, e As EventArgs) _
- Handles SaveFileButton.Click
- If MyTextBox.Modified Then
- Try
- With MyTextBox
- My.Computer.FileSystem.WriteAllText(PathFile, .Text, False)
- MessageBox.Show("File successfully saved.", "File Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
- .Modified = False
- .Select(.Text.Length, 0)
- .ScrollToCaret()
- .Focus()
- End With
- Catch ex As IOException
- MessageBox.Show(ex.Message, "File IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As SecurityException
- MessageBox.Show(ex.Message, "File Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As Exception
- MessageBox.Show(ex.GetType.ToString & ControlChars.NewLine & ControlChars.NewLine & ex.Message,
- "File IO Error",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- End If
- End Sub
- ' Clear the textbox.
- Private Sub ClearTextButton_Click(sender As Object, e As EventArgs) _
- Handles ClearTextButton.Click
- MyTextBox.Clear() ' Modified property is automatically set to False.
- MyTextBox.Focus()
- End Sub
- ' Toggle the SaveFileButton based on the state of the Textbox's Modified property.
- Private Sub MyTextBox_ModifiedChanged(sender As Object, e As EventArgs) _
- Handles MyTextBox.ModifiedChanged
- SaveFileButton.Enabled = MyTextBox.Modified
- End Sub
- #End Region
- End Class
- '===============================================================================================
- ' Starting Point
- '===============================================================================================
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2014. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: QuickNote
- ' Author: Joseph L. Bolen
- ' Date Created: Sep 2014
- '
- ' Description: The program is a very simple TextBox editor. The data is saved and
- ' retrieved from the hardcode PathFile variable of "\QuickNote".
- ' The purpose of program is to demonstrate Exception handling with the
- ' Try/Catch/Finally/End Try block.
- '
- ' Documentation is at:
- ' App's screen capture image is at http://imgur.com/MYee4oc
- ' App's Visual Basic .NET code is at http://pastebin.com/0y8TC2cZ
- ' App's video tutorial is at http://www.youtube.com/user/bolenpresents
- ' For additonal information regarding error/exception handling, see MSDN's
- ' article, "Try...Catch...Finally Statement (Visual Basic)" at
- ' http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx .
- '
- ' Control properties set at design time:
- ' MyTextBox.Font = Calibri, 12pt
- ' MyTextBox.Scroll = Vertical
- ' MyTextBox.Achor = Top,Bottom,Left,Right
- ' SaveFileButton.Enabled = False
- ' *Buttons.Achor = Bottom,Right
- '
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Public Class MainForm
- #Region " Public Properties"
- Const PathFile As String = "D:\QuickNote.txt"
- #End Region
- #Region " Form Events"
- ' Inital intialization of form and settings.
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- SaveFileButton.Enabled = False
- End Sub
- #End Region
- #Region " Control Events"
- ' Read into the Textbox data from a file using My.Computer.FilesSystem.
- Private Sub OpenFileButton_Click(sender As Object, e As EventArgs) _
- Handles OpenFileButton.Click
- With MyTextBox
- .Text = My.Computer.FileSystem.ReadAllText(PathFile)
- .Modified = False
- .Select(.Text.Length, 0)
- .ScrollToCaret()
- .Focus()
- End With
- End Sub
- ' Write from the Textbox data to a file using My.Computer.FilesSystem.
- Private Sub SaveFileButton_Click(sender As Object, e As EventArgs) _
- Handles SaveFileButton.Click
- If MyTextBox.Modified Then
- With MyTextBox
- My.Computer.FileSystem.WriteAllText(PathFile, .Text, False)
- MessageBox.Show("File successfully saved.", "File Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
- .Modified = False
- .Select(.Text.Length, 0)
- .ScrollToCaret()
- .Focus()
- End With
- End If
- End Sub
- ' Clear the textbox.
- Private Sub ClearTextButton_Click(sender As Object, e As EventArgs) _
- Handles ClearTextButton.Click
- MyTextBox.Clear() ' Modified property is automatically set to False.
- MyTextBox.Focus()
- End Sub
- ' Toggle the SaveFileButton based on the state of the Textbox's Modified property.
- Private Sub MyTextBox_ModifiedChanged(sender As Object, e As EventArgs) _
- Handles MyTextBox.ModifiedChanged
- SaveFileButton.Enabled = MyTextBox.Modified
- End Sub
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment