VB2010(18)_各种对话框的使用
Imports System.Drawing.Printing
Public Class 對話框示例
? ? Private strPrintRecord As String
? ? Private WithEvents DialogsPrintDocument As PrintDocument
? ? Private strfileName As String
? ? '打開文件的對話框
? ? Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
? ? ? ? With OpenFileDialog1
? ? ? ? ? ? .Filter = "Text Document (*.txt)|*.txt|All Files (*.*)|*.*"
? ? ? ? ? ? .FilterIndex = 1 '確定在File filter組合框中顯示第幾個過濾器
? ? ? ? ? ? .Title = "demo Open file Dialogs"
? ? ? ? End With
? ? ? ? 'showdialog返回一個DialogResult值,只有兩個結果:OK,Cancel
? ? ? ? If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
? ? ? ? ? ? Try
? ? ? ? ? ? ? ? '保存文件路徑和文件名
? ? ? ? ? ? ? ? strfileName = OpenFileDialog1.FileName
? ? ? ? ? ? ? ? '讀取文本文件內容
? ? ? ? ? ? ? ? '可以用插入代碼段功能:快捷鍵ctrl+k,ctrl+x,
? ? ? ? ? ? ? ? '基本元素->文件系統->從文本中讀取文件
? ? ? ? ? ? ? ? Dim fileContents As String
? ? ? ? ? ? ? ? fileContents = My.Computer.FileSystem.ReadAllText(strfileName)
? ? ? ? ? ? ? ? '在文本框顯示讀出來的內容
? ? ? ? ? ? ? ? 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
? ? '保存文件的對話框
? ? Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
? ? ? ? With SaveFileDialog1
? ? ? ? ? ? .DefaultExt = "txt"
? ? ? ? ? ? .FileName = strfileName
? ? ? ? ? ? .Filter = "Text document (*.txt)|*.txt|all files (*.*)|*.*"
? ? ? ? ? ? .FilterIndex = 1
? ? ? ? ? ? .OverwritePrompt = True
? ? ? ? ? ? .Title = "Demo Save File Dialog"
? ? ? ? End With
? ? ? ? If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
? ? ? ? ? ? Try
? ? ? ? ? ? ? ? '可以用插入代碼段功能:快捷鍵ctrl+k,ctrl+x,
? ? ? ? ? ? ? ? '基本元素->文件系統->創建文件
? ? ? ? ? ? ? ? strfileName = SaveFileDialog1.FileName
? ? ? ? ? ? ? ? My.Computer.FileSystem.WriteAllText(strfileName, txtFile.Text, True)
? ? ? ? ? ? Catch ex As Exception
? ? ? ? ? ? ? ? MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
? ? ? ? ? ? End Try
? ? ? ? End If
? ? End Sub
? ? '字體對話框
? ? Private Sub btnFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFont.Click
? ? ? ? FontDialog1.ShowDialog()
? ? ? ? txtFile.Font = FontDialog1.Font
? ? End Sub
? ? '顏色對話框
? ? Private Sub btnColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColor.Click
? ? ? ? ColorDialog1.ShowDialog()
? ? ? ? txtFile.ForeColor = ColorDialog1.Color
? ? End Sub
? ? Private Sub DialogsPrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles DialogsPrintDocument.PrintPage
? ? ? ? Dim intCharactersToPrint As Integer
? ? ? ? Dim intLinesPerPage As Integer
? ? ? ? '包含在單個頁面上打印的所有數據
? ? ? ? Dim strPrintData As String
? ? ? ? '封裝了用于格式化要打花不了的數據的文本布局信息,用于按詞界修整數據,這樣文本不會超出打印區域
? ? ? ? Dim objStringFormat As New StringFormat
? ? ? ? Dim objPrintFont As New Font("Arial", 10) '設置用于打印文本的字體
? ? ? ? Dim objPageBoundaries As Rectangle '定義頁面的頂坐標和左坐標,以及寬度和高度
? ? ? ? Dim objPrintArea As SizeF '包含面面打印區域的高度和寬度,這是可打印的實際區域,而非頁面的實際尺寸
? ? ? ? '獲取打印邊界
? ? ? ? objPageBoundaries = New Rectangle(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
? ? ? ? '獲取打印區域
? ? ? ? objPrintArea = New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - objPrintFont.GetHeight(e.Graphics))
? ? ? ? '獲取單詞斷點
? ? ? ? objStringFormat.Trimming = StringTrimming.Word
? ? ? ? '獲取字符數
? ? ? ? e.Graphics.MeasureString(strPrintRecord, objPrintFont, objPrintArea, objStringFormat, intCharactersToPRint, intLinesPerPage)
? ? ? ? '獲取打印數據
? ? ? ? strPrintData = strPrintRecord.Substring(0, intCharactersToPRint)
? ? ? ? '打印頁
? ? ? ? e.Graphics.DrawString(strPrintData, objPrintFont, Brushes.Black, objPageBoundaries, objStringFormat)
? ? ? ? If intCharactersToPRint < strPrintRecord.Length Then
? ? ? ? ? ? strPrintRecord = strPrintRecord.Remove(0, intCharactersToPRint)
? ? ? ? ? ? e.HasMorePages = True
? ? ? ? Else
? ? ? ? ? ? e.HasMorePages = False
? ? ? ? End If
? ? End Sub
? ? Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
? ? ? ? DialogsPrintDocument = New PrintDocument
? ? ? ? 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
? ? ? ? ? ? DialogsPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings
? ? ? ? End If
? ? ? ? strPrintRecord = txtFile.Text
? ? ? ? DialogsPrintDocument.Print()
? ? End Sub
? ? Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
? ? ? ? With FolderBrowserDialog1
? ? ? ? ? ? .Description = "選擇一個文件夾"
? ? ? ? ? ? .RootFolder = Environment.SpecialFolder.MyComputer
? ? ? ? ? ? .ShowNewFolderButton = False
? ? ? ? End With
? ? ? ? If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
? ? ? ? ? ? txtFile.Text = FolderBrowserDialog1.SelectedPath
? ? ? ? End If
? ? End Sub
End Class
?
總結
以上是生活随笔為你收集整理的VB2010(18)_各种对话框的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Edittext焦点处理
- 下一篇: 监听键盘弹出高度