vb.net对话框操作实例(三)
Creating a Save dialog that enables you to save filesCreating a Font dialog that enables you to apply the selected font to textCreating a Color dialog that enables you to define and select custom colorsCreating a Print dialog that prints text from your applicationCreating a Browse dialog that enables you to browse for folders
工具/原料
visual studio 2015
打开对话框
1、 Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Cli艘早祓胂ck 'Set the Open dialog properties OpenFileDialog1.Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*" OpenFileDialog1.FilterIndex = 1 OpenFileDialog1.Title = "Demo Open File Dialog" 'Show the Open dialog if the user clicks the Open button, 'load the file If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then Try 'Save the file path and name strFileName = OpenFileDialog1.FileName Dim fileContents As String fileContents = My.Computer.FileSystem.ReadAllText(strFileName) 'Display the file contents in the text box txtFile.Text = fileContents Catch ex As Exception MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Sub

保存对话框
1、Private Sub bthSave_Click(sender As Object, e As EventArgs) Handles bthSave.Click 'Set the Save dialog properties With SaveFileDialog1 .DefaultExt = "txt" .FileName = strFileName .Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*" .FilterIndex = 1 .OverwritePrompt = True .Title = "Demo Save File Dialog" End With 'Show the Save dialog and if the user clicks the Save button, 'save the file If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then Try 'Save the file path and name strFileName = SaveFileDialog1.FileName My.Computer.FileSystem.WriteAllText(strFileName, txtFile.Text, False) Catch ex As Exception MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Sub



浏览对话框
1、 Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click 'Set the FolderBrowser dialog properties With FolderBrowserDialog1 .Description = "Select a backup folder" .RootFolder = Environment.SpecialFolder.MyComputer .ShowNewFolderButton = False End With 'Show the FolderBrowser dialog and if the user clicks the 'OK button, display the selected folder If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then txtFile.Text = FolderBrowserDialog1.SelectedPath End If End Sub
