The following is a sample code I am working on. I can dynamically create three labels and make them appear on the form.
I can even make them work exactly the same btn1_Click function. But, how can I dynamically assign the label names to each label, so I can use them to identify the labels and change their values.
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System:rawing;
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
public:
String^ a;
Form1() {
for (int i=0; i<=2; i++) {
Label^ a= gcnew Label;
a->Text = Convert::ToString(i);
a->Name = "btn"+Convert::ToString(i);
a->Size = Drawing::Size(25, 25);
a->Left = 10+i*30;
a->Top = 10;
this->Controls->Add(a);
Console::WriteLine(a->Name);
a->Click += gcnew System::EventHandler(this, &Form1::btn1_Click);
}
}
private: System::Void btn1_Click(System::Object^ sender, System::EventArgs^ e) {
btn2->Text ="Hi";
}
};
[STAThread]
int main() {
Application::Run(gcnew Form1);
}
I can even make them work exactly the same btn1_Click function. But, how can I dynamically assign the label names to each label, so I can use them to identify the labels and change their values.
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System:rawing;
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
public:
String^ a;
Form1() {
for (int i=0; i<=2; i++) {
Label^ a= gcnew Label;
a->Text = Convert::ToString(i);
a->Name = "btn"+Convert::ToString(i);
a->Size = Drawing::Size(25, 25);
a->Left = 10+i*30;
a->Top = 10;
this->Controls->Add(a);
Console::WriteLine(a->Name);
a->Click += gcnew System::EventHandler(this, &Form1::btn1_Click);
}
}
private: System::Void btn1_Click(System::Object^ sender, System::EventArgs^ e) {
btn2->Text ="Hi";
}
};
[STAThread]
int main() {
Application::Run(gcnew Form1);
}