首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MessageBox挂起

MessageBox挂起
EN

Stack Overflow用户
提问于 2014-07-16 06:14:19
回答 2查看 1.9K关注 0票数 2

在按winforms应用程序上的按钮时,我试图显示一个消息框,但是MessageBox挂起,永远不会返回值。

代码语言:javascript
复制
private void btnApply_Click(object sender, EventArgs e)
    {
        bool current = false;
        if (cmbEmergencyLandingMode.SelectedIndex > 0)
        {
            if (m_WantedData != false)
            {
                DialogResult dr = MessageBox.Show("Are you sure you want to enable Emergency Landing mode?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                //i never get to this point
                current = (dr == DialogResult.Yes);
            }
        }

        if (m_WantedData == current)
        {
            //do something
        }
        else if (m_WantedData != null)
        { 
            //do something else
        }
    }

编辑: Ok,所以我通过处理后台工作人员上的按钮事件来工作:

代码语言:javascript
复制
private void btnApply_Click(object sender, EventArgs e)
    {
        if (!bwApply.IsBusy) 
            bwApply.RunWorkerAsync(cmbEmergencyLandingMode.SelectedIndex);
    }

    void bwApply_DoWork(object sender, DoWorkEventArgs e)
    {
        bool current = false;
        int selectedIndex = (int)e.Argument;

        if (selectedIndex > 0)
        {
            if (m_WantedData != false)
            {
                DialogResult dr = MessageBox.Show(
                    "Are you sure you want to enable Emergency Landing mode?",
                    "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                current = (dr == DialogResult.Yes);
            }
        }

        if (m_WantedData == current)
        {
            //do something
        }
        else if (m_WantedData != null)
        {
            //do something else
        }
    }

感谢每一个帮忙的人!

EN

回答 2

Stack Overflow用户

发布于 2014-07-16 12:05:35

您是否尝试指定消息框所有者,如下所示?

代码语言:javascript
复制
DialogResult dr = MessageBox.Show(
    this,
    "Are you sure you want to enable Emergency Landing mode?",
    "Warning!",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Warning);

如果在消息框后面显示消息框,同时对UI进行并发更改,那么应用程序就会出现这样的行为。如果上面没有帮助,尝试通过异步处理事件来减轻UI处理(即WinAPI消息传递)的压力,如下所示。

代码语言:javascript
复制
public delegate void ApplyDelegate();

private void btnApply_Click(object sender, EventArgs e)
{
   btnApply.BeginInvoke(new ApplyDelegate(ApplyAsync));
}

private void ApplyAsync()
{
    bool current = false;
    if (cmbEmergencyLandingMode.SelectedIndex > 0)
    {
        if (m_WantedData != false)
        {
            DialogResult dr = MessageBox.Show(
                this,
                "Are you sure you want to enable Emergency Landing mode?",
                "Warning!",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            //i never get to this point
            current = (dr == DialogResult.Yes);
        }
    }

    if (m_WantedData == current)
    {
        //do something
    }
    else if (m_WantedData != null)
    { 
        //do something else
    }
}
票数 1
EN

Stack Overflow用户

发布于 2014-07-16 06:26:49

MessageBox.Show()方法中尝试在不分配父窗体的情况下显示消息。我不知道为什么,但是,当我加载模态窗口时,我也遇到了类似的问题。

代码语言:javascript
复制
if (m_WantedData != false)
{
    if (MessageBox.Show("Sample Message", "Are you sure you want to enable Emergency Landing mode?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
    {
        //DO YOUR STUFF HERE IF YES
    }
    else
    {

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24773452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档