.NET中使用VC++语言如何为C#设计的窗口添加事件?
我先创建了一个C#语言的项目,添加一个Form,名称为CSharpForm,并使该项目的输出为类库;然后在解决方案中添加一个VC++ CLR WindowsForm的项目,并添加CSharpForm类库的引用。
我想在VC项目中使用CSharpForm窗口的事件,比如我在VC代码中定义一个CSharpForm的对象,并为其Closed事件添加处理方法,我这样写:
CSharp_CLI::CSharpForm^ form=gcnew CSharpForm();//CSharp_CLI是C#项目的命名空间
form->Closed+=gcnew EventHandler(&CLR_Form::Form1::OnFormClose);
form->Show();
OnFormClose方法定义如下:
System::Void OnFormClose(System::Object^ sender,System::EventArgs^ args)
{
System::Windows::Forms::MessageBox::Show("form closed");
}
编译时老是出错:
'void CLR_Form::Form1::OnFormClose(System::Object ^,System::EventArgs ^)' :
the specified function does not match the delegate type 'void (System::Object ^,System::EventArgs ^)'
哪位大侠知道该怎么定义OnFormClose啊? --------------------编程问答-------------------- 自己写的不对,改成
form->FormClosed +=gcnew System::Windows::Forms::FormClosedEventHandler(this,&Form1::OnFormClose);就可以了 --------------------编程问答--------------------
顶
补充:.NET技术 , C#