Tableau de boutons winform

Fermé
seiferx - 16 janv. 2008 à 18:14
 moi - 12 mars 2008 à 18:11
Bonjour

J ai un bug sur mon code et j'ai vraiment besoin que qqun m'explique.
Le but est de rajouter une boucle qui cree un Tableau de boutton
sur une winforms standard, qui marche bien

Cela vient suremnt des lignes 45 et 77.
J ai deja fait un post avec des parties de code, mais je n ai eu aucune reponse satisfaisante.
Ce coup ci je vous met tout mon code, et peux etre qu'avec de la chance qqun pourra retrouver ce que je fait de faux.
.


Si qqun a 10 minutes a perdre sur mon code, peux etre moins, cela m aidera beaucoup

Quand je lance un Debug je recois ca a la ligne 77 :

System.NullReferenceException was unhandled
Message: Object reference not set to an instance of an object.


Merci d'avance
========================

#pragma once

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


namespace Gomoku {

/// <summary>
/// Summary for test
/// </summary>
public ref class test : public System::Windows::Forms::Form
{
public:
test(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~test()
{
if (components)
{
delete components;
}
}

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;

private: array<System::Windows::Forms::Button^>^button_tab;


#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// test
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Name = L"test";
this->Text = L"test";
this->ResumeLayout(false);
add_button();
}

void add_button(void)
{

//
// tableau de bouton
//
int x = 0;
while (x < 5)
{
this->button_tab[x] = gcnew System::Windows::Forms::Button();
this->button_tab[x]->Location = System::Drawing::Point(10 +(10 * x) , 10);
this->button_tab[x]->Name = L"button1";
this->button_tab[x]->Size = System::Drawing::Size(30, 30);
this->button_tab[x]->TabIndex = x;
this->button_tab[x]->Text = L"button1";
this->button_tab[x]->UseVisualStyleBackColor = true;
this->Controls->Add(this->button_tab[x]);
x++;
}
}
#pragma endregion
};
}

3 réponses

UP
0
UP ..
0
Le tableau 'button_tab' n'est jamais instancié. Sa référence reste donc toujours à null.

Dans le constructeur, il faut rajouter avant le initiializecomponents button_tab = gcnew array<System::Windows::Forms::Button^>()
0