VB form的保存和打印

如图,点击打印可以实现打印,退出时可以保存Form中text的内容,重新加载时又可以加载上次保存的内容该怎样编写代码实现呢?本人菜鸟,不胜感激!... 如图,点击打印可以实现打印,退出时可以保存Form中text的内容,重新加载时又可以加载上次保存的内容该怎样编写代码实现呢?本人菜鸟,不胜感激! 展开
 我来答
育知同创教育
2016-01-31 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
VB form中保存和打印的都是调用系统提供的api。
实现的完整例子如下:
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Drawing.Printing
Imports System.Windows.Forms

Class Form1
Inherits Form
Private WithEvents printPreviewButton As Button

Private printPreviewDialog1 As New PrintPreviewDialog()
Private WithEvents printDocument1 As New PrintDocument()

' Declare a string to hold the entire document contents.
Private documentContents As String

' Declare a variable to hold the portion of the document that
' is not printed.
Private stringToPrint As String

Public Sub New()
Me.printPreviewButton = New System.Windows.Forms.Button()
Me.printPreviewButton.Location = New System.Drawing.Point(12, 12)
Me.printPreviewButton.Size = New System.Drawing.Size(125, 23)
Me.printPreviewButton.Text = "Print Preview"
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.printPreviewButton)

End Sub

Private Sub ReadDocument()
Dim docName As String = "testPage.txt"
Dim docPath As String = "c:\"
printDocument1.DocumentName = docName
Dim stream As New FileStream(docPath + docName, FileMode.Open)
Try
Dim reader As New StreamReader(stream)
Try
documentContents = reader.ReadToEnd()
Finally
reader.Dispose()
End Try
Finally
stream.Dispose()
End Try
stringToPrint = documentContents

End Sub

Sub printDocument1_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage

Dim charactersOnPage As Integer = 0
Dim linesPerPage As Integer = 0

' Sets the value of charactersOnPage to the number of characters
' of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _
StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

' Draws the string within the bounds of the page.
e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _
e.MarginBounds, StringFormat.GenericTypographic)

' Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage)

' Check to see if more pages are to be printed.
e.HasMorePages = stringToPrint.Length > 0

' If there are no more pages, reset the string to be printed.
If Not e.HasMorePages Then
stringToPrint = documentContents
End If

End Sub

Private Sub printPreviewButton_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles printPreviewButton.Click

ReadDocument()
printPreviewDialog1.Document = printDocument1
printPreviewDialog1.ShowDialog()
End Sub

<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())

End Sub
End Class
以上实现方法:继承了printPreviewButton ,实现了里面的ReadDocument和printDocument1_PrintPage实现了打印。
满以柳J7
2015-07-10 · TA获得超过539个赞
知道小有建树答主
回答量:545
采纳率:66%
帮助的人:555万
展开全部
可以利用vb 中读写 ini 文件的方法或写注册表方法来保存临时数据.

以读写ini 文件为例:

'定义ini 文件变量 并给出文件的路径及名称
Public iniFileName As String
iniFileName = "C:\test.ini"

' 按“打印”按扭时,保存当前数据
'以数字型数据为例保存数据
dim tParam as double
dim Res as long

tParam=format(Text1,"#0.00")
Res = WritePrivateProfileString("PARAM", "Text1", Str$(tParam), iniFileName)

tParam=format(Text2,"#0.00")
Res = WritePrivateProfileString("PARAM", "Text2", Str$(tParam), iniFileName)

tParam=format(Text3,"#0.00")
Res = WritePrivateProfileString("PARAM", "Text3", Str$(tParam), iniFileName)

tParam=format(Text4,"#0.00")
Res = WritePrivateProfileString("PARAM", "Text4", Str$(tParam), iniFileName)

'读取保存数据并显示在 TextBox 中
dim rData as String

dim rRet as Long
'这里要注意数据的长度,设置10位长度
rData=Space$(10)
rRet = GetPrivateProfileString(" PARAM", "Text1", "", rData, 10, iniFileName)
Text1=Format$(rData,"#0.00")

rRet = GetPrivateProfileString(" PARAM", "Text2", "", rData, 10, iniFileName)

Text2=Format$(rData,"#0.00")

rRet = GetPrivateProfileString(" PARAM", "Text3", "", rData, 10, iniFileName)

Text3=Format$(rData,"#0.00")

rRet = GetPrivateProfileString(" PARAM", "Text4", "", rData, 10, iniFileName)

Text4=Format$(rData,"#0.00")

读写注册表的方法,自己学习一下了...
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友cc16d5c
2015-07-10 · TA获得超过731个赞
知道小有建树答主
回答量:647
采纳率:50%
帮助的人:537万
展开全部
Private Sub Command1_Click() '载入
Dim txt(1 To 4) As String
Dim sFileName As String, iFileNumber As Integer
Dim i As Integer

iFileNumber = FreeFile
sFileName = App.Path & "\Text.txt"
If Dir(sFileName) <> "" Then
Open sFileName For Input As #iFileNumber
For i = 1 To 4
If EOF(iFileNumber) Then Exit For
Input #iFileNumber, txt(i)
Next
Close #iFileNumber
Text1 = txt(1)
Text2 = txt(2)
Text3 = txt(3)
Text4 = txt(4)
Else
MsgBox "文件不存在,未能载入数据。", vbInformation, "载入数据"
End If
End Sub

Private Sub Command2_Click() '保存
Dim sFileName As String, iFileNumber As Integer
Dim i As Integer

iFileNumber = FreeFile
sFileName = App.Path & "\Text.txt"
Open sFileName For Output As #iFileNumber
Write #iFileNumber, Text1
Write #iFileNumber, Text2
Write #iFileNumber, Text3
Write #iFileNumber, Text4
Close #iFileNumber
MsgBox "保存已完成", vbInformation, "保存数据"
End Sub

Private Sub Command3_Click() '打印
With Printer
.FontSize = 12
.Copies = 1 '打印份数
.ColorMode = vbPRCMMonochrome
Printer.Print Text1
Printer.Print Text2
Printer.Print Text3
Printer.Print Text4
.EndDoc
End With
End Sub
来自:求助得到的回答
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式