Vb.net 创建登录/注册表单
我正在开发一个vb表单,它允许用户创建一个“帐户”。我将用户名和密码存储在两个数组中,并从中提取信息。但当我运行程序时,它会出现一个问题: “类型为'System.ArgumentNullException'的未处理异常” 发生在Microsoft.VisualBasic.dll中,其他信息:值 不能为空。” 其中按钮2/寄存器按钮的代码为(确切地说:Vb.net 创建登录/注册表单,vb.net,Vb.net,我正在开发一个vb表单,它允许用户创建一个“帐户”。我将用户名和密码存储在两个数组中,并从中提取信息。但当我运行程序时,它会出现一个问题: “类型为'System.ArgumentNullException'的未处理异常” 发生在Microsoft.VisualBasic.dll中,其他信息:值 不能为空。” 其中按钮2/寄存器按钮的代码为(确切地说: For i = 0 To (UBound(Usernames)) 你能帮我一个忙,告诉我该怎么做/如何处理这种情况吗?下面是代码: Publ
For i = 0 To (UBound(Usernames))
你能帮我一个忙,告诉我该怎么做/如何处理这种情况吗?下面是代码:
Public Class Form1
Dim Usernames() As String
Dim Passwords() As String
Dim CurrName As String
Dim i As Integer
'Login button is pressed
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Index As Integer
CurrName = TextBox1.Text
For i = 0 To (UBound(Usernames))
If IfRepetition(Usernames, CurrName, i) = True Then
Index = Array.IndexOf(Usernames, TextBox1.Text)
If TextBox2.Text = Passwords(Index) Then
Form3.Show()
Me.Hide()
End If
Else
MsgBox("The username or password is incorrect", MsgBoxStyle.Critical)
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
CurrName = TextBox1.Text
' *** Error (apparently) happens here ***
For i = 0 To (UBound(Usernames))
If IfRepetition(Usernames, CurrName, i) = True Then
MsgBox("This username already exists!")
Else
ReDim Preserve Usernames(UBound(Usernames) + 1)
Usernames(UBound(Usernames)) = TextBox1.Text
ReDim Preserve Passwords(UBound(Passwords) + 1)
Passwords(UBound(Passwords)) = TextBox2.Text
End If
Next
End Sub
Private Function IfRepetition(ByRef Usernames() As String, CurrName As String, i As Integer) As Boolean
Dim j As Integer
'Checks for repetition of a username in the usernames array
IfRepetition = False
For j = 0 To (UBound(Usernames))
If Usernames(j) = CurrName Then
IfRepetition = True
Exit Function
End If
Next
End Function
End Class
您得到的错误是因为如果数组为null,UBound将引发异常。 另外,使用数组来实现这一点也不是一个好方法,我建议您使用字典来实现更简单、更短的代码
Dim dic As New Dictionary(Of String, String) 'A new dictionary which handles usernames & passwords
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Login button
If String.IsNullOrWhiteSpace(TextBox1.Text) Then 'Check the entered username, if it's empty show error message and return ( don't continue )
MessageBox.Show("Enter the username")
Return
End If
If String.IsNullOrWhiteSpace(TextBox2.Text) Then 'Check the entered password, if it's empty show error message and return ( don't continue )
MessageBox.Show("Enter the password")
Return
End If
If Not dic.ContainsKey(TextBox1.Text) Then 'Check the entered username, if it doesn't exist show error message and return ( don't continue )
MessageBox.Show("The username is incorrect")
Return
End If
If dic(TextBox1.Text) <> TextBox2.Text Then 'Validate entered username and password, if it's wrong show error message and return ( don't continue )
MessageBox.Show("The password is incorrect")
Return
End If
Form3.Show() 'If none of above error messages appear which means the entered username and password are correct, show Form3 and hide this form
Me.Hide()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'create account button
If String.IsNullOrWhiteSpace(TextBox1.Text) Then 'Check the entered username, if it's empty show error message and return ( don't continue )
MessageBox.Show("Enter the username")
Return
End If
If String.IsNullOrWhiteSpace(TextBox2.Text) Then 'Check the entered password, if it's empty show error message and return ( don't continue )
MessageBox.Show("Enter the password")
Return
End If
If dic.ContainsKey(TextBox1.Text) Then 'Check the entered username, if it exists show error message and return ( don't continue )
MessageBox.Show("This username already exists")
Return
End If
dic.Add(TextBox1.Text, TextBox2.Text) 'If none of above error messages which means it's okay to create this account, Add the username & password to the dictionary
End Sub
Dim dic As New Dictionary(字符串的)一个处理用户名和密码的新字典
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
'登录按钮
如果String.IsNullOrWhiteSpace(TextBox1.Text),则“检查输入的用户名,如果为空,则显示错误消息并返回(不继续)
MessageBox.Show(“输入用户名”)
返回
如果结束
如果String.IsNullOrWhiteSpace(TextBox2.Text),则“检查输入的密码,如果密码为空,则显示错误消息并返回(不继续)
MessageBox.Show(“输入密码”)
返回
如果结束
如果不是dic.ContainsKey(TextBox1.Text),则检查输入的用户名,如果不存在,则显示错误消息并返回(不继续)
MessageBox.Show(“用户名不正确”)
返回
如果结束
如果dic(TextBox1.Text)TextBox2.Text,则“验证输入的用户名和密码,如果错误,则显示错误消息并返回(不继续)
MessageBox.Show(“密码不正确”)
返回
如果结束
Form3.Show()'如果上面没有显示任何错误消息,这意味着输入的用户名和密码是正确的,则显示Form3并隐藏此表单
我躲起来
端接头
私有子按钮2\u单击(发送者作为对象,e作为事件参数)处理按钮2。单击
'创建帐户按钮
如果String.IsNullOrWhiteSpace(TextBox1.Text),则“检查输入的用户名,如果为空,则显示错误消息并返回(不继续)
MessageBox.Show(“输入用户名”)
返回
如果结束
如果String.IsNullOrWhiteSpace(TextBox2.Text),则“检查输入的密码,如果密码为空,则显示错误消息并返回(不继续)
MessageBox.Show(“输入密码”)
返回
如果结束
如果dic.ContainsKey(TextBox1.Text),则检查输入的用户名,如果存在,则显示错误消息并返回(不继续)
MessageBox.Show(“此用户名已存在”)
返回
如果结束
dic.Add(TextBox1.Text,TextBox2.Text)'如果没有上述错误消息表示可以创建此帐户,请将用户名和密码添加到字典中
端接头
“各种不同的问题”你需要更具体一些,而不是复制粘贴你在互联网上找到的内容。试着理解。你说了很多问题,但不能解释它们?“各种不同的问题”没有真正告诉我们关于这个问题的任何信息。也许你可以说得更具体一点?这不是一个教程网站,各种不同的问题
意味着你希望我们为你调试你的应用程序(这篇文章太宽泛了)没有人能解决所有不同的问题,所以一次只能解决一个。当你遇到一个具体的问题和你的努力时,请在发帖前仔细研究并记录下这个公式。麦德里克,在发帖之前,我不会只是从网上复制粘贴,这就是为什么我要问这个问题——为了理解非常感谢你,这非常有效。感谢你的帮助!