11

I want to display a MessageBox alerting the user that the process is complete, and giving a breakdown on how long each stage of the process took. I've got the text that I want to display formatted appropriately, but the default font of the MessageBox class is not mono-width. As far as I can tell, there's no way to specify the font that the text displays with.

Is there an out-of-the-box library somewhere that I can use for this, or am I going to have to write one up myself?

T.K.
  • 2,229
  • 5
  • 22
  • 26
  • 1
    I'm sure there's libraries already written but it's wouldn't take much to write one yourself. – Ash Burlaczenko Mar 24 '11 at 14:57
  • I had the same issue in one of my projects, wherein I had to display a bigger font size. For that I created my own customized message box. Check this link for reference. http://www.codeproject.com/KB/cs/A_Custom_Message_Box.aspx – reggie Mar 24 '11 at 15:00

5 Answers5

17

Any reason not to just create a Form with a textbox/label using a monospace font, then call Form.ShowDialog? Sounds like a separate library with that would be overkill to me.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Yeah, that's the solution I had in mind. I figured that it would be a common situation, and was surprised to find that there wasn't a way to do it with `MessageBox`, so I hoped I was just missing something. Thanks. – T.K. Mar 24 '11 at 15:14
  • 2
    @T.K.: I could be wrong, but I think that `MessageBox.Show` is just invoking the Win32 message box, and I suspect that doesn't have this sort of functionality. – Jon Skeet Mar 24 '11 at 15:16
  • One big advantage to using a form is that you can make the text copiable to the clipboard. E.g. use a readonly textbox to present an error message to the user; looks like a label but can be copied. – Jon Coombs Aug 06 '14 at 03:10
  • 4
    @JonCoombs: You can also hit Ctrl + C in a standard MessageBox which will copy all contents (caption/text/buttons) to the clipboard as text. – jreichert Feb 25 '15 at 12:06
11

For the record, this is in fact possible, MessageBox() expands tabs. For example:

    private void button1_Click(object sender, EventArgs e) {
        MessageBox.Show(
            "hello\tworld\r\n" + 
            "second\tline");
    }

It isn't very trustworthy if the word width starts to approach the tab width. You still should prefer a little helper form with a ListView.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
4

I have just written a single file replacement for MessageBox with a changeable font. You can download it here and use it like a standard MessageBox:

http://www.codeproject.com/Articles/601900/FlexibleMessageBox-A-flexible-replacement-for-the

Regards, Jörg

jreichert
  • 1,466
  • 17
  • 31
2

Sounds like you may just want to drop a new form in there and use a few labels..

Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
  • I liked Jon Skeets' comment a little more, since he mentioned specifically using ShowDialog() to bring up the form. That way, you can set a DialogResult form property, and the ShowDialog() call will block execution until the dialog is closed, and the DialogResult is returned. – Lynn Crumbling Mar 24 '11 at 15:06
0

I use this:

public void myMessageBox(object str)
{
    System.Windows.Forms.MessageBox.Show( new System.Windows.Forms.Form{ TopMost = true, Width = 300}, str.ToString() );
}
T.Todua
  • 53,146
  • 19
  • 236
  • 237