我前段时间用vb.net写了一个条形码程序,当最终用户点击打印按钮时,程序将打开包含ZPL条形码的文本文件,将序列号加1,将新的序列号写回文本文件,然后将预定数量的条码打印到斑马打印机上。
现在我正在尝试重写代码,以便它将最终用户从他们在 PrintDialog 中键入的数字中打印出他们想要的条形码数量。在我更新的代码中,我尝试放入一个 DO UNTIL ......LOOP。它将打印出用户放入 PrintDialog 框中的条码数量,但不会序列化。我的意思是如果用户在 PrintDialog 中输入 5,程序将打印序列号 01 五次,而不是打印 02、03、04、05、06。
谁能给我一些关于如何根据用户输入到 PrintDialog 的数字让程序读取、写入和打印所需的 x 次的指示?
这是我的原始代码:
Dim sL() As String = IO.File.ReadAllLines("C:\Labels\loop test.txt")
Dim CurrentBar(2) As String
For i = 0 To 2
CurrentBar(i) = sL(sL.Length - 3 + i)
Next
If CurrentBar(1).Length >= 28 Then
CurrentBar(1) = CurrentBar(1).Substring(0, 18) & Format(CInt(CurrentBar(1).Substring(18, 10) + 1), "0000000000") & CurrentBar(1).Substring(28)
Else
MessageBox.Show("String is not long enough!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
'Write the updated vaules back to the Text File
IO.File.WriteAllText("C:\Labels\loop test.txt",
CurrentBar(0) & vbNewLine &
CurrentBar(1) & vbNewLine &
CurrentBar(2))
Dim psi As New ProcessStartInfo
psi.UseShellExecute = True
psi.Verb = "print"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = "C:\Labels\loop test.txt"
Process.Start(psi)
这是我更新的代码:
Dim PrintDialog1 As New PrintDialog()
Dim psi As New ProcessStartInfo
Dim sL() As String = IO.File.ReadAllLines("C:\Labels\loop test.txt")
Dim CurrentBar(2) As String
Do Until PrintDialog1.PrinterSettings.Copies - 1
For i = 0 To 2
CurrentBar(i) = sL(sL.Length - 3 + i)
Next
If CurrentBar(1).Length >= 28 Then
CurrentBar(1) = CurrentBar(1).Substring(0, 18) & Format(CInt(CurrentBar(1).Substring(18, 10) + 1), "0000000000") & CurrentBar(1).Substring(28)
Else
MessageBox.Show("String is not long enough!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
'Write the updated vaules back to the Text File
IO.File.WriteAllText("C:\Labels\loop test.txt",
CurrentBar(0) & vbNewLine &
CurrentBar(1) & vbNewLine &
CurrentBar(2))
'Print the labels
'Get the number of copies to print
If (PrintDialog1.ShowDialog() = DialogResult.OK) Then
'print desired number of copies to target printer
For I = 0 To PrintDialog1.PrinterSettings.Copies - 1
psi.UseShellExecute = True
psi.Verb = "print"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
psi.FileName = "C:\Labels\loop test.txt"
Process.Start(psi)
Next
End If
Loop
这是我的文本文件:
^XA
^LH20,85^BY3^AE^SN0000000001R,,Y^B3N,,60,,Y^FS
^XZ