AlphaNumeric.vb class:
Imports System.Collections.Generic
Imports System.Text
Namespace INCREMENT_IN_C_SHARP
Public Enum SequenceType
''' <summary>
''' 00,01,...,09,0A,...0Z,10,11...,A0,A1,...,ZZ
''' </summary>
NumericToAlpha = 1
''' <summary>
''' AA,AB,...,AZ,A0,...A9,BA,BB...ZZ,00,01,...99
''' </summary>
AlphaToNumeric = 2
''' <summary>
''' A0,A1,...,A9,AA,...AZ,B0,B1...ZZ,00,01,...99
''' </summary>
AlphaNumeric = 3
''' <summary>
''' 00,01,...99
''' </summary>
NumericOnly = 4
''' <summary>
''' AA,AB,...,ZZ
''' </summary>
AlphaOnly = 5
End Enum
Public NotInheritable Class AlphaNumeric
Private Sub New()
End Sub
Private Shared aSCIIValues As Byte()
Private Shared indexToCheck As Integer
Private Shared m_requiredLength As Integer = 2
Public Shared Property RequiredLength() As Integer
Get
Return m_requiredLength
End Get
Set(ByVal value As Integer)
m_requiredLength = value
End Set
End Property
Public Shared Function NextKeyCode(ByVal KeyCode As String, ByVal Sequence As SequenceType) As String
If KeyCode.Length <> m_requiredLength Then
Select Case Sequence
Case SequenceType.NumericToAlpha
Return MakeCustomLengthString("0", m_requiredLength)
Case SequenceType.AlphaToNumeric
Return MakeCustomLengthString("A", m_requiredLength)
Case SequenceType.AlphaNumeric
Return "A" & MakeCustomLengthString("0", m_requiredLength - 1)
Case SequenceType.NumericOnly
Return MakeCustomLengthString("0", m_requiredLength)
Case SequenceType.AlphaOnly
Return MakeCustomLengthString("A", m_requiredLength)
Case Else
Return ""
End Select
End If
'If reached to max
Select Case Sequence
Case SequenceType.NumericToAlpha
If KeyCode = MakeCustomLengthString("Z", m_requiredLength) Then
Throw New OverflowException("Maximum number is reached")
End If
Exit Select
Case SequenceType.AlphaToNumeric
If KeyCode = MakeCustomLengthString("9", m_requiredLength) Then
Throw New OverflowException("Maximum number is reached")
End If
Exit Select
Case SequenceType.AlphaNumeric
If KeyCode = MakeCustomLengthString("9", m_requiredLength) Then
Throw New OverflowException("Maximum number is reached")
End If
Exit Select
Case SequenceType.NumericOnly
If KeyCode = MakeCustomLengthString("9", m_requiredLength) Then
Throw New OverflowException("Maximum number is reached")
End If
Exit Select
Case SequenceType.AlphaOnly
If KeyCode = MakeCustomLengthString("Z", m_requiredLength) Then
Throw New OverflowException("Maximum number is reached")
End If
Exit Select
Case Else
Exit Select
End Select
aSCIIValues = ASCIIEncoding.ASCII.GetBytes(KeyCode.ToUpper())
indexToCheck = aSCIIValues.Length - 1
Dim keepChecking As Boolean = True
While keepChecking
aSCIIValues(indexToCheck) = [next](aSCIIValues(indexToCheck), Sequence)
If aSCIIValues(indexToCheck) = SingleCharacterMaxValue(Sequence) AndAlso indexToCheck <> 0 Then
indexToCheck -= 1
Else
keepChecking = False
End If
End While
KeyCode = ASCIIEncoding.ASCII.GetString(aSCIIValues)
Return KeyCode
End Function
Private Shared Function [next](ByVal current As Integer, ByVal sequence As SequenceType) As Byte
Select Case sequence
Case SequenceType.NumericToAlpha
If current = 57 Then
current = 65
ElseIf current = 90 Then
current = 48
Else
current += 1
End If
Exit Select
Case SequenceType.AlphaToNumeric
If current = 90 Then
current = 48
ElseIf current = 57 Then
current = 65
Else
current += 1
End If
Exit Select
Case SequenceType.AlphaNumeric
If current = 57 Then
current = 65
ElseIf current = 90 Then
current = 48
Else
current += 1
End If
Exit Select
Case SequenceType.NumericOnly
If current = 57 Then
current = 48
Else
current += 1
End If
Exit Select
Case SequenceType.AlphaOnly
If current = 90 Then
current = 65
Else
current += 1
End If
Exit Select
Case Else
Exit Select
End Select
Return Convert.ToByte(current)
End Function
Private Shared Function MakeCustomLengthString(ByVal data As String, ByVal length As Integer) As String
Dim result As String = ""
For i As Integer = 1 To length
result += data
Next
Return result
End Function
Private Shared Function SingleCharacterMaxValue(ByVal sequence As SequenceType) As Integer
Dim result As Integer = 0
Select Case sequence
Case SequenceType.NumericToAlpha
result = 48
Exit Select
Case SequenceType.AlphaToNumeric
result = 65
Exit Select
Case SequenceType.AlphaNumeric
result = 48
Exit Select
Case SequenceType.NumericOnly
result = 48
Exit Select
Case SequenceType.AlphaOnly
result = 65
Exit Select
Case Else
Exit Select
End Select
Return result
End Function
Private Shared Function isAllNine() As Boolean
Dim result As Boolean = True
For i As Integer = 0 To aSCIIValues.Length - 1
If aSCIIValues(i) <> 57 Then
result = False
Exit For
End If
Next
Return result
End Function
End Class
End Namespace
Form1 code:
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Text
Imports alphanumeric_increment.INCREMENT_IN_C_SHARP
Public Class Form1
<STAThread()> _
Private Shared Sub Main()
Application.Run(New Form1())
End Sub
Private sequenceType As SequenceType = sequenceType.NumericOnly
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
labelMessage.Text = ""
Try
AlphaNumeric.RequiredLength = Convert.ToInt32(textBox3.Text)
textBox1.Text = AlphaNumeric.NextKeyCode(textBox1.Text, sequenceType)
listBox1.Items.Insert(0, textBox1.Text)
textBox2.Text = (Convert.ToInt32(textBox2.Text) + 1).ToString()
Catch oex As OverflowException
labelMessage.Text = oex.Message
Catch ex As Exception
labelMessage.Text = ex.Message
End Try
End Sub
Private Sub radioNumericToAlpha_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioNumericToAlpha.CheckedChanged
If radioNumericToAlpha.Checked Then
sequenceType = sequenceType.NumericToAlpha
textBox1.Text = "0"
textBox2.Text = "0"
listBox1.Items.Clear()
labelSample.Text = "00,01,...,09,0A,...0Z,10,11...,A0,A1,...,ZZ"
End If
End Sub
Private Sub radioAlphaToNumeric_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioAlphaToNumeric.CheckedChanged
If radioAlphaToNumeric.Checked Then
sequenceType = sequenceType.AlphaToNumeric
textBox1.Text = "0"
textBox2.Text = "0"
listBox1.Items.Clear()
labelSample.Text = "AA,AB,...,AZ,A0,...A9,BA,BB...ZZ,00,01,...99"
End If
End Sub
Private Sub radioAlphaNumeric_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioAlphaNumeric.CheckedChanged
If radioAlphaNumeric.Checked Then
sequenceType = sequenceType.AlphaNumeric
textBox1.Text = "0"
textBox2.Text = "0"
listBox1.Items.Clear()
labelSample.Text = "A0,A1,...,A9,AA,...AZ,B0,B1...ZZ,00,01,...99"
End If
End Sub
Private Sub radioNumericOnly_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioNumericOnly.CheckedChanged
If radioNumericOnly.Checked Then
sequenceType = sequenceType.NumericOnly
textBox1.Text = "0"
textBox2.Text = "0"
listBox1.Items.Clear()
labelSample.Text = "00,01,...99"
End If
End Sub
Private Sub radioAlphaOnly_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioAlphaOnly.CheckedChanged
If radioAlphaOnly.Checked Then
sequenceType = sequenceType.AlphaOnly
textBox1.Text = "0"
textBox2.Text = "0"
listBox1.Items.Clear()
labelSample.Text = "AA,AB,...,ZZ"
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
|