VB.net输出数据至Zebra打印机打印条码的程序Imports System.IO Imports System.TextPublic Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '判断sn输入是否正确 If Strings.Len(TextBox1.Text) = 11 Then Else MsgBox("Incorrect SN , please check again") TextBox1.Focus() TextBox1.SelectAll() Exit Sub End If '判断itemcode输入是否正确 If Strings.Len(TextBox3.Text) = 11 Then ElseIf Strings.Len(TextBox3.Text) = 12 Then ElseIf Strings.Len(TextBox3.Text) = 14 Then ElseIf Strings.Len(TextBox3.Text) = 15 Then Else MsgBox("Incorrect itemcode , please check again") TextBox3.Focus() TextBox3.SelectAll() Exit Sub End If 'sn赋值 Dim sn As String = "" sn = TextBox1.Text 'itemcode赋值 Dim itemcode As String = "" If Strings.Len(TextBox3.Text) = 15 OrElse Strings.Len(TextBox3.Text) = 12 Then itemcode = Strings.Left(TextBox3.Text, 7) & Strings.Right(TextBox3.Text, 5) Else itemcode = Strings.Left(TextBox3.Text, 7) & Strings.Right(TextBox3.Text, 4) End If '根据itemcode判断itemname Dim itemname As String = "" If itemcode = "082849A.101" Then itemname = "FSMB CORE" ElseIf itemcode = "082849A.102" Then itemname = "FSMB CORE" ElseIf itemcode = "082849A.103" Then itemname = "FSMB CORE" ElseIf itemcode = "082849A.104" Then itemname = "FSMB CORE" ElseIf itemcode = "082849A.105" Then itemname = "FSMB CORE" ElseIf itemcode = "083688A.105" Then itemname = "FRGD CORE" ElseIf itemcode = "083640A.207" Then itemname = "FRIB CORE" ElseIf itemcode = "083689A.101" Then itemname = "FRGC CORE" ElseIf itemcode = "083689A.102" Then itemname = "FRGC CORE" ElseIf itemcode = "083689A.103" Then itemname = "FRGC CORE" ElseIf itemcode = "083689A.104" Then itemname = "FRGC CORE" ElseIf itemcode = "083689A.105" Then itemname = "FRGC CORE" Else : MsgBox("The itemcode isn't in the database , Please check if input is incorrect or contact with you administrator.") Exit Sub End If '更改数据,发送打印 Dim serr As String = "" Dim outdata As String outdata = GetFileContents(WORKING_DIRECTORY_SCRIPT & "\script.txt", serr) If serr = "" Then If outdata <> "" Then outdata = outdata.Replace("$sn$", sn) outdata = outdata.Replace("$itemcode$", itemcode) outdata = outdata.Replace("$itemname$", itemname) Dim file_name As String = WORKING_DIRECTORY_SCRIPT & "\script-temp.txt" Dim fi As New System.IO.FileInfo(file_name) SaveTextToFile(outdata, file_name) CopyFile(file_name, "LPT1", 1) End If End If TextBox3.Clear() TextBox1.Focus() TextBox1.SelectAll() End Sub '更换数据 Public Const WORKING_DIRECTORY_SCRIPT As String = "C:\users\script" Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long Public Function printer(Optional ByRef sn As String = "", Optional ByRef itemcode As String = "", Optional ByRef itemname As String = "", Optional ByRef qty As String = "") As Boolean Dim serr As String = "" Dim outdata As String outdata = GetFileContents(WORKING_DIRECTORY_SCRIPT & "\script.txt", serr) If serr = "" Then If outdata <> "" Then outdata = outdata.Replace("$sn$", sn) outdata = outdata.Replace("$itemcode$", itemcode) outdata = outdata.Replace("$itemname$", itemname) Dim file_name As String = WORKING_DIRECTORY_SCRIPT & "\script-temp.txt" Dim fi As New System.IO.FileInfo(file_name) SaveTextToFile(outdata, file_name) CopyFile(file_name, "LPT1", 1) End If Return True End If Return False End Function '读取文件 Public Function GetFileContents(ByVal FullPath As String, Optional ByRef ErrInfo As String = "") As String Dim strContents As String Dim objReader As StreamReader Try objReader = New StreamReader(FullPath) strContents = objReader.ReadToEnd() objReader.Close() Return strContents Catch Ex As Exception ErrInfo = Ex.Message Return ErrInfo End Try End Function '保存文件 Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String, Optional ByVal ErrInfo As String = "") As Boolean Dim bAns As Boolean = False Dim objReader As StreamWriter Try objReader = New StreamWriter(FullPath) objReader.Write(strData) objReader.Close() bAns = True Catch Ex As Exception ErrInfo = Ex.Message End Try Return bAns End Function 'button1获取焦点后自动激发click响应 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.KeyPreview = True End Sub Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If Button1.Focused = True Then Me.Button1.PerformClick() End If End Sub 'button1获取焦点后自动激发click响应---向上 '以下为设置无标题栏拉动窗体 '注意:要设置form的controlbox属性为false Dim MousX As Integer Dim MousY As Integer Dim CurrX As Integer Dim CurrY As Integer Public Sub myMouseDown(ByVal sender As Object, ByVal ex As MouseEventArgs) Handles MyBase.MouseDown If ex.Button = MouseButtons.Left Then '如果是鼠标左键,则不捕获鼠标 Me.Capture() = False '获得鼠标坐标(相对于窗体) MousX = ex.X MousY = ex.Y End If End Sub Public Sub myMouseMove(ByVal sender As Object, ByVal ex As MouseEventArgs) Handles MyBase.MouseMove If ex.Button = MouseButtons.Left Then '左键操作 CurrX = Me.Left - MousX + ex.X CurrY = Me.Top - MousY + ex.Y '设置窗体相对于屏幕的坐标 Me.SetDesktopLocation(CurrX, CurrY) End If End Sub ' Public Sub myMouseUp(ByVal sender As Object, ByVal ex As MouseEventArgs) Handles MyBase.MouseUp If ex.Button = MouseButtons.Left Then Me.Capture() = True End If End Sub '以上为设置无标题栏拉动窗体 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub End Class
sBuf = "{PV01;1020,0190,0020,0030,B,00,B|}"
.Output = sBuf sCheckValue = Left("qty: " & Space(20), 17) sBuf = "{RV01;" & sCheckValue & sBundle & "|}"
.Output = sBuf
sBuf = "{XB01;1020,0195,3,1,01,01,03,03,02,0,0030|}"
.Output = sBuf
sBuf = "{RB01;" & sBundle & "|}"
.Output = sBuf
Imports System.TextPublic Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '判断sn输入是否正确
If Strings.Len(TextBox1.Text) = 11 Then
Else
MsgBox("Incorrect SN , please check again")
TextBox1.Focus()
TextBox1.SelectAll()
Exit Sub
End If
'判断itemcode输入是否正确
If Strings.Len(TextBox3.Text) = 11 Then
ElseIf Strings.Len(TextBox3.Text) = 12 Then
ElseIf Strings.Len(TextBox3.Text) = 14 Then
ElseIf Strings.Len(TextBox3.Text) = 15 Then
Else
MsgBox("Incorrect itemcode , please check again")
TextBox3.Focus()
TextBox3.SelectAll()
Exit Sub
End If 'sn赋值
Dim sn As String = ""
sn = TextBox1.Text 'itemcode赋值
Dim itemcode As String = ""
If Strings.Len(TextBox3.Text) = 15 OrElse Strings.Len(TextBox3.Text) = 12 Then
itemcode = Strings.Left(TextBox3.Text, 7) & Strings.Right(TextBox3.Text, 5)
Else
itemcode = Strings.Left(TextBox3.Text, 7) & Strings.Right(TextBox3.Text, 4)
End If '根据itemcode判断itemname
Dim itemname As String = "" If itemcode = "082849A.101" Then
itemname = "FSMB CORE" ElseIf itemcode = "082849A.102" Then
itemname = "FSMB CORE" ElseIf itemcode = "082849A.103" Then
itemname = "FSMB CORE" ElseIf itemcode = "082849A.104" Then
itemname = "FSMB CORE" ElseIf itemcode = "082849A.105" Then
itemname = "FSMB CORE" ElseIf itemcode = "083688A.105" Then
itemname = "FRGD CORE" ElseIf itemcode = "083640A.207" Then
itemname = "FRIB CORE" ElseIf itemcode = "083689A.101" Then
itemname = "FRGC CORE" ElseIf itemcode = "083689A.102" Then
itemname = "FRGC CORE" ElseIf itemcode = "083689A.103" Then
itemname = "FRGC CORE" ElseIf itemcode = "083689A.104" Then
itemname = "FRGC CORE" ElseIf itemcode = "083689A.105" Then
itemname = "FRGC CORE" Else : MsgBox("The itemcode isn't in the database , Please check if input is incorrect or contact with you administrator.")
Exit Sub
End If
'更改数据,发送打印
Dim serr As String = ""
Dim outdata As String
outdata = GetFileContents(WORKING_DIRECTORY_SCRIPT & "\script.txt", serr)
If serr = "" Then
If outdata <> "" Then
outdata = outdata.Replace("$sn$", sn)
outdata = outdata.Replace("$itemcode$", itemcode)
outdata = outdata.Replace("$itemname$", itemname)
Dim file_name As String = WORKING_DIRECTORY_SCRIPT & "\script-temp.txt"
Dim fi As New System.IO.FileInfo(file_name)
SaveTextToFile(outdata, file_name)
CopyFile(file_name, "LPT1", 1)
End If
End If TextBox3.Clear()
TextBox1.Focus()
TextBox1.SelectAll() End Sub '更换数据
Public Const WORKING_DIRECTORY_SCRIPT As String = "C:\users\script"
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long Public Function printer(Optional ByRef sn As String = "", Optional ByRef itemcode As String = "", Optional ByRef itemname As String = "", Optional ByRef qty As String = "") As Boolean Dim serr As String = ""
Dim outdata As String
outdata = GetFileContents(WORKING_DIRECTORY_SCRIPT & "\script.txt", serr)
If serr = "" Then
If outdata <> "" Then
outdata = outdata.Replace("$sn$", sn)
outdata = outdata.Replace("$itemcode$", itemcode)
outdata = outdata.Replace("$itemname$", itemname)
Dim file_name As String = WORKING_DIRECTORY_SCRIPT & "\script-temp.txt"
Dim fi As New System.IO.FileInfo(file_name)
SaveTextToFile(outdata, file_name)
CopyFile(file_name, "LPT1", 1)
End If
Return True
End If
Return False
End Function
'读取文件
Public Function GetFileContents(ByVal FullPath As String, Optional ByRef ErrInfo As String = "") As String Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(FullPath)
strContents = objReader.ReadToEnd()
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
Return ErrInfo
End Try
End Function '保存文件
Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String, Optional ByVal ErrInfo As String = "") As Boolean Dim bAns As Boolean = False
Dim objReader As StreamWriter
Try
objReader = New StreamWriter(FullPath)
objReader.Write(strData)
objReader.Close()
bAns = True
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
Return bAns
End Function
'button1获取焦点后自动激发click响应
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True End Sub Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If Button1.Focused = True Then
Me.Button1.PerformClick()
End If
End Sub
'button1获取焦点后自动激发click响应---向上 '以下为设置无标题栏拉动窗体
'注意:要设置form的controlbox属性为false
Dim MousX As Integer
Dim MousY As Integer
Dim CurrX As Integer
Dim CurrY As Integer Public Sub myMouseDown(ByVal sender As Object, ByVal ex As MouseEventArgs) Handles MyBase.MouseDown
If ex.Button = MouseButtons.Left Then
'如果是鼠标左键,则不捕获鼠标
Me.Capture() = False '获得鼠标坐标(相对于窗体)
MousX = ex.X
MousY = ex.Y
End If
End Sub Public Sub myMouseMove(ByVal sender As Object, ByVal ex As MouseEventArgs) Handles MyBase.MouseMove
If ex.Button = MouseButtons.Left Then
'左键操作
CurrX = Me.Left - MousX + ex.X
CurrY = Me.Top - MousY + ex.Y
'设置窗体相对于屏幕的坐标
Me.SetDesktopLocation(CurrX, CurrY)
End If
End Sub
'
Public Sub myMouseUp(ByVal sender As Object, ByVal ex As MouseEventArgs) Handles MyBase.MouseUp
If ex.Button = MouseButtons.Left Then
Me.Capture() = True End If
End Sub
'以上为设置无标题栏拉动窗体
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class