簡體   English   中英

檢查 TextBox 是否為空

[英]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.usernameMy.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM