如何在一个MsgBox中包含多个消息

时间:2013-06-13 07:39:40

标签: asp.net vb.net

我想使用\n在一个MsgBox中包含多个消息。我需要在显示MsgBox时删除此字符串。

这是我的代码。

Dim Msg As String = ""
Dim EmployeeFirstName As String
Dim EmployeeLastName As String
EmployeeFirstName = txtFirstName.Text.Trim
EmployeeLastName = txtLastName.Text.Trim
If EmployeeFirstName = "" Then
  Msg = "Please enter First Name!"
  Msg += "/n"
End If
If EmployeeLastName = "" Then
  Msg += "Please enter Last Name!"
  Msg += "/n"
End If
If ddlGender.SelectedItem.Value = -1 Then
  Msg += "Plrase Select department"
  Msg += "/n"
End If
MsgBox(Msg)

2 个答案:

答案 0 :(得分:2)

当您需要动态构建字符串时,

StringBuilder通常是一个不错的选择。它通常表现得更好,并且通常比使用一串字符串连接更简洁,更易于维护。

Dim msgBuilder As New StringBuilder()
'...
If EmployeeFirstName = "" Then
    msgBuilder.AppendLine("Please enter First Name!")
End If
'And so forth
MsgBox(msgBuilder.ToString())

但是,正如Matt Wilko指出的那样,如果这是ASP.NET,你根本不想使用MsgBox。

答案 1 :(得分:1)

喜欢这个......

Msg = "Please enter First Name!" & vbCrlf
Msg &= "Please enter Last Name!" & vbCrlf
Msg &= "Please Select department"