2014-10-01 103 views
0

我有一個程序,我使用複雜的if語句。下面的if語句沒有正確處理:vb.net複雜if語句

如果form3上的textbox1.text中有數據,它從不處理第一個else語句,它就好像它退出sub。現在,如果form3 textbox1.text是空白的,它將處理messagebox if語句到底是如何寫入的。以下if語句有什麼不對?

If form3.TextBox1.Text = "" Then 
    Dim result1 As DialogResult = MessageBox.Show("Click OK to fill out user settings, or CANCEL to do it later", "Settings", MessageBoxButtons.OKCancel) 
    If result1 = Windows.Forms.DialogResult.OK Then 
     pwauthen.ShowDialog() 
    ElseIf result1 = Windows.Forms.DialogResult.Cancel Then 
     Exit Sub 
    Else 
    If TextBox1.Text = "" Then 
     MsgBox("Please describe the issue you're having Bender, I'm not a mindreader!") 
     Exit Sub 
    Else 
    .......Do a lot of other processing..... 
    End if 
    End if 
End if 

回答

0

這種看法對不對?格式應該可以幫到你。當您點擊If語句的其中一個元素時,將會突出顯示所有的部分,以向您顯示它們所屬的位置。

If form3.TextBox1.Text = "" Then 
    Dim result1 As DialogResult = MessageBox.Show("Click OK to fill out user settings, or CANCEL to do it later", "Settings", MessageBoxButtons.OKCancel) 
    If result1 = Windows.Forms.DialogResult.OK Then 
     pwauthen.ShowDialog() 
    Else 
     Exit Sub 
    End If 
Else 
    If TextBox1.Text = "" Then 
     MessageBox.Show("Please describe the issue you're having Bender, I'm not a mindreader!") 
     Exit Sub 
    Else 
     '.....Do a lot of other processing..... 
    End If 
End If 
0

如果我正確計算,最後一個結束,如果關閉那第一個如果。第一個如果沒有其他塊。 else塊將在結束前立即結束。

讓Visual Studio幫助縮進。這通常會使代碼更易於閱讀。你也可以懸停在if語句看到結尾(至少對於C++和c#,有一段時間沒有使用VB)