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 © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Dialogs Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: Jan 2016
- '
- ' Description: Demonstrate the OpenFileDialog, SaveFileDialog, FontDialog, ColorDialog,
- ' and FolderBrowserDialog dialog classes.
- '
- ' Documentation is at:
- ' App's screen image is at: http://i.imgur.com/OKq1hPC.png
- ' App's Visual Basic .NET code is at http://pastebin.com/TCE4UPaM
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Public Class MainForm
- ' Set Class level variable and Constant.
- Private pathFile As String = String.Empty
- Private Const FILE_FILTER As String = "Text Documents (*.txt)|*.txt|All files (*.*)|*.*"
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles Me.Load
- ' Set some OpenFileDialog properties.
- With OpenFileDialog1
- .Filter = FILE_FILTER
- .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
- .Title = "Open File"
- End With
- ' Set some SaveFileDialog properties.
- With SaveFileDialog1
- .Filter = FILE_FILTER
- .DefaultExt = "txt"
- .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
- .OverwritePrompt = True
- .Title = "Save File"
- End With
- End Sub
- ' Demo the OpenFileDialog to get a source pathFile for input.
- Private Sub OpenButton_Click(sender As Object, e As EventArgs) _
- Handles OpenButton.Click
- OpenFileDialog1.FileName = String.Empty
- If OpenFileDialog1.ShowDialog = DialogResult.OK Then
- pathFile = OpenFileDialog1.FileName
- ReadFileContents(pathFile)
- End If
- End Sub
- ' Using Insert Snippet read pathFile data to the ContentsTextBox's
- ' text property.
- Private Sub ReadFileContents(file As String)
- Try
- Dim fileContents As String
- fileContents = My.Computer.FileSystem.ReadAllText(file)
- ContentsTextBox.Text = fileContents
- Catch ex As Exception
- MessageBox.Show(ex.Message,
- My.Application.Info.Title,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- End Sub
- ' Demo the SaveFileDialog to set a target pathFile for output.
- Private Sub SaveButton_Click(sender As Object, e As EventArgs) _
- Handles SaveButton.Click
- SaveFileDialog1.FileName = My.Computer.FileSystem.GetName(pathFile)
- If SaveFileDialog1.ShowDialog = DialogResult.OK Then
- pathFile = SaveFileDialog1.FileName
- WriteFileContents(pathFile)
- End If
- End Sub
- ' Using Insert Snippet write ContentsTextBox's text property value.
- Private Sub WriteFileContents(file As String)
- Try
- My.Computer.FileSystem.WriteAllText(file, ContentsTextBox.Text, False)
- Catch ex As Exception
- MessageBox.Show(ex.Message,
- My.Application.Info.Title,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- End Sub
- ' Demo the FontDialog to change both the Font and Forecolor of the ContentsTextBox.
- Private Sub FontButton_Click(sender As Object, e As EventArgs) _
- Handles FontButton.Click
- With FontDialog1
- .ShowColor = True
- .Font = ContentsTextBox.Font
- .Color = ContentsTextBox.ForeColor
- If .ShowDialog = DialogResult.OK Then
- ContentsTextBox.Font = .Font
- ContentsTextBox.ForeColor = .Color
- End If
- End With
- End Sub
- ' Demo the ColorDialog to change the ContentsTextBox's backcolor.
- Private Sub ColorButton_Click(sender As Object, e As EventArgs) _
- Handles ColorButton.Click
- With ColorDialog1
- .Color = ContentsTextBox.BackColor
- '.FullOpen = True
- If .ShowDialog = DialogResult.OK Then
- ContentsTextBox.BackColor = .Color
- End If
- End With
- End Sub
- ' Demo the FolderBrowserDialog.
- Private Sub BrowseButton_Click(sender As Object, e As EventArgs) _
- Handles BrowseButton.Click
- With FolderBrowserDialog1
- .RootFolder = Environment.SpecialFolder.MyComputer
- .ShowNewFolderButton = False
- If .ShowDialog = DialogResult.OK Then
- ContentsTextBox.Text = .SelectedPath
- End If
- End With
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment