Is it possible to build a MessageBox message that is then passed to a message box to include new lines?

954 Views Asked by At

I am attempting to generate a message for the user that returns a list of missing checklist items. My question: is there a way to build a message that can then be passed to a MessageBox that includes new lines. I have considered overloading the method to accept various numbers of individual messages, but there has to be a more elegant way to do this. Below is the class that I have designed to handle this message collection, display, and future exportation to a more convenient format.

Public Class clsChecklistMissingItems

Private Shared iWrong As Integer = 0 'Number of items wrong.
Private Shared sMissingItems() As String 'Will use the number of items wrong.

Public Shared Sub CollectItem(ByVal mess As String) 'Saves the message passed to it.


    ReDim Preserve sMissingItems(iWrong) 'Resize the array based on the counter.
    sMissingItems(iWrong) = mess 'Assign the message to the missing items string array.
    iWrong = iWrong + 1 'Increment the counter (may give us +1 

End Sub

Public Sub DisplayList() 'Displays the message at the end of the execution.
    'Can this be generated procedurally?

    MessageBox.Show("There were " & iWrong & " missing or incorrect items." & vbNewLine &
                    sMissingItems(iWrong))

End Sub End Class

My alternate solution is to write a form that is formatted like a text box that will behave similar to a text box, but will have all of the described functionality.

2

There are 2 best solutions below

1
On BEST ANSWER

Using arrays is not the best option. .NET has plenty of built-in collection classes that are far superior to an array, like List<T>. I understand it's tempting to use an array when you're coming from other "flavors" of Visual Basic (VBScript, VBA, etc.) because that's what you're familiar with, but you should learn what's available in the .NET FCL.

You could do something like this using a loop and a StringBuilder to build your list of messages:

Dim wrongItems As New List(Of String)()

' fill the collection however you do it...
wrongItems.AddRange({"Reason 1", "Reason 2", "Reason 3"})

Dim sb As New StringBuilder()

For Each item In wrongItems
    sb.AppendLine(item)
Next

MsgBox(String.Format("There were {0} missing or incorrect items.",
                     wrongItems.Count) & vbNewLine & sb.ToString())
0
On

After speaking with my co-worker it was pointed out to me that VB.NET has a carriage return line feed that is designed to be concatenated into a string to represent a new line.

Public Sub DisplayList()

    Dim sMessage As String = ""

    For i As Integer = 0 To sMissingItems.Length - 1
        sMessage = sMessage & sMissingItems(i) & vbCrLf
    Next

    MessageBox.Show(sMessage)

End Sub

I have not had a chance to implement using a list rather than an array at this point.