当用户点击该表单上的“确定”按钮时,我试图获得一个返回值的窗口。
我正在尝试使用this code作为指南,但我一直从标题中得到错误:
Operator '==' cannot be applied to operands of type 'bool?' and 'System.Windows.Forms.DialogResult'
这是我的代码:
using (var form = new MyNewForm())
{
if (form.ShowDialog() == DialogResult.OK) //line with error
{
string val = form.ReturnValue1;
string dateString = form.ReturnValue2;
//do stuff
}
}
我有读取this主题,这就是我将if
更改为form.ShowDialog()
的原因,但错误仍然存在。
答案 0 :(得分:5)
using (var form = new MyNewForm())
{
if (form.ShowDialog() ?? false /*== DialogResult.OK*/) //line with error
{
string val = form.ReturnValue1;
string dateString = form.ReturnValue2;
//do stuff
}
}
使用null合并运算符??