VB.NET如何把字符串(不全是数字或为空)转换成数字?
编一个类似于注册机的程序,怎么把TextBox里带字母的字符串转换成数字?(转换成编码也行,只要能计算)这样写如果不输入或输入的不是纯数字就报错:PrivateSubBu...
编一个类似于注册机的程序,怎么把TextBox里带字母的字符串转换成数字?(转换成编码也行,只要能计算)
这样写如果不输入或输入的不是纯数字就报错:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'两个TextBox已建立,TextBox1用于输入用户名,TextBox2用于显示注册码
Dim usernane, pasward As String
Dim temp As Long
usernane = CLng(TextBox1.Text)
temp = usernane * 3 - 500
pasward = CStr(temp)
TextBox2.Text = pasward
End Sub 展开
这样写如果不输入或输入的不是纯数字就报错:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'两个TextBox已建立,TextBox1用于输入用户名,TextBox2用于显示注册码
Dim usernane, pasward As String
Dim temp As Long
usernane = CLng(TextBox1.Text)
temp = usernane * 3 - 500
pasward = CStr(temp)
TextBox2.Text = pasward
End Sub 展开
展开全部
明白你的意思了,只要是字母都会转换成数字,帮你写好了("usernane"和 "pasward" 我这里写成 "username" 和" password")
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim username, password As String
Dim temp As Long
Dim r As String = ""
Dim v As String = TextBox1.Text
For Each c In v.ToString
If IsNumeric(c) Then
r &= c
Else
r &= ((Asc(c) - 65).ToString("00"))
End If
Next
username = r
temp = username * 3 - 500
password = CStr(temp)
TextBox2.Text = password
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim username, password As String
Dim temp As Long
Dim r As String = ""
Dim v As String = TextBox1.Text
For Each c In v.ToString
If IsNumeric(c) Then
r &= c
Else
r &= ((Asc(c) - 65).ToString("00"))
End If
Next
username = r
temp = username * 3 - 500
password = CStr(temp)
TextBox2.Text = password
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
解决的思路:
方法1:把其中非数字忽略掉,即去除。
方法2:把字符转换成它的ASCII码.
另外,你的代码中声明变量的语句:Dim usernane, pasward As String
把usernane声明为String类型了,怎么在使用的时候:usernane = CLng(TextBox1.Text)
是试图存放长整型的数值呢?
方法1:把其中非数字忽略掉,即去除。
方法2:把字符转换成它的ASCII码.
另外,你的代码中声明变量的语句:Dim usernane, pasward As String
把usernane声明为String类型了,怎么在使用的时候:usernane = CLng(TextBox1.Text)
是试图存放长整型的数值呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
明白你的意思了,只要是字母都会转换成数字,帮你写好了("usernane"和 "pasward" 我这里写成 "username" 和" password")
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim username, password As String
Dim temp As Long
Dim r As String = ""
Dim v As String = TextBox1.Text
For Each c In v.ToString
If IsNumeric(c) Then
r &= c
Else
r &= ((Asc(c) - 65).ToString("00"))
End If
Next
username = r
temp = username * 3 - 500
password = CStr(temp)
TextBox2.Text = password
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim username, password As String
Dim temp As Long
Dim r As String = ""
Dim v As String = TextBox1.Text
For Each c In v.ToString
If IsNumeric(c) Then
r &= c
Else
r &= ((Asc(c) - 65).ToString("00"))
End If
Next
username = r
temp = username * 3 - 500
password = CStr(temp)
TextBox2.Text = password
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询