Creating a global array of labels at runtime
-
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 -
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->timer11. 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.
-
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.
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
-
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
-
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 }
Awesome Thank you for your time that works. Douglas
-
Awesome Thank you for your time that works. Douglas