vb.net对话框操作实例(三)

2025-10-19 00:53:13

1、  Private Sub btnOpen_Click(sender As Object,

    e As EventArgs) Handles btnOpen.Click

        '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

vb.net对话框操作实例(三)

vb.net对话框操作实例(三)

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

vb.net对话框操作实例(三)

1、  Private Sub btnFont_Click(sender As Object,

        e As EventArgs) Handles btnFont.Click

        'Set the Font dialog properties

        FontDialog1.ShowColor = True

        'Show the Font dialog and if the user clicks the OK button,

        'update the font and color in the text box

        If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            txtFile.Font = FontDialog1.Font

            txtFile.ForeColor = FontDialog1.Color

        End If

    End Sub

vb.net对话框操作实例(三)

vb.net对话框操作实例(三)

1、    Private Sub btmColor_Click(sender As Object, e As EventArgs) Handles btmColor.Click

        'Show the Color dialog and if the user clicks the OK button,

        'update the background color of the form

        If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            Me.BackColor = ColorDialog1.Color

        End If

    End Sub

vb.net对话框操作实例(三)

vb.net对话框操作实例(三)

1、  Private Sub btnPrint_Click(sender As Object,

    e As EventArgs) Handles btnPrint.Click

        'Instantiate a new instance of the PrintDocument

        DialogsPrintDocument = New PrintDocument

        'Set the PrintDialog properties

        With PrintDialog1

            .AllowCurrentPage = False

            .AllowPrintToFile = False

            .AllowSelection = False

            .AllowSomePages = False

            .Document = DialogsPrintDocument

            .PrinterSettings.DefaultPageSettings.Margins.Top = 25

            .PrinterSettings.DefaultPageSettings.Margins.Bottom = 25

            .PrinterSettings.DefaultPageSettings.Margins.Left = 25

            .PrinterSettings.DefaultPageSettings.Margins.Right = 25

        End With

        If PrintDialog1.ShowDialog = DialogResult.OK Then

            'Set the selected printer settings in the PrintDocument

            DialogsPrintDocument.PrinterSettings =

            PrintDialog1.PrinterSettings

            'Get the print data

            strPrintRecord = txtFile.Text

            'Invoke the Print method on the PrintDocument

            DialogsPrintDocument.Print()

        End If

    End Sub

vb.net对话框操作实例(三)

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

vb.net对话框操作实例(三)

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢