In this tip, you will see what happens when a string longer that 65536 is shown using a message box. The result might be unexpected.
Introduction
A message box is designed to show a small amount of text with a few choices that the user can choose from. However, in some extreme or erroneus cases, the text might grow very long so what happens in such situation.
It seems that the message box has a limitation of 65536 characters for the text. That seems reasonable since that amount of text cannot be shown anymore because the window flows over the screen borders. One would expect an exception to be thrown in this case but what actually happens is that the message box is not shown, no exception is raised and the result for the message box is unexpected.
Test Case
First, let's have a small class providing the long string, like this:
public static class MessageString {
private readonly static string baseString = @"Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium
doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore
veritatis et quasi architecto beatae vitae dicta sunt, explicabo.Nemo enim ipsam
voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur
magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est,
qui do lorem ipsum, quia dolor sit amet consectetur adipisci velit, sed quia non
numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat
voluptatem.Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis
suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum
iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur,
vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur? At vero eos et
accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum
deleniti atque corrupti, quos dolores et quas molestias excepturi sint, obcaecati
cupiditate non provident, similique sunt in culpa, qui officia deserunt mollitia animi,
id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita
distinctio.Nam libero tempore, cum soluta nobis est eligendi optio, cumque nihil impedit,
quo minus id, quod maxime placeat, facere possimus, omnis voluptas assumenda est,
omnis dolor repellendus.Temporibus autem quibusdam et aut officiis debitis aut rerum
necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae
non recusandae.Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis
voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
";
public readonly static string UnlimitedString;
static MessageString() {
for (int counter = 0; counter < 35; counter++) {
UnlimitedString += baseString;
}
}
public static string LimitedString =>
UnlimitedString.Length > 65536
? UnlimitedString.Substring(0, 65535)
: UnlimitedString;
}
Now we can use this class to test the behaviour in different scenarios. For example, using C# and WPF, the code could look like:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Limitless_Click(object sender, RoutedEventArgs e) {
this.ShowMessage(MessageString.UnlimitedString);
}
private void Limited_Click(object sender, RoutedEventArgs e) {
this.ShowMessage(MessageString.LimitedString);
}
private void ShowMessage(string text) {
MessageBoxResult result;
Mouse.OverrideCursor = Cursors.Wait;
result = MessageBox.Show(text,
"Message",
MessageBoxButton.YesNoCancel,
MessageBoxImage.Question,
MessageBoxResult.Yes);
MessageBox.Show($"Result was {result}", "Result", MessageBoxButton.OK);
Mouse.OverrideCursor = null;
}
}
Equally using Visual Basic.NET with Forms could look like:
Public Class Form1
Private Sub Limitless_Click(sender As Object, e As EventArgs) Handles Limitless.Click
Me.ShowMessage(MessageString.UnlimitedString)
End Sub
Private Sub Limited_Click(sender As Object, e As EventArgs) Handles Limited.Click
Me.ShowMessage(MessageString.LimitedString)
End Sub
Private Sub ShowMessage(text As String)
Dim result As DialogResult
Application.UseWaitCursor = True
result = MessageBox.Show(text, _
"Message", _
MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button1)
MessageBox.Show($"Result was {result}", "Result", MessageBoxButtons.OK)
Application.UseWaitCursor = False
End Sub
End Class
So What About the Result
As mentioned, the result of the message box is unexpected. If you try the test code where the message box has three buttons, Yes
, No
, and Cancel
and the default button is Yes
. When the unlimited text is passed to the message box, the result of the question is No (again, without actually showing the question).
Conclusion
So, if you have a situation where the text to be shown in a message box could grow long, remember to limit the text to some reasonable length. Otherwise, you may end up in peculiar situations and get wrong answers from UI.
History