Moe Sisko
Moe Sisko

Reputation: 12005

MessageBox.Show behaviour with a lot of lines

Sample code to illustrate:

    private void button2_Click(object sender, EventArgs e)
    {
        const int numLines = 3000;

        StringBuilder sb = new StringBuilder();
        for(int i=0; i < numLines; i++)
        {
            sb.AppendFormat("Hello world {0}\r\n", i);
        }

        MessageBox.Show(this, sb.ToString(), "Error");
    }

Now, I realise it is probably not a good idea to attempt to display too many lines in a MessageBox. But I was curious as to its expected behaviour when numLines is "large". e.g. on my Windows 10 box, if numLines is 3000, the messagebox does not show at all, and you have to kill the program from the taskbar. Can anyone shed some light on this ?

EDIT: thanks to all the responses. The behaviour seems fairly random.

e.g. if numLines = 2175, the message box shows ok. if numLines = 2176, the message box does not appear, and the program becomes unresponsive. Pressing ALT-F4 makes the program responsive again (as though you just closed a modal dialog box which you could not see). if numLines = 4208, a message box with only the title "Error" (and no body) can be seen at the bottom of the screen like this:

enter image description here

Upvotes: 2

Views: 249

Answers (1)

MessageBox uses an really crazy system to find it's best size to fit into the screen. The big number of lines causes it to become stuck calculating and trying to render.

Upvotes: 1

Related Questions