Hi, I want to rewrite one of my old projects in VS2008 using the C++ CLR.
It contains alot of Controls like Buttons, Combo Boxes ect... and i want
to create a Control Array for it. I already searched a few hours for a solution,
but i didn't found any 100% working solution.
I have added the following to my constructor code:
Code:
array<System::Windows::Forms::Button^>^test = gcnew array<System::Windows::Forms::Button^>(2);
test[0] = (gcnew System::Windows::Forms::Button());
test[0]->Location = System::Drawing::Point(100, 100);
test[0]->Size = System::Drawing::Size(40, 40);
test[0]->Text = L"M";
test[0]->Visible = true;
this->Controls->Add(test[0]);
So far, it create the button and i can see it. The problem is that Iam not
able to access the Object out of this code, so its some kind of local (if I
try to read informations, the debugger say undeclared variable.....)
Would be nice if someone could help me.
Thats the whole code:
Code:
#pragma once
namespace arrayvars {
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>
/// Zusammenfassung für Form1
///
/// Warnung: Wenn Sie den Namen dieser Klasse ändern, müssen Sie auch
/// die Ressourcendateiname-Eigenschaft für das Tool zur Kompilierung verwalteter Ressourcen ändern,
/// das allen RESX-Dateien zugewiesen ist, von denen diese Klasse abhängt.
/// Anderenfalls können die Designer nicht korrekt mit den lokalisierten Ressourcen
/// arbeiten, die diesem Formular zugewiesen sind.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
array<System::Windows::Forms::Button^>^test = gcnew array<System::Windows::Forms::Button^>(2);
test[0] = (gcnew System::Windows::Forms::Button());
test[0]->Location = System::Drawing::Point(100, 100);
test[0]->Size = System::Drawing::Size(40, 40);
test[0]->Text = L"M";
test[0]->Visible = true;
this->Controls->Add(test[0]);
InitializeComponent();
//
//TODO: Konstruktorcode hier hinzufügen.
//
}
protected:
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(77, 97);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(79, 32);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show(Convert::ToString(test[0]->Text)); // <---- Don't work
}
};
}