views:

281

answers:

6

How might one go about creating a Modeless MessageBox? Do I have to just create my own Windows Form class and use that? If so, is there an easy way of adding a warning icon (rather than inserting my own image of one) and resizing based on text volume?

A: 

You can use the standard system warning icon using SystemIcons

Shay Erlichmen
A: 

You have to either use form and call showDialog()

And for icons use

MessageBoxIcon.Warning

prashant
`ShowDialog` will make it Modal. `Show` is for Modeless, I believe.
Smashery
sorry i was confused..
prashant
A: 

Note: this will create a Modal dialog box, which is not what the question is asking

here is a sample code

if (MessageBox.Show("Description of the message", "Caption text", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
{
    // Do some stuff if yes pressed
}
else
{
    // no pressed
}
ghiboz
I'm after a modeless dialog; not a modal one.
Smashery
This answer was receiving votes, despite being the opposite of what I asked for (modeless). So that others don't think this answer will solve this particular problem, I have edited the response with a disclaimer.
Smashery
+2  A: 

You'll have to create a Form and use Show() to display it Modeless. MessageBox.Show(...) behaved Modal as seen in the example by ghiboz; "Description of the message" is displayed until the user presses a button.

With MessageBox.Show(...) you get the result as soon as the messagebox is closed; with a modeless message box, your code will have to have a mechanism such as an event to react when the user eventually selects something on your message box.

JLWarlow
This was my initial implementation - unfortunately, it causes my program to crash when being launched from a background process for some reason. Nonetheless, there doesn't seem to be any better solution. Thanks.
Smashery
+1  A: 

Short of writing the code, you could create a small form that in the constructor does the following

  • Takes a parameter string as the message to display
  • Fills up a label on the form with this string
  • Loads an icon with one of the following (pass in an Enum to the constructor)
    • SystemIcons.Application
    • SystemIcons.Asterix
    • SystemIcons.Error
    • SystemIcons.Exclamation
    • SystemIcons.Hand
    • SystemIcons.Information
    • SystemIcons.Question
    • SystemIcons.Shield
    • SystemIcons.Warning
    • SystemIcons.WinLogo
  • Calls Show() which will cause it to be a modal dialog

If you really wanted, you could listen to an event that is fired when the OK button is pushed.

Stefan Valianu
A: 

//no commnet

object sync = new object();
ManualResetEvent Wait = new ManualResetEvent();
//you should create a place holder named MessageData for Message Data.
List<MessageData> Messages = new List<MessageData>();
internal void ShowMessage(string Test, string Title, ....)
{
    MessageData MSG = new MessageData(Test, Title);
    Wait.Set();
    lock(sync) Messages.Add(MSG);
}
// another thread should run here.
void Private_Show()
{
    while(true)
{
        while(Messsages.Count != 0)
        {
            MessageData md;
            lock(sync)
            {
                md = List[0];
                List.RemoveAt(0);
            }
            MessageBox.Show(md.Text, md.Title, md....);
        }
        Wait.WaitOne();
    }
}

needs more threads and more code(I don't have enough time to write) for concurrent messageboxes.

Behrooz