×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How do I dynamically assign label control names through a loop?

How do I dynamically assign label control names through a loop?

How do I dynamically assign label control names through a loop?

(OP)
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::Drawing;
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);
}

 

RE: How do I dynamically assign label control names through a loop?

A name in a compiled language is used by the compiler and doesn't have any real meaning at runtime; i.e., it's a compile-time entity.  Since you are creating objects at run time, there isn't any way to create a fixed name.  You can put the object into a variable, and then refer to it that way, like you're doing here (the variable named 'a') -- all you need to do is have names for each label.  If you want to handle a variable number of labels, you could create an array, place each label into the array, and then refer to items as a[0], a[1], etc.
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close