sobota, 7 stycznia 2023

Dynamic form and components (Visual C++)

 


private:

Form^ my_Form;

Button^ info_Button;

Button^ close_Button;

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

}

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

my_Form = gcnew Form;

info_Button = gcnew Button;

close_Button = gcnew Button;

info_Button->Text = "Date";

close_Button->Text = "Close";

info_Button->Location = Point(10, 10);

close_Button->Location = Point(10, 100);

my_Form->Controls->Add(info_Button);

my_Form->Controls->Add(close_Button);

info_Button->Click += gcnew EventHandler(this, &Form1::info_Button_Click);

close_Button->Click += gcnew EventHandler(this, &Form1::close_Button_Click);

my_Form->Show();

}

private:

System::Void close_Button_Click(System::Object^ sender, System::EventArgs^ e)

{

my_Form->Close();

}

private:

System::Void info_Button_Click(System::Object^ sender, System::EventArgs^ e)

{

DateTime^ now_Date = gcnew DateTime;

now_Date = DateTime::Now;

int width, height;

width = my_Form->Width;

height = my_Form->Height;

Label^ date_Label = gcnew Label;

String^ temp_Format = now_Date->Year.ToString()+"-"+now_Date->Month.ToString() +

"-"+now_Date->Day.ToString();

date_Label->Text = temp_Format;

date_Label->Location = Point(width / 2, height / 2);

((Button^)sender)->Parent->Controls->Add(date_Label);

}