请问一个OpenFileDialog的使用问题
MSDN上的示例代码如下:
private:
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;
if ( openFileDialog1->ShowDialog() == ::DialogResult::OK )
{
if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
{
// Insert code to read the stream here.
myStream->Close();
}
}
}
但我在实际使用中if ( openFileDialog1->ShowDialog() == ::DialogResult::OK )出错
错误信息为:
1>d:\program\多媒体发布系统\源代码\多媒体发布系统_管理工作站_clr\多媒体发布系统_管理工作站_clr\Form1.h(376): error C3083: “DialogResult”:“::”左侧的符号必须是一种类型
1>d:\program\多媒体发布系统\源代码\多媒体发布系统_管理工作站_clr\多媒体发布系统_管理工作站_clr\Form1.h(376): error C2039: “OK”: 不是“`global namespace'”的成员
1>d:\program\多媒体发布系统\源代码\多媒体发布系统_管理工作站_clr\多媒体发布系统_管理工作站_clr\Form1.h(376): error C2065: “OK”: 未声明的标识符
1>
1>生成失败。
跪求详解!!
[解决办法]
不太了解cli的语法,但是改成下面的没有问题
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
[解决办法]
找到问题答案了:)
The :: in front of DialogResult means DialogResult is a global identifier. The problem occurs when your using namespace declarations are inside of your local namespace.
The solution is to move those statements outside of and before your form application namespace.
*** THIS IS A PROBLEM ***
namespace myApp {
using namespace System;
using namespace System::Windows::Forms;
if ( result == :ialogResult::Yes ){
}
}
************************************
*** THIS FIXES THE PROBLEM ***
using namespace System;
using namespace System::Windows::Forms;
namespace myApp {
if ( result == :ialogResult::Yes ){
}
}
*****************************
来自:
http://social.msdn.microsoft.com/Forums/eu/Vsexpressinstall/thread/c97c2d37-c56a-4213-a889-4cc9e2ca259c