Question

This is my first head on attempt with Visual Studio C++ 2008 (yeap, company policy :( ) I encountered the following issue trying to create an array of Panels on a Form.

#pragma once


namespace Pruebaformulariogrilla {

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

    /// <summary>
    /// Resumen de Form1
    ///
    /// ADVERTENCIA: si cambia el nombre de esta clase, deberá cambiar la
    ///          propiedad 'Nombre de archivos de recursos' de la herramienta de compilación de recursos administrados
    ///          asociada con todos los archivos .resx de los que depende esta clase. De lo contrario,
    ///          los diseñadores no podrán interactuar correctamente con los
    ///          recursos adaptados asociados con este formulario.
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {

    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: agregar código de constructor aquí
            //
        }

    protected:
        /// <summary>
        /// Limpiar los recursos que se estén utilizando.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }

    private:
        /// <summary>
        /// Variable del diseñador requerida.
        /// </summary>
        System::ComponentModel::Container ^components;

    private: List<Panel^>^ ArrayPaneles;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Método necesario para admitir el Diseñador. No se puede modificar
        /// el contenido del método con el editor de código.
        /// </summary>
        void InitializeComponent(void)
        {
            Panel^ p = gcnew Panel();
            ArrayPaneles->Add(p);

            this->SuspendLayout();
            // 
            // panel1
            //
            for each (Panel^ p in ArrayPaneles)
            {
            int i=1;
            p->Location = System::Drawing::Point(16, 26+i);
            p->Name = L"panel1";
            p->BackColor = System::Drawing::Color::Cyan;
            p->Size = System::Drawing::Size(54, 61);
            p->TabIndex = 1;
            i=i+10;
            }
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(765, 569);
            for each (Panel^ p in ArrayPaneles)
            {
                this->Controls->Add(p);
            }
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             }
    };
}

The error I'm encountering is System.NullReferenceException adding the first Panel to ArrayPaneles.

The porpouse of this code is to make an array (eventually a matrix) of Panels in the form, variable according to a constant, for example.

Thanks in advance for the patience

Was it helpful?

Solution

ArrayPaneles is null, because you have never gcnew'ed it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top