這裏是一個直接回答你的問題:。
可以存儲在列表中的空控件,並在最後創建這樣的消息:
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)
善待用戶:看ErrorProvider控件 – Plutonix
@Plutonix感謝您的建議。但我希望它顯示在MessageBox中。可能? – Student
如果當然有可能,只需要將錯誤信息累加到一個字符串中 – Plutonix