Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Creating a global array of labels at runtime

Creating a global array of labels at runtime

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++delphidatabasevisual-studio
6 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Douglas Kirk
    wrote on last edited by
    #1

    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

    #pragma once
    #include 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 for Form1

    int count;
    int tindex;

    ///
    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:
        /// /// Clean up any resources being used.
        /// 
        ~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:
    /// /// Required designer variable.
    /// 
    

    #pragma region Windows Form Designer generated code
    /// /// Required method do not modify
    /// the contents of this method with the code editor.
    ///
    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

    L 1 Reply Last reply
    0
    • D Douglas Kirk

      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

      #pragma once
      #include 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 for Form1

      int count;
      int tindex;

      ///
      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:
          /// /// Clean up any resources being used.
          /// 
          ~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:
      /// /// Required designer variable.
      /// 
      

      #pragma region Windows Form Designer generated code
      /// /// Required method do not modify
      /// the contents of this method with the code editor.
      ///
      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      1. Yes you need to move the array declaration out of the constructor. As it is now, the array is a local variable to the constructor and not accessible outside of the constructor. Read up on variable scope. 2. Use the string class ToUpper() method to make the string all uppercase.

      D 1 Reply Last reply
      0
      • L Lost User

        1. Yes you need to move the array declaration out of the constructor. As it is now, the array is a local variable to the constructor and not accessible outside of the constructor. Read up on variable scope. 2. Use the string class ToUpper() method to make the string all uppercase.

        D Offline
        D Offline
        Douglas Kirk
        wrote on last edited by
        #3

        Hello Thank you for your reply I had tried declaring the following after the global int count; up at the top of the example

        array< Label ^ > ^ labels;
        labels = gcnew array(256);

        But I got an error saying I could declare on an unmanaged heap Any thoughts or is this the wrong place. My help on visual c++ does not work as nice as the c# version does :( Thank you Douglas

        L 1 Reply Last reply
        0
        • D Douglas Kirk

          Hello Thank you for your reply I had tried declaring the following after the global int count; up at the top of the example

          array< Label ^ > ^ labels;
          labels = gcnew array(256);

          But I got an error saying I could declare on an unmanaged heap Any thoughts or is this the wrong place. My help on visual c++ does not work as nice as the c# version does :( Thank you Douglas

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Without seeing more of what you did, it's hard to tell but it should look something like:

          private array< Label ^ > ^ labels;
          Form1(void)
          {
            // the rest of your constructor here
          }
          
          D 1 Reply Last reply
          0
          • L Lost User

            Without seeing more of what you did, it's hard to tell but it should look something like:

            private array< Label ^ > ^ labels;
            Form1(void)
            {
              // the rest of your constructor here
            }
            
            D Offline
            D Offline
            Douglas Kirk
            wrote on last edited by
            #5

            Awesome Thank you for your time that works. Douglas

            L 1 Reply Last reply
            0
            • D Douglas Kirk

              Awesome Thank you for your time that works. Douglas

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Great! Glad to help.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups