输入的报告 [英] Reports from inputs
问题描述
自从我上次使用VB已经有一段时间了,但是我需要为一家小型汽车旅馆做客房整理报告.基本上,我需要在打开程序时显示我们所有的房号,然后在它们旁边应该有复选框,以便我可以选择需要的房间 被清洁,然后我可以单击打印或创建报告,然后将其打印出来.我什至会开始编码呢?我做的最后一个程序是一些简单的税收计算,这很容易.但是在此应用程序上,我可以使用复选框来确定房间号 进行选择,但是在将其输出到报告以进行打印时,我会留下空白,有任何建议吗?
Its been a while since I last used VB, but i need to make a housekeeping report for a small motel. Basically i need all our room numbers displayed upon opening the program, then there should be check boxes next to them so i can select which rooms need to be cleaned then i can click print or create report then print it out. How would i even start coding this? the last program i did was some simple tax calculations which was pretty easy. But on this application, i can do the room numbers with the check boxes for selection, but im drawing a blank when it comes to outputting it to a report for printing it, any suggestions??
thx
推荐答案
嗨
这不像过去的那样简单明了,
This is not as straighforward as it was in 'the old days',
'
这是一些代码来说明.如果您选择尝试此操作,则需要启动一个新项目.这也会在页面上打印任何图形.
Here is some code to illustrate. If you choose to try this out, you will need to start a new Project. This will print any graphics on the page as well.
'
第1步:将新的类添加到项目中.复制/替换所有代码:
Step1: add a new Class to the Project. Copy/replace all code with this:
' https://support.microsoft.com/en-us/kb/811401
' https://support.microsoft.com/en-us/help/811401/how-to-print-the-content-of-a-richtextbox-control-by-using-visual-basic-.net-or-visual-basic-2005
' https://social.msdn.microsoft.com/Forums/vstudio/en-US/7777bae1-3610-43de-802c-2a9842cd6d2c/how-to-print-a-richtextbox-in-visual-basic-2013?forum=vbgeneral
Option Explicit On
Imports System.Runtime.InteropServices
Imports System.Drawing.Printing
Namespace RichTextBoxPrintCtrl
Public Class RichTextBoxPrintCtrl
Inherits RichTextBox
' Convert the unit that is used by the .NET framework (1/100 inch)
' and the unit that is used by Win32 API calls (twips 1/1440 inch)
Private Const AnInch As Double = 14.4
<StructLayout(LayoutKind.Sequential)>
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure CHARRANGE
Public cpMin As Integer ' First character of range (0 for start of doc)
Public cpMax As Integer ' Last character of range (-1 for end of doc)
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure FORMATRANGE
Public hdc As IntPtr ' Actual DC to draw on
Public hdcTarget As IntPtr ' Target DC for determining text formatting
Public rc As RECT ' Region of the DC to draw to (in twips)
Public rcPage As RECT ' Region of the whole DC (page size) (in twips)
Public chrg As CHARRANGE ' Range of text to draw (see above declaration)
End Structure
Private Const WM_USER As Integer = &H400
Private Const EM_FORMATRANGE As Integer = WM_USER + 57
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
' Render the contents of the RichTextBox for printing
' Return the last character printed + 1 (printing start from this point for next page)
Public Function Print(ByVal charFrom As Integer, ByVal charTo As Integer, ByVal e As PrintPageEventArgs) As Integer
' Mark starting and ending character
Dim cRange As CHARRANGE
cRange.cpMin = charFrom
cRange.cpMax = charTo
' Calculate the area to render and print
Dim rectToPrint As RECT
rectToPrint.Top = e.MarginBounds.Top * AnInch
rectToPrint.Bottom = e.MarginBounds.Bottom * AnInch
rectToPrint.Left = e.MarginBounds.Left * AnInch
rectToPrint.Right = e.MarginBounds.Right * AnInch
' Calculate the size of the page
Dim rectPage As RECT
rectPage.Top = e.PageBounds.Top * AnInch
rectPage.Bottom = e.PageBounds.Bottom * AnInch
rectPage.Left = e.PageBounds.Left * AnInch
rectPage.Right = e.PageBounds.Right * AnInch
Dim hdc As IntPtr = e.Graphics.GetHdc()
Dim fmtRange As FORMATRANGE
fmtRange.chrg = cRange ' Indicate character from to character to
fmtRange.hdc = hdc ' Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc ' Point at printer hDC
fmtRange.rc = rectToPrint ' Indicate the area on page to print
fmtRange.rcPage = rectPage ' Indicate whole size of page
Dim res As IntPtr = IntPtr.Zero
Dim wparam As IntPtr = IntPtr.Zero
wparam = New IntPtr(1)
' Move the pointer to the FORMATRANGE structure in memory
Dim lparam As IntPtr = IntPtr.Zero
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
Marshal.StructureToPtr(fmtRange, lparam, False)
' Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)
' Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam)
' Release the device context handle obtained by a previous call
e.Graphics.ReleaseHdc(hdc)
' Return last + 1 character printer
Return res.ToInt32()
End Function
End Class
End Namespace
第2步:重建项目
'
第3步:
在设计器的Form1中添加以下内容
Add the following to the Form1 in the Designer
'此示例需要具有以下内容的Form1:
'
'GroupBox1包含尽可能多的房间"
'根据需要选择复选框.
'
'Button1-打印上一页
'Button2-页面设置
'Button3-打印
'
'RichTextBoxPrintCtrl1-(重建后应该在工具箱中)
'
'PageSetUpDialog1-来自工具箱
'PrintPreviewDialog1-来自工具箱
'PrintDialog1-来自工具箱
'PrintDocument1-来自工具箱
' this example needs a Form1 with:
'
' GroupBox1 containing as many 'room'
' CheckBoxes as are needed.
'
' Button1 - Print Prevfiew
' Button2 - Page SetUp
' Button3 - Print
'
' RichTextBoxPrintCtrl1 - (should be in the toolbox after the rebuild)
'
' PageSetUpDialog1 - from toolbox
' PrintPreviewDialog1 - from toolbox
' PrintDialog1 - from toolbox
' PrintDocument1 - from toolbox
'
第4步:
使用以下命令复制/替换所有Form1代码:
Copy/replace all the Form1 code with this:
' this example needs a Form1 with:
' GroupBox1 containing as many 'room'
' CheckBoxes as are needed.
' Button1 - Print Prevfiew
' Button2 - Page SetUp
' Button3 - Print
' RichTextBoxPrintCtrl1 - (from Class RichTextBoxPrintCtrl)
' PageSetUpDialog1 - from toolbox
' PrintPreviewDialog1 - from toolbox
' PrintDialog1 - from toolbox
' PrintDocument1 - from toolbox
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PrintDialog1.Document = PrintDocument1
PrintPreviewDialog1.Document = PrintDocument1
PageSetupDialog1.Document = PrintDocument1
End Sub
Private checkPrint As Integer
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
checkPrint = 0
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' Print the content of the RichTextBox. Store the last character printed.
checkPrint = RichTextBoxPrintCtrl1.Print(checkPrint, RichTextBoxPrintCtrl1.TextLength, e)
' Look for more pages
If checkPrint < RichTextBoxPrintCtrl1.TextLength Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles Button1.Click
SetText()
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles Button2.Click
SetText()
PageSetupDialog1.ShowDialog()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles Button3.Click
SetText()
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub SetText()
RichTextBoxPrintCtrl1.Clear()
RichTextBoxPrintCtrl1.AppendText("This is the Header Text" & vbCrLf)
RichTextBoxPrintCtrl1.AppendText("Rooms requiring severe scrub down" & vbCrLf & vbCrLf)
For i As Integer = GroupBox1.Controls.Count - 1 To 0 Step -1
If GroupBox1.Controls(i).GetType() Is GetType(CheckBox) Then
If CType(GroupBox1.Controls(i), CheckBox).Checked Then
RichTextBoxPrintCtrl1.AppendText(CType(GroupBox1.Controls(i), CheckBox).Text & vbCrLf)
End If
End If
Next
End Sub
End Class
第5步:
重建项目-希望它可以编译.如果是这样,请运行并尝试.
Step 5:
Rebuild Project - hopefully it compiles OK. If so, run and try it out.
'
我希望我已包括所有必要的信息.如果我发布的内容有任何问题,代码中的几个链接可能会有所帮助.
I hope I have included all the necessary info. There are a couple of links in the code that may help if any problems with what I posted.
这篇关于输入的报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!