vb中从键盘输入一个字符串(要求任意长度),然后删除其中的非数字字符,最后输出。(FOR语句)
1个回答
展开全部
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Dim Txt As String
For i = 1 To Len(Text1.Text)
If Asc(Mid(Text1.Text, i, 1)) >= 48 And Asc(Mid(Text1.Text, i, 1)) <= 57 Then Txt = Txt & Mid(Text1.Text, i, 1)
Next i
MsgBox Txt
End If
End Sub
另外:
If Asc(Mid(Text1.Text, i, 1)) >= 48 And Asc(Mid(Text1.Text, i, 1)) <= 57 Then Txt = Txt & Mid(Text1.Text, i, 1) 还可以简化为:
If Mid(Text1.Text, i, 1) Like "[0-9]" Then Txt = Txt & Mid(Text1.Text, i, 1)
If KeyAscii = vbKeyReturn Then
Dim Txt As String
For i = 1 To Len(Text1.Text)
If Asc(Mid(Text1.Text, i, 1)) >= 48 And Asc(Mid(Text1.Text, i, 1)) <= 57 Then Txt = Txt & Mid(Text1.Text, i, 1)
Next i
MsgBox Txt
End If
End Sub
另外:
If Asc(Mid(Text1.Text, i, 1)) >= 48 And Asc(Mid(Text1.Text, i, 1)) <= 57 Then Txt = Txt & Mid(Text1.Text, i, 1) 还可以简化为:
If Mid(Text1.Text, i, 1) Like "[0-9]" Then Txt = Txt & Mid(Text1.Text, i, 1)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询