This may seem a little bit fundamental but I am relatively new to C++ and cannot find the answer anywhere so far.
I need to create a new button on a form when the user clicks on the button that is there already. I also need to set the on click event to be an avent that is defined in my code already.
I have tried this:
Button btn1 = gcnew Button();
but I got an error: class does not have a copy-constructor
I assume you are working with Visual C++/CLI using Windows Forms...
I also assume that if you are already trying to create various controls dynamically, you will be able to answer on your request by yourself - just check how it's done when you drag various components by using the designer :)
anyway, this is what you need:
1 2 3 4 5 6 7 8 9
//"click" event handler for button1 generated automatically, to do it just click twice on the button in the Form designer
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//at first we need to create and initialize the button
Button^ button2 = gcnew System::Windows::Forms::Button();
//then we have to add it to our Form
this->Controls->Add(button2);
//then optionally assign various properties to your button, e.g. Location, Size, Text, Tag, event handlers etc...
button2->Location = System::Drawing::Point(19, 52);
}
EDIT
I also need to set the on click event to be an avent that is defined in my code already.
to define event handler for the new button, you just created, add this to the code above: