2017-09-11 26 views
0

所以我有一個用戶註冊表單。有對輸入名稱年齡,用戶名密碼確認密碼安全問題答案文本框。 在點擊提交按鈕之前,我想確保所有的框都已填滿並且密碼匹配。知道哪些條件返回False在IF..THEN語句

PasswordMatch返回密碼是否匹配或不爲真或假

這是我的代碼:

Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click 
    'check if everything is filled 
    If Name_TxtBox.Text <> "" And Age_Box.Text <> "" And username_Box.Text.Length > 4 And PasswordMatch = True And Security_Ques.Text.Length > 5 And Answer_Box.Text.Length > 2 Then 
     'yupp, everything fine. now continue 
    End If 
End Sub 

現在,我想知道哪些條件是假的,所以我可以做出相應的反應。

有什麼方法可以知道嗎?像捕捉異常或其他東西?我知道這與此無關,但我只是想,也許我們可以捕捉哪種情況是錯誤的。

我看到this答案,他們建議使用ElseIf。這可能是一種方式,但這太冗長了,需要更多的代碼。

+1

單獨使用* if * then * per test單獨檢查它們。如果需要「更多行代碼」來完成某項任務,那麼「更多行代碼」沒有任何問題。 –

+0

不相關,但你應該在邏輯表達式中使用'AndAlso',而不是'And'。 –

+0

所以沒有辦法知道哪個條件返回False? – Subaz

回答

2

如果你有一個如果條件,我認爲你不能確定他們哪些是假的,所以創建多個條件。

If Name_TxtBox.Text = "" Then 
    'Do whatever you want 
    Return 'This exits the sub, so the following lines won't execute 
End If 

If Age_Box.Text = "" Then 
    'Do whatever you want 
    Return 'This exits the sub, so the following lines won't execute 
End If 

If Not username_Box.Text.Length > 4 Then 
    'Do whatever you want 
    Return 'This exits the sub, so the following lines won't execute 
End If 

If Not PasswordMatch Then 
    'Do whatever you want 
    Return 'This exits the sub, so the following lines won't execute 
End If 

If Not Security_Ques.Text.Length > 5 Then 
    'Do whatever you want 
    Return 'This exits the sub, so the following lines won't execute 
End If 

If Not Answer_Box.Text.Length > 2 Then 
    'Do whatever you want 
    Return 'This exits the sub, so the following lines won't execute 
End If 

'yupp, everything fine. now continue 
+0

這不完全是我要求的,但你做了一半的作品,所以我會給你15次代表哈哈:D – Subaz

1

我想,你可以這樣做。

If Name_TxtBox.Text <> "" Then 
    If Age_Box.Text <> "" Then 
    Else 
     'Error: Age is empty 
    End If 
Else 
    'Error: Name is empty 
End If