|
สร้างป้ายบาร์โค๊ตจากฟอร์น Code39 และ Crystal Report ด้วย Visual Basic 2013 |
บทความนี้จะนำเสนอแนวทางการสร้างบาร์โค๊ต โดยใช้ Font บาร์โค๊ต ซึ่งฟอร์นนี้มีชื่อว่า 3OF9_NEW.TTF ซึ่งเป็นฟอร์นเก่าดั้งเดิม คาดว่าน่าจะออกมานานแล้ว ซึ่งจะพัฒนาบน Visual Studio 2013 , .net 3.5 ทำคู่กับ Crystal Report For Visual Studio 2013 (หากใครยังไม่มีโหลดได้ที่นี่ https://www.aspsnippets.com/Articles/Download-Crystal-Reports-for-Visual-Studio-2013.aspx)
หลักการ จะทำการบันทึกข้อมูลลงในดาต้าเทเบิ้ล Z(DataTabel) และแสดงผลออกทาง Crystal Report โดย เพิ่ม * เข้าไปในส่วนหัวและท้ายของชุดตัวอักษรที่จะนำมาทำบาร์โค๊ต จากนั้นกำหนดให้แสดงผลโดยใช้ฟอร์น 3OF9_NEW.TTF ดังนั้นในเครื่องของผู้ใช้งานต้องติดตั้งฟอร์น 3OF9_NEW.TTF เข้าไปก่อน
เริ่มต้นด้วยการสร้าง Windows Form และ คอนโทรลข้อมาตารูปด้านล่าง
และสร้าง DataSet ขึ้นมา 1 ตัว ชื่อ DataSet1
จากนั้นสร้าง DataTabel ขึ้นมา 2 ตาราง ชื่อ txtLabel และ snlable_id ตามรูป
โค๊ตใน Form1
Code (VB.NET)
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Text
Public Class Form1
''' <summary>
''' สั่งปิด/เปิด คอนโทรลต่าง ๆ
''' </summary>
''' <remarks></remarks>
Private Sub rbCheck()
If rbOneNum.Checked = True Then
Me.NumericUpDown1.Enabled = True
Me.txtOneNum.Clear()
Me.txtOneNum.Enabled = True
Else
Me.txtOneNum.Clear()
Me.txtOneNum.Enabled = False
End If
If rbCount.Checked = True Then
Me.NumericUpDown1.Enabled = False
Me.txtCountStart.Enabled = True
Me.txtCountEnd.Enabled = True
Else
Me.NumericUpDown1.Enabled = True
Me.txtCountStart.Enabled = False
Me.txtCountEnd.Enabled = False
End If
If rbRanDom.Checked = True Then
Me.NumericUpDown1.Enabled = True
Me.cbbRandom.Enabled = True
Me.NumericUpDown2.Enabled = True
Else
Me.cbbRandom.Enabled = False
Me.NumericUpDown2.Enabled = False
End If
End Sub
Dim snum As String = "123456789" & _
"123456789" & _
"123456789" & _
"123456789" & _
"123456789"
Dim stext As String = "ABCDEFGHIJKLMNPQRSTUVWXYZ" & _
"ABCDEFGHIJKLMNPQRSTUVWXYZ" & _
"ABCDEFGHIJKLMNPQRSTUVWXYZ" & _
"ABCDEFGHIJKLMNPQRSTUVWXYZ" & _
"ABCDEFGHIJKLMNPQRSTUVWXYZ"
''' <summary>
''' ฟังก์ชั่นสุ่มตัวเลขและตัวอักษร
''' </summary>
''' <param name="s"></param>
''' <param name="n"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function snRnd(ByVal s As String, ByVal n As Integer)
Dim r As New Random
Dim sb As New StringBuilder
For i As Integer = 1 To n
Dim idx As Integer = r.Next(0, s.Length)
sb.Append(s.Substring(idx, 1))
Next
Return sb.ToString()
End Function
Private Sub rbCount_CheckedChanged(sender As Object,
e As EventArgs) Handles rbCount.CheckedChanged, rbOneNum.CheckedChanged, rbRanDom.CheckedChanged
Call rbCheck()
End Sub
Private Sub cbbRandom_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cbbRandom.KeyPress
'ปิด ComboBox ไม่ให้พิมพ์ได้
e.Handled = True
End Sub
Private Sub SNLabelFrm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Me.Dispose()
End Sub
Private Sub SNLabelFrm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'เพิ่มไอเท็มเข้า ComboBox
Me.cbbRandom.Items.Clear()
Me.cbbRandom.Items.Add("ตัวเลขและตัวอักษร")
Me.cbbRandom.Items.Add("ตัวเลข")
Me.cbbRandom.Items.Add("ตัวอักษร")
Me.cbbRandom.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'เคลียร์ตารางในดาต้าเทเบิ้ล textLabel
Me.DataSet11.textLabel.Rows.Clear()
'เพิ่มข้อมูลฉลากป้ายบาร์โค๊ตลงในดาต้าเทเบิ้ล
Me.DataSet11.textLabel.Rows.Add(Me.TextBox1.Text)
'เคลียร์ตารางในดาต้าเทเบิ้ล snlable_id
Me.DataSet11.snlable_id.Rows.Clear()
'ตรวจสอบเงื่อนไขตามการเลือก RadioButton
'rbOneNum คือ กำหนดให้ป้ายฉลากทั้งหมด เหมือนกันทุกอัน
If rbOneNum.Checked = True Then
For r As Integer = 0 To Me.NumericUpDown1.Value - 1
Dim newrowsss As DataSet1.snlable_idRow
newrowsss = DataSet11.snlable_id.NewRow
newrowsss.num_sn = Me.txtFrontNum.Text & Me.txtOneNum.Text
Me.DataSet11.snlable_id.Rows.Add(newrowsss)
Next
'rbCount คือ กำหนดให้ป้ายฉลากทั้งหมด เรียงตามลำดับตัวเลข เช่น 00001, 00002, 00003 ฯ
ElseIf rbCount.Checked = True Then
If Me.txtCountStart.Text = Nothing And Me.txtCountEnd.Text = Nothing Then
MessageBox.Show(Me, "กรุณาป้อนค่าตัวเลขเริ่มต้นและสิ้นสุดในช่องด้วย",
"ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
'เงื่อนไขการใส่ตัวเลขเรียงลำดับ
'txtCountStart.Text ต้องมีค่าน้อยกว่า txtCountEnd.Text เสมอ ถ้าไม่ใช่จะแสดงข้อความเตือน
If CInt(Me.txtCountStart.Text) > CInt(Me.txtCountEnd.Text) Then
MessageBox.Show(Me, "ตัวเลขในช่องเริ่มมีค่ามากกว่าช่องถึง" & vbNewLine & "กรุณาใส่ค่าช่องตัวเลขเริ่มต้น ให้มีค่าน้อยกว่าค่า สิ้นสุด",
"ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
Else
'หาค่าจำนวนการเรียงลำดับ
Dim ccc As Integer = CInt(Me.txtCountEnd.Text) - CInt(Me.txtCountStart.Text)
'เงื่อนไขถ้าเกิน 10000 จะแสดงข้อความเตือน (สร้างป้ายบาร์โค๊ตเยอะมากจะทำให้โปรแกรมทำงานช้าและอาจจะค้างได้ ฉะนั้นไม่เกินหมื่นก็น่าจะพอ)
If ccc > 10000 Then
MessageBox.Show(Me, "สร้างป้ายแยะเกินไปแล้วแก ! ขนาดเกินหมื่น มันใช้เวลานานนะ",
"สร้างทีล่ะนิด ทีละหน่อยนะ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
Else
'สร้างข้อมูลลงในดาต้าเทเบิ้ล
Dim num_start As Integer = Me.txtCountStart.Text
For r As Integer = CInt(Me.txtCountStart.Text) To CInt(Me.txtCountEnd.Text)
num_start = num_start + 1
Dim newrowsss As DataSet1.snlable_idRow
newrowsss = DataSet11.snlable_id.NewRow
newrowsss.num_sn = Me.txtFrontNum.Text & num_start
Me.DataSet11.snlable_id.Rows.Add(newrowsss)
Next
End If
End If
' rbRanDom คือ การส้รางรหัสบาร์โค๊ตจากการสุ่ม จาก ฟังก์ชั่น snRnd
ElseIf rbRanDom.Checked = True Then
'สร้างข้อมูลลงในดาต้าเทเบิ้ล
For r As Integer = 0 To Me.NumericUpDown1.Value - 1
Dim newrowsss As DataSet1.snlable_idRow
newrowsss = DataSet11.snlable_id.NewRow
Dim nr As String = String.Empty
If Me.cbbRandom.Text = "ตัวเลขและตัวอักษร" Then
nr = snRnd(snum & stext, Me.NumericUpDown2.Value)
ElseIf Me.cbbRandom.Text = "ตัวเลข" Then
nr = snRnd(snum, Me.NumericUpDown2.Value)
ElseIf Me.cbbRandom.Text = "ตัวอักษร" Then
nr = snRnd(stext, Me.NumericUpDown2.Value)
End If
Threading.Thread.Sleep(100)
newrowsss.num_sn = Me.txtFrontNum.Text & nr
Me.DataSet11.snlable_id.Rows.Add(newrowsss)
Next
End If
Dim cry As ReportDocument = New ReportDocument
'เลือกรีพอร์ต แบบที่มีป้ายฉากหรือไม่มีป้ายฉลาก
If Me.cbLable.Checked = True Then
cry.Load(Application.StartupPath & "\CrystalReport1.rpt")
Else
cry.Load(Application.StartupPath & "\CrystalReport2.rpt")
End If
cry.SetDataSource(Me.DataSet11)
CrystalReportViewer1.ReportSource = cry
cry.Refresh()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'สั่งพิมพ์ออกทางเครื่องพิมพ์
If Not CrystalReportViewer1.ReportSource Is Nothing Then
CrystalReportViewer1.PrintReport()
Else
MessageBox.Show(Me, "ยังไม่มีข้อมูลที่จะพิมพ์ โปรดกด 'แสดงตัวอย่าง'", "ไม่มีข้อมูล",
MessageBoxButtons.OK, MessageBoxIcon.None)
End If
End Sub
Private Sub txtCountStart_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCountStart.KeyPress
'ทำให้ TextBox พิมพ์ได้เฉพาะตัวเลขเท่านั้น
Select Case Asc(e.KeyChar)
Case 48 To 57 ' key โค๊ด ของตัวเลขจะอยู่ระหว่าง48-57ครับ 48คือเลข0 57คือเลข9ตามลำดับ
e.Handled = False
Case 8, 13, 127 ' ปุ่ม Backspace = 8,ปุ่ม Enter = 13, ปุ่มDelete = 46
e.Handled = False
Case Else
e.Handled = True
End Select
End Sub
Private Sub txtCountEnd_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCountEnd.KeyPress
'ทำให้ TextBox พิมพ์ได้เฉพาะตัวเลขเท่านั้น
Select Case Asc(e.KeyChar)
Case 48 To 57 ' key โค๊ด ของตัวเลขจะอยู่ระหว่าง48-57ครับ 48คือเลข0 57คือเลข9ตามลำดับ
e.Handled = False
Case 8, 13, 127 ' ปุ่ม Backspace = 8,ปุ่ม Enter = 13, ปุ่มDelete = 46
e.Handled = False
Case Else
e.Handled = True
End Select
End Sub
Private Sub NumericUpDown1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles NumericUpDown1.KeyPress
'ปิด NumericUpDown1 ไม่ให้ผู้ใช้สามารถพิมพ์อะไรลงไปได้
e.Handled = True
End Sub
Private Sub NumericUpDown2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles NumericUpDown2.KeyPress
'ปิด NumericUpDown2 ไม่ให้ผู้ใช้สามารถพิมพ์อะไรลงไปได้
e.Handled = True
End Sub
Private Sub cbLable_CheckedChanged(sender As Object, e As EventArgs) Handles cbLable.CheckedChanged
If Me.cbLable.Checked = True Then
Me.TextBox1.Enabled = True
Else
Me.TextBox1.Enabled = False
End If
End Sub
Private Sub txtFrontNum_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtFrontNum.KeyPress
'ตัวอักษรนำหน้า เมื่อมีอักษรนำหน้า จะไปบวก ลบ กับรหัสตัวเลข ซึ่งรวมกันต้องไม่เกิน 10 ตัวอักษร หากเกินอาจจะยาวจะเกินขอบฉลากบาร์โค๊ต ทำให้สแกนไม่ได้
Dim ml As Integer = 10 - (Me.txtFrontNum.Text.Length + 1)
Me.txtOneNum.MaxLength = ml
Me.txtCountStart.MaxLength = ml
Me.txtCountEnd.MaxLength = ml
Me.NumericUpDown2.Maximum = ml
End Sub
End Class
จากนั้นก็สร้าง Crystal Report ชื่อ CrystalReport1

เลือกเป็น Mail Label จากนั้นกด ok

เลือก Databel ที่สร้างไว้ จากนั้นกด Finish

กำหนดขนาดของป้าย ตามขนาดที่ต้องการ โดยผมตั้งขนาด 4 * 1.8 cm


จากนั้นสร้าง Formula Fields เพื่อที่จะใส่เครื่องหมาย * ลงไปในส่วนหัวและท้ายของตัวอักษร เช่น *12345678908*

โดยผมจะกำหนดชื่อของ Formula Fields เป็น star

จากนั้นดับเบิ้ลคลิ๊กที่ Datable ชื่อ num_sn ที่สร้างเอาไว้

จากนั้นเพิ่ม * เข้าไปที่หัวกับท้าย ตามรูป

จากนั้นกด บันทึก (มุมบนซ้ายของหน้าต่างโปรแกรม) ก็จะได้ Formula Fields ชื่อ star

ลากมาลงใน Crystal Report เลือกแบบอักษรเป็น 3 of 9 Barcode

จากนั้นลาก Databel ชื่อ num_sn ลงมาเพื่อทำเป็นอักษรกำกับด้านล่างของบาร์โค๊ต

ลาก text ลงมาเพื่อทำเป็นป้ายฉลากด้านบนของบาร์โค๊ต

จัดรูปแบบให้พอดี สวยงามตามต้องการ
เมื่อจะใช้งาน ต้องก็อปปี้ CrystalReport1.rpt ไปไว้ในโฟล์เดอร์ของโปรแกรมที่จะรันด้วย ผมได้ทำไว้ 2 ตัว คือ CrystalReport1.rpt และ CrystalReport2.rpt แบบไม่มีป้ายฉลากด้านบนบาร์โค๊ต

เสร็จเรียบร้อยพร้อมใช้งาน ผู้ใช้งานต้องปริ๊นต์ด้วยกระดาษสติ๊กเกอร์แบบปริ๊นต์ได้ ถ้าต้องการนำไปติดกับสินค้าต่าง ๆ
จะให้คม สแกนง่าย ๆ ต้องเป็นปริ๊นต์เตอร์เลเซอร์จึงจะดี

ซอร์ทโค๊ต นำไปดูเป็นแนวทางประยุกต์ใช้งานกันต่อไป (VB.net, .net3.5, Crystal Report For Visual Studio 2013)
https://drive.google.com/file/d/0B0tm4MRb-kmFTVZWVEdRRkpVcTQ/view?usp=sharing
Download!!!

|
|
|
|
 |
|
|
|
By : |
pakyaudio
|
|
Article : |
บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ |
|
Score Rating : |
   |
|
Create Date : |
2017-06-10 |
|
Download : |
No files |
|
Sponsored Links |
|
|
|
|
|
|