Click here to Skip to main content
15,914,071 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
QuestionCreating a global array of labels at runtime Pin
Douglas Kirk31-Mar-13 11:30
Douglas Kirk31-Mar-13 11:30 
I have an old Borland C++ project that displayed a 16 x 16 group of labels displaying 0x00 to 0xFF for the purpose of diagnostics.

I am now using MicroSoft Visual Studio 2010 C++

I am having a problem creating an array of labels at runtime that the labels are accessable by other routines such as a timer interval routine

C++
#pragma once
#include <stdio.h>

namespace SerialTester {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1

int count;
int tindex;
    

/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
        
    Form1(void)
    {
      InitializeComponent();
      count = 0;
      tindex = 0;
      char buffer[200];
      array< Label ^ > ^ labels;
      labels = gcnew array<Label^>(256);

      for (int index = 0; index < 256; ++index) 
      {
         Label ^ label = gcnew Label;
         label->Size = System::Drawing::Size(25, 25);
         label->Name = "C" + index;
         sprintf(buffer,"%2X",index);
         label->Text = Convert::ToString( index, 16 ); 

         String^ clistr = gcnew String(buffer);
         //label->Text = clistr; //buffer;
         Controls->Add(label);
         labels[index] = label;
      }
      //
      //TODO: Add the constructor code here
      //
    }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::Panel^  panel1;
    private: System::Windows::Forms::Timer^  timer1;
    private: System::ComponentModel::IContainer^  components;
    protected: 

    private:
    /// <summary>
    /// Required designer variable.
    /// </summary>


#pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method  do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
      this->components = (gcnew System::ComponentModel::Container());
      this->label1 = (gcnew System::Windows::Forms::Label());
      this->panel1 = (gcnew System::Windows::Forms::Panel());
      this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
      this->SuspendLayout();
      // 
      //
      this->Controls->Add(this->panel1);
      this->Controls->Add(this->label1);
      this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
      this->ResumeLayout(false);
   }

#pragma endregion
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
                 count++;
                 label1->Text = Convert::ToString( count, 10 );
                
             }
    private: System::Void panel1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
             }



private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) 
{
 labels[tindex++]->BackColor = System::Drawing::Color::SkyBlue;
 tindex%=256;
}
};
}



So when I create the form I created an array of 256 Labels at runtime, labels[x] but in the timer1_Tick I can not access them because the array is not global.

QUESTION: How can I do this, should my creation be somewheres else so that it is global.

QUESTION: in my Borland project I simply used sprintf to format the caption of each label to 00 .. FF, but can not seem to format the text simply in upper case.

Any help from anyone would be greatly appriciated.

Or a link to a tutorial on this.

Thank you in advance
Douglas
AnswerRe: Creating a global array of labels at runtime Pin
NotPolitcallyCorrect31-Mar-13 14:10
NotPolitcallyCorrect31-Mar-13 14:10 
GeneralRe: Creating a global array of labels at runtime Pin
Douglas Kirk31-Mar-13 14:38
Douglas Kirk31-Mar-13 14:38 
GeneralRe: Creating a global array of labels at runtime Pin
NotPolitcallyCorrect31-Mar-13 15:22
NotPolitcallyCorrect31-Mar-13 15:22 
GeneralRe: Creating a global array of labels at runtime Pin
Douglas Kirk31-Mar-13 15:56
Douglas Kirk31-Mar-13 15:56 
GeneralRe: Creating a global array of labels at runtime Pin
NotPolitcallyCorrect31-Mar-13 16:26
NotPolitcallyCorrect31-Mar-13 16:26 
QuestionPassing a C# string to C++/CLI does not show up from a CPP program Pin
SujayG26-Mar-13 0:24
SujayG26-Mar-13 0:24 
AnswerRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
David Knechtges26-Mar-13 8:50
David Knechtges26-Mar-13 8:50 
GeneralRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
SujayG26-Mar-13 21:33
SujayG26-Mar-13 21:33 
AnswerRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
Richard MacCutchan26-Mar-13 22:59
mveRichard MacCutchan26-Mar-13 22:59 
GeneralRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
SujayG27-Mar-13 3:54
SujayG27-Mar-13 3:54 
GeneralRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
Richard MacCutchan27-Mar-13 3:58
mveRichard MacCutchan27-Mar-13 3:58 
GeneralRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
SujayG27-Mar-13 4:11
SujayG27-Mar-13 4:11 
GeneralRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
Richard MacCutchan27-Mar-13 4:19
mveRichard MacCutchan27-Mar-13 4:19 
GeneralRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
SujayG27-Mar-13 4:52
SujayG27-Mar-13 4:52 
GeneralRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
Richard MacCutchan27-Mar-13 5:03
mveRichard MacCutchan27-Mar-13 5:03 
GeneralRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
SujayG29-Mar-13 19:09
SujayG29-Mar-13 19:09 
GeneralRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
Richard MacCutchan29-Mar-13 23:00
mveRichard MacCutchan29-Mar-13 23:00 
AnswerRe: Passing a C# string to C++/CLI does not show up from a CPP program Pin
jschell30-Mar-13 13:33
jschell30-Mar-13 13:33 
QuestionDidn't Understand to logic of calculation of this program.? Pin
Taqi_Shah11-Mar-13 7:26
Taqi_Shah11-Mar-13 7:26 
AnswerRe: Didn't Understand to logic of calculation of this program.? Pin
NotPolitcallyCorrect11-Mar-13 7:55
NotPolitcallyCorrect11-Mar-13 7:55 
SuggestionRe: Didn't Understand to logic of calculation of this program.? Pin
MicroVirus13-Mar-13 12:16
MicroVirus13-Mar-13 12:16 
QuestionNeed Urgent Help!! Pin
Member 933160011-Mar-13 2:18
Member 933160011-Mar-13 2:18 
AnswerRe: Need Urgent Help!! Pin
Marco Bertschi14-Mar-13 10:36
protectorMarco Bertschi14-Mar-13 10:36 
Questionhow to call vcvarsall.bat with Process::Start method? Pin
aupres8-Mar-13 23:43
aupres8-Mar-13 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.