JayBeeOH

TxtEditor - File Save, Save As Options - SaveFileDialog

Feb 23rd, 2015
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.36 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 Save option.
  46.     Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) _
  47.         Handles SaveToolStripMenuItem.Click, SaveToolStripButton.Click
  48.  
  49.         ' If there is a valid file name, save file. Otherwise follow Save As logic.
  50.         If (workingPathFile <> String.Empty) AndAlso
  51.             (workingPathFile.EndsWith(".txt") OrElse workingPathFile.EndsWith(".log")) Then
  52.             SaveFile()
  53.         Else
  54.             SaveAsToolStripMenuItem_Click(sender, e)
  55.         End If
  56.     End Sub
  57.  
  58.     ' File Save As option.
  59.     Private Sub SaveAsToolStripMenuItem_Click(sender As Object, e As EventArgs) _
  60.         Handles SaveAsToolStripMenuItem.Click
  61.  
  62.         ' Use the Save File Dialog to get the file name, then save file.
  63.         With SaveFileDialog1
  64.             .Filter = fileFilter
  65.             .DefaultExt = "txt"
  66.             If workingPathFile = String.Empty Then
  67.                 .FileName = "Document.txt"
  68.                 .InitialDirectory = workingPath
  69.             Else
  70.                 .FileName = Path.GetFileName(workingPathFile)
  71.                 .InitialDirectory = Path.GetDirectoryName(workingPathFile)
  72.             End If
  73.             If DialogResult.OK = .ShowDialog Then
  74.                 workingPathFile = .FileName
  75.                 SaveFile()
  76.             End If
  77.         End With
  78.     End Sub
  79.  
  80.     ' Write from Textbox's text property the data to a file using My.Computer.FileSystem.
  81.     Private Sub SaveFile()
  82.  
  83.         Try
  84.         My.Computer.FileSystem.WriteAllText(workingPathFile, MyTextBox.Text, False, System.Text.Encoding.UTF8)
  85.             MyTextBox.Modified = False
  86.             MyTextBox.Focus()
  87.  
  88.             workingPath = Path.GetDirectoryName(workingPathFile)
  89.             Me.Text = applicationTitle & " - " & Path.GetFileName(workingPathFile)
  90.  
  91.         Catch ex As IOException
  92.             MessageBox.Show(ex.Message, "File IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  93.         Catch ex As SecurityException
  94.             MessageBox.Show(ex.Message, "File Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  95.         Catch ex As Exception
  96.             MessageBox.Show("There was a problem saving the file." &
  97.                             ControlChars.NewLine & ex.Message,
  98.                            "Error Saving File",
  99.                            MessageBoxButtons.OK,
  100.                            MessageBoxIcon.Error)
  101.         End Try
  102.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment