C#错误:运算符'=='不能应用于'System.Windows.Forms.DialogResult'和'bool'类型的操作数, [英] C# error: Operator '==' cannot be applied to operands of type 'System.Windows.Forms.DialogResult' and 'bool'
问题描述
在Visual Studio C#Express上,当我运行下面的脚本时,在线上出现以下错误消息:
On Visual Studio C# Express when I run the script below, I get the following error message on the line saying:
if ofd.ShowDialog()== true):
错误1运算符'=='不能应用于类型为'System.Windows.Forms.DialogResult'和'bool'的操作数
if (ofd.ShowDialog() == true): Error 1 Operator '==' cannot be applied to operands of type 'System.Windows.Forms.DialogResult' and 'bool'
我怎么能解决这个问题?代码如下:
How could I solve this? Code below:
public override GH_ObjectResponse RespondToMouseDoubleClick(GH_Canvas sender, GH_CanvasMouseEvent e)
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*";
if (ofd.ShowDialog() == true)
{
string[] filePath = ofd.FileNames;
string[] safeFilePath = ofd.SafeFileNames;
}
return base.RespondToMouseDoubleClick(sender, e);
}
推荐答案
我怀疑你已经阅读 WPF OpenFileDialog.ShowDialog
文档方法结果是 Nullable< bool>
。但是,如果您使用的是 Windows窗体 OpenFileDialog.ShowDialog
,它返回 DialogResult code>
- 你显然无法与 bool
进行比较。
I suspect you've been reading the WPF OpenFileDialog.ShowDialog
documentation where the method result is Nullable<bool>
. If, however, you're using Windows Forms OpenFileDialog.ShowDialog
, that returns DialogResult
- which you clearly can't compare with bool
.
看看 DialogResult
并看看你实际上想要做什么。请注意,文档声明:
Have a look at DialogResult
and see what you actually want to do. Note that the documentation claims:
返回:DialogResult.OK,如果用户在对话框中单击确定;否则,DialogResult.Cancel。
Return: DialogResult.OK if the user clicks OK in the dialog box; otherwise, DialogResult.Cancel.
...所以这些应该是唯一需要考虑的情况。
... so those should be the only cases you need to consider.
这篇关于C#错误:运算符'=='不能应用于'System.Windows.Forms.DialogResult'和'bool'类型的操作数,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!