[英]Check if TextBox is Empty or not
我目前正在编写一个小登录系统,我试图阻止用户创建一个没有在 TextBox 中输入任何内容的帐户。 这是我当前注册帐户的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = My.Settings.username
TextBox2.Text = My.Settings.password
If Trim(TextBox1.Text).Length < 1 AndAlso Trim(TextBox2.Text).Length < 1 Then
MsgBox("Wrong username or password!")
ElseIf Trim(TextBox1.Text).Length > 1 AndAlso Trim(TextBox2.Text).Length > 1 Then
MsgBox("Your account was created!", MsgBoxStyle.Information, "Create")
Me.Hide()
Form1.Show()
End If
End Sub
不知何故,即使我输入了一些东西,它也会总是说“错误的用户名或密码”。如果输入什么都没有,我如何让它只响应“错误的用户名或密码”?
编辑:我修复了代码。 但是我如何才能让这个人只能使用他注册的信息登录?
请检查My.Settings.username
和My.Settings.password
是否有非空值。 您正在用这些值替换两个文本框的Text
属性。 你可以这样做:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not String.IsNullOrWhitespace(My.Settings.username) Then
TextBox1.Text = My.Settings.username
End If
If Not String.IsNullOrWhitespace(My.Settings.password) Then
TextBox2.Text = My.Settings.password
End If
If String.IsNullOrWhitespace(TextBox1.Text) or String.IsNullOrWhitespace(TextBox2.Text) Then
MsgBox("Wrong username or password!")
...
请注意,在您的代码中,您没有评估何时TextBox1.Text.Trim().Length = 1
和/或TextBox2.Text.Trim().Length = 1
你能试试这个吗? 正如 Emilio 上面提到的,请确保您的 My.Settings.username 和 My.Settings.password 没有传递任何值。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = My.Settings.username
TextBox2.Text = My.Settings.password
If String.IsNullOrEmpty(TextBox1.Text) AndAlso String.IsNullOrEmpty(TextBox2.Text) Then
MsgBox("Wrong username or password!")
Else
MsgBox("Your account was created!", MsgBoxStyle.Information, "Create")
Me.Hide()
Form1.Show()
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.