代码之家  ›  专栏  ›  技术社区  ›  Smashery

创建无模式Messagebox

  •  21
  • Smashery  · 技术社区  · 15 年前

    如何创建无模式的MessageBox?我必须创建自己的Windows窗体类并使用它吗?如果是这样的话,有没有一种简单的方法可以添加一个警告图标(而不是插入我自己的图片)并根据文本大小调整大小?

    7 回复  |  直到 15 年前
        1
  •  6
  •   John Warlow    15 年前

    你必须创建一个窗体并使用 Show() MessageBox.Show(...) 在吉布斯的例子中看到的行为模式直到用户按下一个按钮,信息“的描述”才会显示。

    MessageBox.Show(…)

        2
  •  52
  •   tojotamies    14 年前

    如果您需要一个只在代码继续在后台运行时显示自身的消息框(该框仍然是模态的,在单击“确定”之前将阻止用户使用其他窗口),则可以始终在其自己的线程中启动消息框,并继续执行在原始线程中执行的操作:

        // 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
  •  1
  •   Stefan Valianu    15 年前

    在编写代码之前,您可以创建一个小表单,在构造函数中执行以下操作

    • 将参数字符串作为要显示的消息
    • 用此字符串填充窗体上的标签
    • 加载具有以下内容之一的图标(将枚举传递给构造函数)
      • SystemIcons.Application
      • SystemIcons.Asterix
      • SystemIcons.Error
      • SystemIcons.Exclamation
      • SystemIcons.Hand
      • SystemIcons.Information
      • SystemIcons.Question
      • SystemIcons.Shield
      • SystemIcons.Warning
      • SystemIcons.WinLogo

    如果你真的想,你可以听一个事件,当确定按钮被按下时触发。

        4
  •  0
  •   Shay Erlichmen    15 年前

    您可以使用标准系统警告图标 SystemIcons

        5
  •  0
  •   prashant    15 年前

    您必须使用form并调用showDialog()

    和图标使用

    MessageBoxIcon.Warning

        6
  •  -1
  •   Smashery    15 年前

    注意:这将创建一个模式对话框,而不是问题所要问的

    if (MessageBox.Show("Description of the message", "Caption text", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
    {
        // Do some stuff if yes pressed
    }
    else
    {
        // no pressed
    }
    
        7
  •  -1
  •   Behrooz    15 年前

    //没有通讯网

    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();
        }
    }
    

    需要更多的线程和代码(我没有足够的时间来编写)来实现并发MessageBox。

    推荐文章