2016-12-06 46 views
0

我有一些代碼,將檢查是否有任何值在窗體上標記爲0,每個文本框都有其自己的唯一警告消息。目前如果所有3個值都是0,則3個警告將依次出現。代碼如下第一次發生後停止警告消息?

If txt_quantity.Text = 0 Then MsgBox(Prompt:="Please enter a correct quantity") Else 
If txt_quantitysupplied.Text = 0 Then MsgBox(Prompt:="Please enter a correct quantity supplied") Else 
If txt_value.Text = 0 Then MsgBox(Prompt:="Please enter a correct value") Else 

如何修改代碼,這樣只要一個消息框diplayed停止檢查其他條件,所以理想的,如果有3個錯誤,會顯示一個警告,然後啓動當用戶下一次提交時再次處理。希望這是有道理的,我很難把它寫成文字。

+2

將邏輯放入IsValid()函數中,然後在msgbox之後退出函數? (在結尾處返回True) –

+3

與其僅僅顯示一條帶有一個錯誤的消息,請考慮顯示帶有* all *錯誤的1條消息 – Plutonix

+1

[ErrorProvider Class](https://msdn.microsoft.com/en-us/library /system.windows.forms.errorprovider(v=vs.110).aspx)是一種向用戶顯示錯誤的方法,不需要大量的模態MsgBoxes – Plutonix

回答

1

您需要使錯誤測試彼此獨立,並追加到錯誤字符串中,然後在必要時顯示。

下面的僞代碼說明了這一點:

string errorMessage = "" 

If txt_quantity.Text = 0 Then errorMessage += "\nPlease enter a correct quantity" 
If txt_quantitysupplied.Text = 0 Then errorMessage += "\nPlease enter a correct quantity supplied" 
If txt_value.Text = 0 Then errorMessage += "\nPlease enter a correct value" 

If errorMessage.Length > 0 Then 
    MsgBox(Prompt:=errorMessage) 
End If 

你可能會需要做的錯誤消息的更多的格式,以確保是正確顯示在單行和整件事線的每個錯誤。

+0

太棒了,感謝您的幫助! – Boneyt