Home > Enterprise >  Creating an array from already created labels
Creating an array from already created labels

Time:10-06

I am using Visual Studio 2022, C

I would like to create an array of already created labels. The reason for this is printing on labels, which are in another form, through a for loop, in order to shorten the code, according to labels[i] labels[0] - label1, labels[1] - label2...

postavke.cpp(Form2), kontrolna is Form1


kontrolna^ kontrolna_name = gcnew kontrolna();
    
    for (int i = 0; i < 7; i  ) {

        kontrolna_name->labels[i]->Text = " ";
    }

Does anyone have an idea how to perform this?

CodePudding user response:

You need to use a CLI array. You can create an array of labels as follows cli::array<System::Windows::Forms::Label^ >^ Labels = gcnew cli::array<System::Windows::Forms::Label^>(4) { label1, label2, label3, label4 };

where 4 is the number of elements in the array. Above utilized an initialization list but it can be also be declared empty and populated after such as

cli::array<System::Windows::Forms::Label^ >^ Labels = gcnew cli::array<System::Windows::Forms::Label^>(4); 
Labels[0] = label1;
Labels[1] = label2;
Labels[2] = label3;
Labels[3] = label4;

CodePudding user response:

Thanks a lot! I managed that part, but even though there are no bugs in the code and the program works, the labels from the second form (kontrolna) do not change values.

I took advantage

kontrolna^ kontrolna_name = gcnew kontrolna();

....

kontrolna_name->labels[i]->Text = " ";

Should something be changed? I linked the header files normally.

  • Related