2015-12-03 50 views
1

假設我有一個包含100個文本框,組合框和其他控件的窗體,並且我想檢查是否有任何控件是空的。當我點擊「確定」按鈕時,我想讓消息框顯示錯誤列表,例如Textbox1爲空,Textbox30爲空等。VB.NET使用MessageBox進行錯誤檢查時需要指導

我可以通過執行冗長的方法來實現這一點,其中我檢查textbox1並顯示messagebox,檢查textbox2並再次顯示messagebox等等。

我希望消息框只顯示一次。我怎樣才能做到這一點?

我所做的是我設置了一個數組,並通過選擇(例如Msgbox(errMessages(3)+ Environment.newline + errMessages(30))來存儲所有要顯示的錯誤消息,我知道這是不正確的方式做到這一點,以及

請和謝謝你提前

+1

善待用戶:看ErrorProvider控件 – Plutonix

+0

@Plutonix感謝您的建議。但我希望它顯示在MessageBox中。可能? – Student

+0

如果當然有可能,只需要將錯誤信息累加到一個字符串中 – Plutonix

回答

1

這裏是一個直接回答你的問題:。

可以存儲在列表中的空控件,並在最後創建這樣的消息:

Dim empty_controls = New List(Of Control) 

If TextBox1.Text = String.Empty Then 
    empty_controls.Add(TextBox1) 
End If 

If TextBox2.Text = String.Empty Then 
    empty_controls.Add(TextBox2) 
End If 

Dim result As String = String.Join(
    Environment.NewLine, 
    empty_controls.Select(Function(c As Control) c.Name + " is empty")) 

MessageBox.Show(result) 

這裏甚至有一個更好的方法來檢測該文本框爲空:

Dim empty_controls = New List(Of Control) 

//The following line will search through all text boxes on the form 
empty_controls.AddRange(
    Controls.OfType(Of TextBox).Where(Function(c As Control) c.Text = String.Empty)) 

//Here you can add other kinds of controls with their own way of determining if they are empty 

Dim result As String = String.Join(
    Environment.NewLine, 
    empty_controls.Select(Function(c As Control) c.Name + " is empty")) 

MessageBox.Show(result) 
+0

非常感謝!這正是我需要的! – Student