[visual-studio]Visual Studio C+ + / CLI 语法错误时使用.Net Windows 形成

发布时间: 2014/3/1 1:17:07
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

下面的代码和 Visual Studio c + + 报告两个错误:

#include "windows.h"

#using <mscorlib.dll>

#using <System.dll>

#using <System.Windows.Forms.dll>

using namespace System::Windows::Forms;

__gc class MyForm : public Form

{

public: 

MyForm()

 { 

     Text = "Hello, Windows Forms!";

     Button* button = new Button();

     button->Text = "Click Me!";

     button->Click += new EventHandler(this, button_click);

     this->Controls->Add(button);

}

void button_click(Object* sender, EventArgs* e)

{

MessageBox::Show("Ouch!");

}

};

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

Application::Run(new MyForm);

}

和的错误: 错误 C2061 语法错误: 事件参数错误 C2061 语法错误: EventHandler

我应该做什么要运行的代码?提前发帖。

解决方法 1:

添加行 '使用命名空间系统 ;'

    using namespace System;

更改到 button_click 作为 ref: & MyForm::button_click

完整的工作代码:

    #include "windows.h" 

    #using <mscorlib.dll> 
    #using <System.dll> 
    #using <System.Windows.Forms.dll> 

    using namespace System::Windows::Forms; 
    using namespace System;

    __gc class MyForm : public Form 
    { 

        public: 

        MyForm() 
        {  

             Text = "Hello, Windows Forms!"; 

             Button* button = new Button();  
             button->Text = "Click Me!"; 

             button->Click += new EventHandler(this, &MyForm::button_click); 

             this->Controls->Add(button);  
        } 

        void button_click(Object* sender, EventArgs* e) 
        { 
            MessageBox::Show("Ouch!"); 
        } 

    }; 

    int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)  
    { 
      Application::Run(new MyForm);  
    } 
赞助商