开发者

C++ .NET Give 2 buttons the same click function?

开发者 https://www.devze.com 2023-01-09 09:50 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号