การ Generate number ร่วมกับตัวอักษร 001-ZZZ
แก้ไขครั้งสุดท้ายโดย Soutk เมื่อ 2019-5-10 21:14มีสมาชิกท่านใด มีวิธี Generate จำนวน ตามตัวอย่างด้านล่างมั้ยครับ
ขอคำแนะนำด้วยครับ
001-999 A01-A99 B01-B99 AB1-AB9 etc...............ZZZ
โจทย์การบ้าน ผมก็จะตอบให้ครับ เพราะมีพื้นฐานอยู่เรื่องหนึ่งที่สำคัญคือเรื่อง ASCII Code ... ลาก DataGridView เข้ามาวางบนฟอร์ม ปรับแค่ Anchor ให้ขยายได้ทุกทิศทาง ที่เหลือผมใช้โค้ดแบบ Run Time และปุ่มคำสั่งอีก 4 ตัว ... Public Class frmGenerateCode
Private Sub frmGenerateCode_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Call SetupDataGridView(Me.DataGridView1)
End Sub
' / --------------------------------------------------------------------------
' / Organized DataGridView.
Private Sub SetupDataGridView(ByRef DGV As DataGridView)
With DGV
.Columns.Clear()
.Rows.Clear()
.RowTemplate.Height = 26
.AllowUserToOrderColumns = True
.AllowUserToDeleteRows = False
.AllowUserToAddRows = False
.ReadOnly = True
.MultiSelect = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.Font = New Font("Tahoma", 8)
.AlternatingRowsDefaultCellStyle.BackColor = Color.OldLace
.DefaultCellStyle.SelectionBackColor = Color.SeaGreen
'.AlternatingRowsDefaultCellStyle.BackColor = Color.LightYellow
'.DefaultCellStyle.SelectionBackColor = Color.LightBlue
'/ Auto size column width of each main by sorting the field.
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.AutoResizeColumns()
Dim Column1 As New DataGridViewTextBoxColumn
With Column1
.DataPropertyName = "Code"
.Name = "Code"
.HeaderText = "Code"
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
End With
.Columns.Add(Column1)
End With
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Call SetupDataGridView(Me.DataGridView1)
'001-999
For i As Int32 = 1 To 999
'// จัดรูปแบบโดยใช้ 3 หลัก "000" & 1 = "0001" แต่เราใช้ Right เพื่อให้นับจากทางขวามา 3 หลัก Right("0001", 3) = "001"
Dim row = New String() {Microsoft.VisualBasic.Right("000" & i, 3)}
DataGridView1.Rows.Add(row)
Next
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Call SetupDataGridView(Me.DataGridView1)
' A01-A99 B01-B99 ... Z99
For i As Byte = 65 To 90 '// ASCII Code A = 65, Z = 90
For j As Byte = 1 To 99
'// Chr(ค่า Ascii Code) เช่น Chr(65) = A, Chr(90) = Z
Dim row = New String() {Chr(i) & Microsoft.VisualBasic.Right("00" & j, 2)}
DataGridView1.Rows.Add(row)
Next
Next
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Call SetupDataGridView(Me.DataGridView1)
' AB1-AB9 ... AZ9
For i As Byte = 65 To 90 '// ASCII Code A = 65, Z = 90
For j As Byte = 66 To 90
For k As Byte = 1 To 9
Dim row = New String() {Chr(i) & Chr(j) & k}
DataGridView1.Rows.Add(row)
Next
Next
Next
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
Call SetupDataGridView(Me.DataGridView1)
' AAA-ZZZ
For i As Byte = 65 To 90 '// ASCII Code A = 65, Z = 90
For j As Byte = 65 To 90
For k As Byte = 65 To 90
Dim row = New String() {Chr(i) & Chr(j) & Chr(k)}
DataGridView1.Rows.Add(row)
Next
Next
Next
End Sub
End Class
thongkorn ตอบกลับเมื่อ 2019-5-11 15:52
โจทย์การบ้าน ผมก็จะตอบให้ครับ เพราะมีพื้นฐานอยู่เรื ...
ขอบคุณอาจารย์มากๆเลยครับ
หน้า:
[1]