C++ .NET Give 2 buttons the same click function?
First of, is making a Windows Form Application from the CLR section in the new project window called .NET or something else? I just want to know so I can search things on it better.
How do I distinguish between the individual buttons if I gave them both the same click function?
this->button1->Click += gcnew System::EventHandler(this, &test::button1_Click);
this->button2->Click += gcnew System::EventHandler(this, &test::button1_Click);
private: System::Void button1_Clic开发者_如何学运维k(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show (Convert::ToString (sender));
}
};
That shows me System.Windows.Forms.Button, Text:button1 or button2
Fastest method I thought of was to do if statements using the Text but how do I actually access the Text property of the sender object?
Edit: Maybe I'm doing it wrong but I added
Button button = sender as Button
right above the MessageBox line and I got
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'
How do I distinguish between the individual buttons if I gave them both the same click function?
by sender
how do I actually access the Text property of the sender object? convert sender to Button type and call Text property.
Button^ button = (Button^)sender ;
button->Text;
actually it's not good idea to search buttons by Text prop. you'd better search by Name or Id.
精彩评论