2010-07-30 100 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