我希望你能帮助我弄清楚我的应用程序发生了什么。我在VB.net 2010中编写了一个标签打印窗口应用程序。该应用程序运行良好,直到我修改它以允许用户指定他们一次需要打印的标签数量(我只允许1到20之间的数字)。我使用NumericUpDown控件获得标签(副本)的数量。
在打印标签之前,我会检查用户是否指定了超过20个标签。如果他们有,我会专注于控制并要求他们将所需的副本减少到至少最大数量。
If Me.nudPrintQty.Text > 20 Then
MsgBox("You cannot print more than 20 tags at a time.", MsgBoxStyle.Exclamation, "Maximum Value Specified")
Me.nudPrintQty.Focus()
Me.nudPrintQty.Select(0, Me.nudPrintQty.Value.ToString.Length)
Validrecord = False
Exit Sub
End If
当控制器获得或失去控制或按下回车键时,我也会对控件执行以下操作:
Private Sub nudPrintQty_GotFocus(sender As Object, e As System.EventArgs) Handles nudPrintQty.GotFocus
Me.nudPrintQty.StateCommon.Back.Color1 = Color.Yellow
Me.nudPrintQty.Select(0, Me.nudPrintQty.Value.ToString.Length)
End Sub
Private Sub nudPrintQty_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles nudPrintQty.KeyDown
'Allow select keys without Autocompleting
Select Case e.KeyCode
Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down
Return
End Select
If e.KeyCode = Keys.Enter Then
'nudPrintQty.Focus
Me.btnSave_Click(sender, New System.EventArgs())
e.SuppressKeyPress = True
End If
End Sub
Private Sub nudPrintQty_LostFocus(sender As Object, e As System.EventArgs) Handles nudPrintQty.LostFocus
Me.nudPrintQty.StateCommon.Back.Color1 = Color.White
End Sub
如果用户完成了所有必需的值并且所需的标签数量符合规范,那么我按如下方式调用打印程序:
Private Sub PrintTag()
path = System.IO.Path.GetTempPath()
strfileName = "RMS.txt"
FileOpen(1, path & strfileName, OpenMode.Output) ' Open file for output.
PrintLine(1, Chr(2) & "qA")
PrintLine(1, Chr(2) & "XA")
PrintLine(1, Chr(2) & "M3000")
PrintLine(1, Chr(2) & "c0000")
PrintLine(1, Chr(2) & "e")
PrintLine(1, Chr(2) & "O0215")
PrintLine(1, Chr(2) & "f255")
PrintLine(1, Chr(2) & "D" & Chr(2) & "L")
PrintLine(1, Chr(2) & "L")
PrintLine(1, "D11")
PrintLine(1, "H15")
'------------------------------------------------------------------------
' Title Start
'------------------------------------------------------------------------
PrintLine(1, "2901S000499034000440105" & "Company Name")
PrintLine(1, "2901S000489032000240050" & "Country")
PrintLine(1, "ySPM")
PrintLine(1, "2901S000759021002340208" & "---- Ok To RELEASE ----")
PrintLine(1, "ySPM")
'------------------------------------------------------------------------
' Title End
'------------------------------------------------------------------------
'------------------------------------------------------------------------
' Column 1 Start
'------------------------------------------------------------------------
PrintLine(1, "2901S000739016000940050CODE:_______________________")
PrintLine(1, "ySPM")
PrintLine(1, "2901S000739010000940050BATCH / NO.:_________________")
PrintLine(1, "ySPM")
PrintLine(1, "2901S000739004000940050MC NO.:_____________________")
PrintLine(1, "ySPM")
'------------------------------------------------------------------------
' Column 1 End
'------------------------------------------------------------------------
'------------------------------------------------------------------------
' Column 1 Text Start
'------------------------------------------------------------------------
PrintLine(1, "2901S000589016000940050" & strCode)
PrintLine(1, "ySPM")
PrintLine(1, "2901S000589010000940050" & strBatchLotNo)
PrintLine(1, "ySPM")
PrintLine(1, "2901S000589004000940050" & strMCNo)
PrintLine(1, "ySPM")
'------------------------------------------------------------------------
' Column 1 Text End
'------------------------------------------------------------------------
'------------------------------------------------------------------------
' Column 2 Start
'------------------------------------------------------------------------
PrintLine(1, "2901S000349016000940050DATE:______________________")
PrintLine(1, "ySPM")
PrintLine(1, "2901S000349010000940050QTY:_______________________")
PrintLine(1, "ySPM")
PrintLine(1, "2901S000349004000940050SIGN:______________________")
PrintLine(1, "ySPM")
'------------------------------------------------------------------------
' Column 2 End
'------------------------------------------------------------------------
'------------------------------------------------------------------------
' Column 2 Text Start
'------------------------------------------------------------------------
If strReprint = "Y" Then
PrintLine(1, "2901S000249016000940050" & strR_date)
strReprintDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
Else
PrintLine(1, "2901S000249016000940050" & Me.dtpReleaseDate.Text)
strReprintDate = Me.dtpReleaseDate.Text
End If
PrintLine(1, "ySPM")
PrintLine(1, "2901S000249010000940050" & decQTY)
PrintLine(1, "ySPM")
PrintLine(1, "2901S000349000000640030Label Print Date: " & strReprintDate)
PrintLine(1, "ySPM")
'------------------------------------------------------------------------
' Column 2 Text End
'------------------------------------------------------------------------
'------------------------------------------------------------------------
' Close File Start
'------------------------------------------------------------------------
PrintLine(1, "Q0001")
PrintLine(1, "E")
FileClose(1)
'------------------------------------------------------------------------
' Close File End
'------------------------------------------------------------------------
'------------------------------------------------------------------------
' Print the Tag List
'------------------------------------------------------------------------
Dim retval As String
Dim counter As Integer = 0
'Dim hProcess As Long
'Dim taskId As Long
'Dim cmdline As String
Do While counter <= nudPrintQty.Value
'cmdline = "print /D:\\Server\Printer " & path & strfileName
'taskId = Shell(cmdline, vbNormalFocus)
'hProcess = OpenProcess(SYNCHRONIZE, True, taskId)
'Call WaitForSingleObject(hProcess, WAIT_INFINITE)
'CloseHandle(hProcess)
retval = Shell("print /D:\\" & Trim(Server) & "\" & Trim(PRINTER) & " " & path & strfileName)
counter += 1
Loop
If strReprint = "Y" Then
UpdateReprintDate()
MsgBox("Print out Completed. Click Ok to Continue.", MsgBoxStyle.Information, "Reprint Complete")
End If
End Sub
在Do语句中是我的问题。当我作为用户指定5个标签时,我得到4,当我要求4时,我得到3,有时我要求1得到2,有时我要求4得到2或者甚至1.当我单步执行代码时,每次打印正确的份数。我有点认为它与shell打印的时间有关。也许它没有机会在下一个作业发送之前完成打印,所以我在网上查看并获得了WaitForSingleObject方法,但这也没有帮助。我不确定我是否正确实现了它。
我希望周围的专家可以帮助我弄清楚我的代码出了什么问题,因为自从周一以来我一直试图找到解决方案。
提前致谢。