【发布时间】:2014-07-16 06:14:19
【问题描述】:
我试图在按下 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
}
}
编辑:好的,所以我通过处理后台工作人员上的按钮事件来让它工作:
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
}
}
感谢所有帮助过的人!
【问题讨论】:
-
你的代码应该可以工作...
-
它工作正常,在我做了一些与这段代码无关的更改后,它开始挂起,整个应用程序卡住了。
-
你重建你的应用了吗? (F6) 此外,您可能需要删除 ..\Projects\Debug 文件夹中的旧 .exe 文件
-
“在我做了一些与这段代码无关的更改后,它开始挂起”,听起来那是你真正的问题