Create a Modeless Messagebox

后端 未结 7 1476
难免孤独
难免孤独 2021-02-07 01:00

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 (rat

相关标签:
7条回答
  • 2021-02-07 01:25

    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.
    
    0 讨论(0)
  • 2021-02-07 01:28

    You can use the standard system warning icon using SystemIcons

    0 讨论(0)
  • 2021-02-07 01:32

    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
    }
    
    0 讨论(0)
  • 2021-02-07 01:32

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

    0 讨论(0)
  • 2021-02-07 01:34

    You have to either use form and call showDialog()

    And for icons use

    MessageBoxIcon.Warning

    0 讨论(0)
  • 2021-02-07 01:43

    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.

    0 讨论(0)
提交回复
热议问题