i am designing windows calculator on visual studio 2013 using winform application.i want to write a single button handler for the numeric buttons on a WinForms application that simulates a calculator keypad.but in order to do this i must first convert the object^ sender to button type have .
already tried to do it this way:
Button generic = Button sender;
but it give the following errors:
Error 3 error C1506: unrecoverable block scoping
Error 4 IntelliSense: type name is not allowed
here is the part of myform.h
that i need to code:
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void button12_Click(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void button9_Click(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void button3_Click(System::Object^ sender,
System::EventArgs^ e) {
//this is the button i want to change to a generic button
//Button generic = Button sender;
results->Text += "1";
}
};
}
I'm not exactly sure I understand the issue, but based on what I think I understand I'm going to try and answer.
It sounds like you want to write a single button handler for the numeric buttons on a WinForms application that simulates a calculator keypad. You can do this by writing a method with a signature for a button handler event, and then point the click event handler of each keypad button to that single function. The "sender" parameter should hold the actual source of the click; you will have to interrogate some aspect of the button to determine what number it represents.
Edit - Revising a bit for clarification
Assuming that you've already written the shell for your single event handler, which is an event method of the signature you've shown above, and named that handler 'allButtons_click', of the general form here:
`
private: System::Void allButtons_Click(System::Object^ sender, System::EventArgs^ e) {
Button^ foo = (Button^)sender;
// do stuff
}
`
)
To make each button respond to the same Click event handler within the IDE:
To cast the sender parameter of the Click event handler to a button, try
Button^ myButton = (Button^)sender;
You must then interrogate each button to determine which value it represents.
You could do this at runtime when the form loads, but I'm inferring your objective is to accomplish this in the IDE.