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?
You can use the standard system warning icon using SystemIcons
You have to either use form and call showDialog()
And for icons use
MessageBoxIcon.Warning
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
}
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.
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.
//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.