如何使用表单名称作为变量使用公共子过程 [英] How to use a public sub procedure using a form name as the variable
问题描述
我有大量需要打印的表格。我已经为每个表单添加了一个按钮,可以触发表单的打印(PDF格式)。我想创建一个可以在选择按钮时调用的常用过程,而不是为每个表单重复代码。我的下面的代码会产生错误!
我们将非常感谢您的帮助。
我是什么尝试过:
在我的申请表中,我有许多表格可供我打印。所以在每个表单上我都有一个按钮来创建打印。
按钮背后的代码如下;
I have a large number of forms that need to be printed. I have added a button to each form that can trigger the printing of the form (in PDF format). Rather than repeat the code for every form, I want to create a common procedure that can be called when the button is selected. My code below creates errors!
Any help would be gratefully received.
What I have tried:
In my application I have a number of forms that I wish to print. So on each form I have a button to create the print.
The code behind the button is as follows;
Private Sub PDFTeamBut_Click(sender As Object, e As EventArgs) Handles PDFTeamBut.Click
PDFTeamBut.Visible = False
PrtTSBut.Visible = False
Array.ForEach(Me.Controls.OfType(Of TextBox).ToArray, Sub(tb As TextBox) tb.DeselectAll())
PrintForm1.PrinterSettings.PrinterName = "Microsoft Print to PDF"
PrintForm1.PrintAction = Printing.PrintAction.PrintToPrinter
If PrintForm1.PrinterSettings.IsValid Then
PrintForm1.PrinterSettings.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("PaperA4", 830, 1170)
PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = False
PrintForm1.PrinterSettings.DefaultPageSettings.Margins = New System.Drawing.Printing.Margins(3.2, 3.2, 1.5, 14.5)
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
Else
MessageBox.Show("Microsoft Print to PDF printer not set up!")
End If
PDFTeamBut.Visible = True
PrtTSBut.Visible = True
Me.Hide()'Hides form to be printed
FormName.InputForm.Show()'Returns to input form
End Sub
我想通过调用Public Sub Procedure替换代码;例如
I want to replace the code by calling up a Public Sub Procedure; such as
Private Sub PDFTeamBut_Click(sender As Object, e As EventArgs) Handles PDFTeamBut.Click
CreatePDF(NameOfForm)
End Sub
使用公共子程序;
With a Public Sub Procedure ;
Public Class PDFPrint
Public Sub CreatePDF(ByVal FormName As Form)
'Creates a PDF file of the named form
FormName.PDFTeamBut.Visible = False
FormName.PrtTSBut.Visible = False
Array.ForEach(FormName.Controls.OfType(Of TextBox).ToArray, Sub(tb As TextBox) tb.DeselectAll())
FormName.PrintForm1.PrinterSettings.PrinterName = "Microsoft Print to PDF"
FormName.PrintForm1.PrintAction = Printing.PrintAction.PrintToPrinter
If FormName.PrintForm1.PrinterSettings.IsValid Then
FormName.PrintForm1.PrinterSettings.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("PaperA4", 830, 1170)
FormName.PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = False
FormName.PrintForm1.PrinterSettings.DefaultPageSettings.Margins = New System.Drawing.Printing.Margins(3.2, 3.2, 1.5, 14.5)
FormName.PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
Else
MessageBox.Show("Microsoft Print to PDF printer not set up!")
End If
FormName.PDFTeamBut.Visible = True
FormName.PrtTSBut.Visible = True
FormName.Hide()'Hides form to be printed
InputForm.Show()'Returns to input form
End Sub
End Class
但我收到以下编码错误,例如:
But I get the following coding errors such as;
Error BC30456 'PDFTeamBut' is not a member of 'Form'.
和
and
Error BC30456 'PrintForm1' is not a member of 'Form'.
推荐答案
自发布问题以来,我在互联网上找到了一些信息,让我改变了以下几行
Since posting the problem I have found some information on the internet that made me change the following lines
FormName.PDFTeamBut.Visible = False
FormName.PrtTSBut.Visible = False
和
and
FormName.PDFTeamBut.Visible = True
FormName.PrtTSBut.Visible = True
to
to
FormName.Controls("PDFTeamBut").Visible = False
FormName.Controls("PrtTSBut").Visible = False
和
and
FormName.Controls("PDFTeamBut").Visible = True
FormName.Controls("PrtTSBut").Visible = True
这更正了第一个错误。
仍然有PrintForm1错误的问题。
This corrected the first error.
Still have a problem with the PrintForm1 error.
这篇关于如何使用表单名称作为变量使用公共子过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!