VB求助高手。编写一个检查编号是否合法的程序。

2025-02-24 13:20:07
推荐回答(3个)
回答1:

VB2008的代码:
。。。。。。。。。。。。。。。。。。。。。。。
Public Class Form1

Dim in_str '输入的字符串
Dim warn() As String = {"第", "个字符必须是大写字母", "个字符必须是-", "个字符必须是数字", "编码不能少于4位"}

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer '记录输入的字符串中字符的个数
Dim n As Integer '临时变量,用于简化代码的书写
If Len(TextBox1.Text) < 4 Then
MsgBox(warn(4))
End If
n = Len(TextBox1.Text)
For i = 1 To n
in_str = Mid(TextBox1.Text, i, 1)
Select Case i
Case 1, 2
If Asc(in_str) < 65 Or Asc(in_str) > 90 Then
MsgBox(warn(0) & i & warn(1))
End If
Case 3
If in_str <> "-" Then
MsgBox(warn(0) & i & warn(2))
End If
Case Else
If Not IsNumeric(in_str) Then
MsgBox(warn(0) & i & warn(3))
End If
End Select
Next
End Sub
End Class
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
共同学习一下。
张志晨

回答2:

待会看答案。

Dim A As String, B As Integer, C As String, I As Integer
A = Text1.Text
B = Len(A)
For I = 1 To B
C = Mid(A, I, 1)
Select Case I
Case 1
If C < "A" Or C > "Z" Then
MsgBox "第一个字符必须是大写字母!", 16, "错误!"
Exit Sub
End If
Case 2
If C < "A" Or C > "Z" Then
MsgBox "第二个字符必须是大写字母!", 16, "错误!"
Exit Sub
End If
Case 3
If C <> "-" Then
MsgBox "第三个字符必须是“ - ”号!", 16, "错误!"
Exit Sub
End If
Case Is > 3
If Val(C) <= 0 Then
MsgBox "第" & I & "个字符必须是数字!", 16, "错误!"
Exit Sub
End If
End Select
Next I

回答3:

Dim i As Integer, Tmp As String, Msg As String
Dim a() As String
If Len(Text1) < 4 Then
MsgBox "位数最少为4位!"
Exit Sub
End If
Msg = ""
For i = 1 To Len(Text1)
Tmp = Mid(Text1, i, 1)
Select Case i
Case 1, 2
If Asc(Tmp) < 65 Or Asc(Tmp) > 90 Then
Msg = Msg & "第" & i & "个字符必须是大写字母;"
End If
Case 3
If Tmp <> "—" Then
Msg = Msg & "第" & i & "个字符必须是“—”;"
End If
Case Else
If Not IsNumeric(Tmp) Then
Msg = Msg & "第" & i & "个字符必须是数字;"
End If
End Select
Next
If Len(Msg) > 0 Then
MsgBox Msg
End If