2010-07-30 97 views
0

首先,是在.NET项目或其他项目的新项目窗口中从CLR部分创建一个Windows窗体应用程序?我只是想知道,所以我可以更好地搜索它。C++ .NET为2个按钮提供相同的点击功能?

如果我给他们两个相同的点击功能,我该如何区分各个按钮?

this->button1->Click += gcnew System::EventHandler(this, &test::button1_Click); 
this->button2->Click += gcnew System::EventHandler(this, &test::button1_Click); 

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 
MessageBox::Show (Convert::ToString (sender)); 
} 

};

这表明我System.Windows.Forms.Button,文本:Button1的或BUTTON2

我认为

最快的方法是,如果使用文本语句做的,但实际上,我怎么访问发送对象的Text属性?

编辑: 也许我做错了,但我加

Button button = sender as Button 

权在MessageBox线以上和我

System::Windows::Forms::Button' : class does not have a copy-constructor 
syntax error : missing ';' before identifier 'as' 
error C2065: 'as' : undeclared identifier 
syntax error : missing ';' before identifier 'Button' 
System::Windows::Forms::Button' : illegal use of this type as an expression 
see declaration of 'System::Windows::Forms::Button' 
+0

也许标签'managed-C++'? – 2010-07-30 21:09:52

+0

它被称为C++ CLI。 – Puppy 2010-07-31 10:18:41

回答

2

如何将各个按钮,如果区分我给他们两个相同的点击功能?

发件人

实际上,我怎么访问发送对象的Text属性? 将发件人转换为按钮类型并调用Text属性。

Button^ button = (Button^)sender ; 
button->Text; 

实际上这不是好主意,通过文字道具搜索按钮。你最好按名称或编号进行搜索。

+0

它的工作!尽管我没有在任何地方看到ID,但我只能找到Name和TabIndex。 – TreeTree 2010-07-31 13:59:29