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)_各种对话框的使用相关推荐

  1. duilib combo增加搜索栏_微信对话框上线搜一搜,搜索一步到位!

    关注[玩机小公举] 星标公众号 最新技巧,第一时间送达 前两天微信内测了新功能,据透露,今天将会在对话框全量上线搜一搜功能.注意这不是[发现页]-[搜一搜]而是在聊天对话框,直接增加了快速搜索的入口. ...

  2. vs如何设置对话框显示在最前面_“打开”对话框的使用

    原文链接: No.20 "打开"对话框的使用​mp.weixin.qq.com 打开 对话框 我们昨天熟悉了"字体"对话框,今天我们来看一下"打开&q ...

  3. 如何建立MFC绘图工程:外貌框架_基于对话框(开发平台VS2017)

    对于学习MFC的初学者,入门的步骤很重要,新建一个基于对话框的MFC应用程序,并通过拖拉按钮等,初步搭建自己的MFC工程. 工具/原料 笔记本电脑 VS2017 方法/步骤 1.打开VS2017,并新 ...

  4. 前端将时间格式‘2020-03-03T16:49:18.000+0000‘转化成正常格式‘2020-03-03 16:49:18‘ _@jie

    直接在前端对于原来的时间格式进行工具类转化: function renderTime(date) {var dates = new Date(date).toJSON();return new Dat ...

  5. 反爬虫兵法演绎18 _ 如何搞定老板(上):如何编造虚无缥缈的OKR?

    18 | 如何搞定老板(上):如何编造虚无缥缈的OKR? 你好,我是DS Hunter. 众所周知,每个项目创建的时候,都要频繁的设定目标,即使这些目标每天都在变化,刚设置完就已经没用了.但是,上层的 ...

  6. python打开文件夹对话框_文件对话框打开文件夹中的文件(tkinter)

    我想把它实现到我自己的代码中,但是当我运行这个(没有我的代码,只有你看到的代码)时,所有显示的文件夹都是空的,我实际上不能打开任何东西.在from tkinter import * from tkin ...

  7. QtQuick PC端开发实战系列(18)_自定义Switch

    总目录传送门 本博文技术等级: ★★☆☆☆☆☆☆☆☆ 我们先看看原始的switch什么效果 https://doc.qt.io/qt-5/qml-qtquick-controls2-switch.ht ...

  8. VB2010(19)_菜单工具栏上下文菜单

    Public Class Menus     '窗体载入     Private Sub Menus_Load(ByVal sender As System.Object, ByVal e As Sy ...

  9. Ruby学习笔记(18)_冒号用法

    What is the difference between: 1) abc: 2) :xyz 3) Abc::Xyz 4) abc: :xyz 5) abc: xyz 6) :abc => x ...

  10. progressdialog进度框_进度对话框 ProgressDialog 用法总结

    ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog public class ProgressDialog extends AlertDialog P ...

最新文章

  1. 在电脑上实现手机app抓包
  2. SAP MM 采购订单收货被取消了还是不能增加新的delivery cost!
  3. 瞒不住了,难怪.NET进大厂这么难!
  4. 前端小插件之手写js循环滚动特效
  5. Git 新建仓库推送远程技巧
  6. php开发视频播放顺序,请问关于php代码运行顺序问题
  7. python支持多种编程范式吗_Python3学习之路~6.1 编程范式:面向过程 VS 面向对象...
  8. sqlserver 查询一个表的所有字段代码
  9. vue2.0中ckeckbox(复选框)的使用心得,及对click事件和change的理解
  10. Hadoop HDFS (3) JAVA訪问HDFS
  11. sqlldr导入数据(以PostgreSqlOracle为例)
  12. mac+ffmpeg+php,mac折腾安装ffmpeg小记
  13. cocoaPods:公有库私有库
  14. python 反编译exe文件为py文件
  15. 【Scratch】青少年蓝桥杯_每日一题_3.01_画莲花
  16. 单片机广告灯实验总结_关于单片机的一些小实验lowbar;01点亮一个灯
  17. mysql 数据快速删除
  18. 计算机教师的应用计划书,教师信息技术个人提升计划
  19. Squeeze-and-Excitation Networks论文翻译——中文版
  20. 三星 android 备忘录,便捷工具组件:三星Note S备忘录迎升级

热门文章

  1. Linux多线程编程-线程间参数传递
  2. 怎样打开.jar格式文件,怎样运行.jar格式文件
  3. UML 简单易懂 教程
  4. 华为BIOS系统升级
  5. 教大家一个免费复制粘贴百度文库文字的方法
  6. 企业项目管理软件介绍
  7. C3P0连接池的基本配置与使用
  8. 计算机视觉应用期末试卷,计算机视觉期末复习
  9. DevExpress Windows Form(1) DevExpress控件之主题
  10. 程序员常识--OJ系统及ACM测试题库大全