在按winforms应用程序上的按钮时,我试图显示一个消息框,但是MessageBox挂起,永远不会返回值。
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,所以我通过处理后台工作人员上的按钮事件来工作:
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
}
}感谢每一个帮忙的人!
发布于 2014-07-16 12:05:35
您是否尝试指定消息框所有者,如下所示?
DialogResult dr = MessageBox.Show(
this,
"Are you sure you want to enable Emergency Landing mode?",
"Warning!",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);如果在消息框后面显示消息框,同时对UI进行并发更改,那么应用程序就会出现这样的行为。如果上面没有帮助,尝试通过异步处理事件来减轻UI处理(即WinAPI消息传递)的压力,如下所示。
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
}
}发布于 2014-07-16 06:26:49
在MessageBox.Show()方法中尝试在不分配父窗体的情况下显示消息。我不知道为什么,但是,当我加载模态窗口时,我也遇到了类似的问题。
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
{
}
}https://stackoverflow.com/questions/24773452
复制相似问题