在Stack Overflow上,我找到了问题 Generate a receipt number in this range 。
在回答中(来自Adam Maras)我发现这段代码(对我来说非常有趣):
Private Const FirstReceiptNumber As String = "GA00000"
Public Function GenerateReceiptNumber(ByVal lastNumber As String) As String
If lastNumber.Length <> 7 Then
Throw New ArgumentException("Incorrect length", "lastNumber")
End If
If lastNumber.StartsWith("G") = False Then
Throw New ArgumentException("Incorrect start character", "lastNumber")
End If
Dim letterPortion As Char = lastNumber.Chars(1)
If letterPortion < "A"c Or letterPortion > "Z"c Then
Throw New ArgumentException("Non-letter second character", "lastNumber")
End If
If letterPortion = "I"c Or letterPortion = "O"c Then
Throw New ArgumentException("Invalid second character", "lastNumber")
End If
Dim numericPortionString As String = lastNumber.Substring(2)
Dim numericPortion As Integer
If Integer.TryParse(numericPortionString, numericPortion) = False Then
Throw New ArgumentException("Invalid numeric portion", "lastNumber")
End If
If numericPortion = 99999 Then
If letterPortion = "Z"c Then
Throw New ArgumentException("No more receipt numbers possible", "lastNumber")
End If
numericPortion = 0
letterPortion = letterPortion + Chr(1)
If letterPortion = "I"c Or letterPortion = "O"c Then
letterPortion = letterPortion + Chr(1)
End If
Else
numericPortion = numericPortion + 1
End If
Return String.Format("G{0}{1:00000}", letterPortion, numericPortion)
End Function
我将其粘贴到Visual Studio 2010表单中,然后尝试查看Form_Load
上发生的情况。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = GenerateReceiptNumber() '<--HERE IS THE ERROR
End Sub
但在GenerateReceiptNumber()
显示错误。
当我打开此表单时,如何使用该代码在TextBox1
中为我提供收据编号?
下次使用Form_Load
时,收据编号必须为+1。
答案 0 :(得分:0)
很高兴你找到了我对上一个问题的答案!
现在,通过我在另一个问题中提供mr_dunski的解决方案,他要求算法根据之前的数字创建收据编号。所以你需要从一个初始数开始才能开始序列:
GenerateReceiptNumber(FirstReceiptNumber)
或
GenerateReceiptNumber("GA00000")
答案 1 :(得分:0)
您可以在此模式下书写:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = GenerateReceiptNumber("GA00000")
End Sub
或
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = GenerateReceiptNumber(FirstReceiptNumber)
End Sub