JayBeeOH

TxtEditor - File Open Option - OpenFileDialog

Feb 18th, 2015
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.75 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 © 2015. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Text Editor
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   Feb 2015
  16. '
  17. ' Description:    Simple plain text editor program with printing ability.
  18. '
  19. '                 Documentation is at:
  20. '                   App's screen image is at: http://imgur.com/TzWs9kO
  21. '                   App's screen design time specs at: http://pastebin.com/bhinRQ5d
  22. '                   App's Visual Basic .NET Event Procedures & TODO's is at
  23. '                       http://pastebin.com/UZ4tcEpE
  24. '                   App's Visual Basic .NET code is at http://pastebin.com/u/jaybeeoh
  25. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  26. '-------------------------------------------------------------------------------------------
  27.  
  28. Imports System.IO
  29. Imports System.Security
  30.  
  31. #Region "  Declare class level variables"
  32.  
  33.     Public applicationTitle As String = My.Application.Info.Title
  34.  
  35.     Private Const fileFilter As String =
  36.       "Plain Text (*.txt)|*.txt|" &
  37.       "Log Files (*.log)|*.log|" &
  38.       "All Files (*.*)|*.*"
  39.  
  40.     Private workingPathFile As String = String.Empty
  41.     Private workingPath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  42.  
  43. #End Region
  44.  
  45.     ' File Open option.
  46.     Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) _
  47.         Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
  48.  
  49.         If SaveChanges() Then
  50.             SaveToolStripMenuItem_Click(sender, e)
  51.         End If
  52.  
  53.         With OpenFileDialog1
  54.             .Multiselect = False
  55.             .Filter = fileFilter
  56.             .DefaultExt = "txt"
  57.             .FileName = String.Empty
  58.             .InitialDirectory = workingPath
  59.             If DialogResult.OK = .ShowDialog Then
  60.                 workingPathFile = .FileName
  61.                 GetFile()
  62.             End If
  63.         End With
  64.     End Sub
  65.  
  66.     ' Read into the Textbox's text property the data from a file using My.Computer.FileSystem.
  67.     Private Sub GetFile()
  68.  
  69.         Try
  70.             With MyTextBox
  71.         .Text = My.Computer.FileSystem.ReadAllText(workingPathFile, System.Text.Encoding.UTF8)
  72.                 .Modified = False
  73.                 .Select(0, 0)
  74.                 .Focus()
  75.             End With
  76.  
  77.             workingPath = Path.GetDirectoryName(workingPathFile)
  78.             Me.Text = applicationTitle & " - " & Path.GetFileName(workingPathFile)
  79.  
  80.         Catch ex As IOException
  81.             MessageBox.Show(ex.Message, "File IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  82.         Catch ex As SecurityException
  83.             MessageBox.Show(ex.Message, "File Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  84.         Catch ex As Exception
  85.             MessageBox.Show("There was a problem opening the file selected." &
  86.                             ControlChars.NewLine & ex.Message,
  87.                            "Error Opening File",
  88.                            MessageBoxButtons.OK,
  89.                            MessageBoxIcon.Error)
  90.         End Try
  91.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment