JayBeeOH

Dialogs Demo

Jan 6th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.52 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 © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Dialogs Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   Jan 2016
  16. '
  17. ' Description:    Demonstrate the OpenFileDialog, SaveFileDialog, FontDialog, ColorDialog,
  18. '                 and FolderBrowserDialog dialog classes.
  19. '
  20. '                 Documentation is at:
  21. '                   App's screen image is at: http://i.imgur.com/OKq1hPC.png
  22. '                   App's Visual Basic .NET code is at http://pastebin.com/TCE4UPaM
  23. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  24. '-------------------------------------------------------------------------------------------
  25.  
  26. Public Class MainForm
  27.  
  28.     ' Set Class level variable and Constant.
  29.     Private pathFile As String = String.Empty
  30.     Private Const FILE_FILTER As String = "Text Documents (*.txt)|*.txt|All files (*.*)|*.*"
  31.  
  32.  
  33.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  34.         Handles Me.Load
  35.  
  36.         ' Set some OpenFileDialog properties.
  37.         With OpenFileDialog1
  38.             .Filter = FILE_FILTER
  39.             .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  40.             .Title = "Open File"
  41.         End With
  42.  
  43.         ' Set some SaveFileDialog properties.
  44.         With SaveFileDialog1
  45.             .Filter = FILE_FILTER
  46.             .DefaultExt = "txt"
  47.             .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  48.             .OverwritePrompt = True
  49.             .Title = "Save File"
  50.         End With
  51.     End Sub
  52.  
  53.     ' Demo the OpenFileDialog to get a source pathFile for input.
  54.     Private Sub OpenButton_Click(sender As Object, e As EventArgs) _
  55.         Handles OpenButton.Click
  56.  
  57.  
  58.         OpenFileDialog1.FileName = String.Empty
  59.  
  60.         If OpenFileDialog1.ShowDialog = DialogResult.OK Then
  61.             pathFile = OpenFileDialog1.FileName
  62.             ReadFileContents(pathFile)
  63.         End If
  64.     End Sub
  65.  
  66.     ' Using Insert Snippet read pathFile data to the ContentsTextBox's
  67.     ' text property.
  68.     Private Sub ReadFileContents(file As String)
  69.  
  70.         Try
  71.             Dim fileContents As String
  72.             fileContents = My.Computer.FileSystem.ReadAllText(file)
  73.             ContentsTextBox.Text = fileContents
  74.  
  75.         Catch ex As Exception
  76.             MessageBox.Show(ex.Message,
  77.                             My.Application.Info.Title,
  78.                             MessageBoxButtons.OK,
  79.                             MessageBoxIcon.Error)
  80.         End Try
  81.     End Sub
  82.  
  83.     ' Demo the SaveFileDialog to set a target pathFile for output.
  84.     Private Sub SaveButton_Click(sender As Object, e As EventArgs) _
  85.         Handles SaveButton.Click
  86.  
  87.         SaveFileDialog1.FileName = My.Computer.FileSystem.GetName(pathFile)
  88.  
  89.         If SaveFileDialog1.ShowDialog = DialogResult.OK Then
  90.             pathFile = SaveFileDialog1.FileName
  91.             WriteFileContents(pathFile)
  92.         End If
  93.     End Sub
  94.  
  95.     ' Using Insert Snippet write ContentsTextBox's text property value.
  96.     Private Sub WriteFileContents(file As String)
  97.  
  98.         Try
  99.             My.Computer.FileSystem.WriteAllText(file, ContentsTextBox.Text, False)
  100.  
  101.         Catch ex As Exception
  102.             MessageBox.Show(ex.Message,
  103.                             My.Application.Info.Title,
  104.                             MessageBoxButtons.OK,
  105.                             MessageBoxIcon.Error)
  106.         End Try
  107.     End Sub
  108.  
  109.     ' Demo the FontDialog to change both the Font and Forecolor of the ContentsTextBox.
  110.     Private Sub FontButton_Click(sender As Object, e As EventArgs) _
  111.         Handles FontButton.Click
  112.  
  113.         With FontDialog1
  114.             .ShowColor = True
  115.             .Font = ContentsTextBox.Font
  116.             .Color = ContentsTextBox.ForeColor
  117.  
  118.             If .ShowDialog = DialogResult.OK Then
  119.                 ContentsTextBox.Font = .Font
  120.                 ContentsTextBox.ForeColor = .Color
  121.             End If
  122.         End With
  123.     End Sub
  124.  
  125.     ' Demo the ColorDialog to change the ContentsTextBox's backcolor.
  126.     Private Sub ColorButton_Click(sender As Object, e As EventArgs) _
  127.         Handles ColorButton.Click
  128.  
  129.         With ColorDialog1
  130.             .Color = ContentsTextBox.BackColor
  131.             '.FullOpen = True
  132.  
  133.             If .ShowDialog = DialogResult.OK Then
  134.                 ContentsTextBox.BackColor = .Color
  135.             End If
  136.         End With
  137.     End Sub
  138.  
  139.     ' Demo the FolderBrowserDialog.
  140.     Private Sub BrowseButton_Click(sender As Object, e As EventArgs) _
  141.         Handles BrowseButton.Click
  142.  
  143.         With FolderBrowserDialog1
  144.             .RootFolder = Environment.SpecialFolder.MyComputer
  145.             .ShowNewFolderButton = False
  146.  
  147.             If .ShowDialog = DialogResult.OK Then
  148.                 ContentsTextBox.Text = .SelectedPath
  149.             End If
  150.         End With
  151.     End Sub
  152. End Class
Advertisement
Add Comment
Please, Sign In to add comment