[英]Check if TextBox is Empty or not
I'm currently programming a little login system and I'm trying to prevent users from creating an account with nothing typed into the TextBoxes.我目前正在编写一个小登录系统,我试图阻止用户创建一个没有在 TextBox 中输入任何内容的帐户。 This is my current code for registering an account:
这是我当前注册帐户的代码:
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
Somehow, it will always say "Wrong username or password" even if I type something in. How do I make it only respond "Wrong username or password" if the input is nothing?不知何故,即使我输入了一些东西,它也会总是说“错误的用户名或密码”。如果输入什么都没有,我如何让它只响应“错误的用户名或密码”?
Edit: I fixed the code.编辑:我修复了代码。 But how do i make it possible that the person can only login with the information he registered?
但是我如何才能让这个人只能使用他注册的信息登录?
Please check if My.Settings.username
and My.Settings.password
have non empty values.请检查
My.Settings.username
和My.Settings.password
是否有非空值。 You are replacing both text boxes' Text
properties with those values.您正在用这些值替换两个文本框的
Text
属性。 You could do something like:你可以这样做:
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!")
...
Please note that in your code, you are not evaluating when TextBox1.Text.Trim().Length = 1
and/or TextBox2.Text.Trim().Length = 1
请注意,在您的代码中,您没有评估何时
TextBox1.Text.Trim().Length = 1
和/或TextBox2.Text.Trim().Length = 1
Can you try this?你能试试这个吗? And as what Emilio mentioned above, please make sure that your My.Settings.username and My.Settings.password are not passing any values.
正如 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.