Create a Modeless Messagebox

為{幸葍}努か 提交于 2019-12-20 17:26:11

问题


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?


回答1:


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.




回答2:


If you need a message box that just displays itself while your code continues running in the background (the box is still modal and will prevent the user from using other windows until OK is clicked), you can always start the message box in its own thread and continue doing what ever you do in the original thread:

    // Do stuff before.
    // Start the message box -thread:
    new Thread(new ThreadStart(delegate
    {
      MessageBox.Show
      (
        "Hey user, stuff runs in the background!", 
        "Message",
        MessageBoxButtons.OK,
        MessageBoxIcon.Warning
      );
    })).Start();
    // Continue doing stuff while the message box is visible to the user.
    // The message box thread will end itself when the user clicks OK.



回答3:


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.




回答4:


You can use the standard system warning icon using SystemIcons




回答5:


You have to either use form and call showDialog()

And for icons use

MessageBoxIcon.Warning




回答6:


//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.




回答7:


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
}


来源:https://stackoverflow.com/questions/3059487/create-a-modeless-messagebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!