我在使用C ++创建Click事件时遇到了一些问题

时间:2013-08-01 06:32:58

标签: c++ visual-studio events textbox click

我在Visual Studio 2008 C ++中遇到了在TextBox上创建Click事件的问题。我有一个TextBoxs数组,我需要在Click事件上添加它们。我试着这样:

点击功能:

private: System::Void ButtonClickHandler(System::Object^  sender, System::EventArgs^  e) 
{
    //something   

}

创建点击事件的代码:

this->text_edit[i]->Click +=gcnew System::EventHandler(this, &New::ButtonClickHandler);

代码在此处:

for(i=0; i < howMany; i++)
{
    this->text_edit[i]=(gcnew System::Windows::Forms::TextBox());  
    this->text_edit[i]->AutoSize = true;  
    this->text_edit[i]->Location = System::Drawing::Point(where_X_Edit, where_Y_Edit);  
    this->text_edit[i]->Name = interf[i];  
    this->text_edit[i]->Size = System::Drawing::Size(90, 30);  
    this->text_edit[i]->TabIndex = 0;  
    this->text_edit[i]->Text = interf[i];
    this->text_edit[i]->Enabled=false;

    this->text_edit[i]->Click +=gcnew System::EventHandler(this, &New::ButtonClickHandler);

    panel->Controls->Add(this->text_edit[i]);
    where_Y_Edit += 32; 
}

问题在于创建click事件,仅在该行代码处出现9个错误。

Error   2   error C2653: 'New' : is not a class or namespace name   c:\users\uidw7119\documents\visual studio 2008\projects\datalogger\datalogger\DataView_Form.h   502
Error   3   error C2276: '&' : illegal operation on bound member function expression    c:\users\uidw7119\documents\visual studio 2008\projects\datalogger\datalogger\DataView_Form.h   502
Error   4   error C3350: 'System::EventHandler' : a delegate constructor expects 2 argument(s)  c:\users\uidw7119\documents\visual studio 2008\projects\datalogger\datalogger\DataView_Form.h   502
Error   13  error C2653: 'New' : is not a class or namespace name   c:\users\uidw7119\documents\visual studio 2008\projects\datalogger\datalogger\DataView_Form.h   502
Error   14  error C2276: '&' : illegal operation on bound member function expression    c:\users\uidw7119\documents\visual studio 2008\projects\datalogger\datalogger\DataView_Form.h   502
Error   15  error C3350: 'System::EventHandler' : a delegate constructor expects 2 argument(s)  c:\users\uidw7119\documents\visual studio 2008\projects\datalogger\datalogger\DataView_Form.h   502
Error   17  error C2653: 'New' : is not a class or namespace name   c:\users\uidw7119\documents\visual studio 2008\projects\datalogger\datalogger\DataView_Form.h   502
Error   18  error C2276: '&' : illegal operation on bound member function expression    c:\users\uidw7119\documents\visual studio 2008\projects\datalogger\datalogger\DataView_Form.h   502
Error   19  error C3350: 'System::EventHandler' : a delegate constructor expects 2 argument(s)  c:\users\uidw7119\documents\visual studio 2008\projects\datalogger\datalogger\DataView_Form.h   502

这个问题比第一个问题更重要: 如果有人知道可以在TextBox上创建一个Click事件,该事件将具有System::Object^ sender, System::EventArgs^ e的其他参数的函数,如下所示:void ClickBtn(int value, String ^ something)

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。

创建Click事件的正确代码是:

this->text_edit[i]->Click += gcnew System::EventHandler(this, &DataView_Form::TextBoxClick);

TextBoxClick是问题中的函数ButtonClickHandler

DataView_Form是我的表格:

public ref class DataView_Form : public System::Windows::Forms::Form { //code here };