winformsc++-clivisual-c++-2013

VS 2013 winform application: how to convert generic object to button type


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";
    }
};
}

Solution

  • 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:

    1. In the GUI designer for the form, click on each button, and then look at the Properties window in Visual STudio
    2. Ensure the "events" button (with the lightning bolt icon) in the Properties browser is pressed (enabled)
    3. Look for the "CLick" event
    4. In the adjacent dropdown, select 'allButtons_click'
    5. Repeat the process for each of the keypad buttons.

    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.