This is a section of code:

this->SquareImage1->Visible = false;
	this->SquareImage2->Visible = false;
	this->SquareImage3->Visible = false;
	this->SquareImage4->Visible = false;
	this->SquareImage5->Visible = false;
	this->SquareImage6->Visible = false;
	this->SquareImage7->Visible = false;
	this->SquareImage8->Visible = false;
	this->SquareImage9->Visible = false;
	this->SquareImage10->Visible = false;
	this->SquareImage11->Visible = false;
	this->SquareImage12->Visible = false;
	this->SquareImage13->Visible = false;
	this->SquareImage14->Visible = false;
	this->SquareImage15->Visible = false;
	this->SquareImage16->Visible = false;

	this->SquareImage17->Visible = false;
	this->SquareImage18->Visible = false;
	this->SquareImage19->Visible = false;
	this->SquareImage20->Visible = false;
	this->SquareImage21->Visible = false;
	this->SquareImage22->Visible = false;
	this->SquareImage23->Visible = false;
	this->SquareImage24->Visible = false;
	this->SquareImage25->Visible = false;
	this->SquareImage26->Visible = false;
	this->SquareImage27->Visible = false;
	this->SquareImage28->Visible = false;
	this->SquareImage29->Visible = false;
	this->SquareImage30->Visible = false;
	this->SquareImage31->Visible = false;
	this->SquareImage32->Visible = false;

	this->SquareImage33->Visible = false;
	this->SquareImage34->Visible = false;
	this->SquareImage35->Visible = false;
	this->SquareImage36->Visible = false;
	this->SquareImage37->Visible = false;
	this->SquareImage38->Visible = false;
	this->SquareImage39->Visible = false;
	this->SquareImage40->Visible = false;
	this->SquareImage41->Visible = false;
	this->SquareImage42->Visible = false;
	this->SquareImage43->Visible = false;
	this->SquareImage44->Visible = false;
	this->SquareImage45->Visible = false;
	this->SquareImage46->Visible = false;
	this->SquareImage47->Visible = false;
	this->SquareImage48->Visible = false;

	this->SquareImage49->Visible = false;
	this->SquareImage50->Visible = false;
	this->SquareImage51->Visible = false;
	this->SquareImage52->Visible = false;
	this->SquareImage53->Visible = false;
	this->SquareImage54->Visible = false;
	this->SquareImage55->Visible = false;
	this->SquareImage56->Visible = false;
	this->SquareImage57->Visible = false;
	this->SquareImage58->Visible = false;
	this->SquareImage59->Visible = false;
	this->SquareImage60->Visible = false;
	this->SquareImage61->Visible = false;
	this->SquareImage62->Visible = false;
	this->SquareImage63->Visible = false;
	this->SquareImage64->Visible = false;

As you can see, it's a lot of lines.

How can I do this so that it just cycles through a loop and sets all of them to false?

E.G like:

for(int i=1;i<65;i++)
{
  this->some sort of array here->Visible = false;
}

Thanks for any help anyone gives!

can you make SquareImage1 to SquareImage64 an array? Then you could do that in a simple for loop. And it might simplify other parts of the program. Image SquareImage[64];

Edited 6 Years Ago by Ancient Dragon: n/a

Didn't work, it doesn't seem to like that.

Please could someone post an example of how to do this sort of thing?

If you do this then you may no longer be able to use the designer to visually edit the form tht contains all those picture boxes. So set up the form the normal way to make it the way you want it.

You will have to make a lot of changes.

1) declare the array like this: private: array<System::Windows::Forms::PictureBox^>^ pictureBox1; 2) declare a const int for the number of pictureBox objects you want const int MaxPictures = 10; 3) allocate the array

void InitializeComponent(void)
{
            pictureBox1 = gcnew array<System::Windows::Forms::PictureBox^>(MaxPictures);
            for(int i = 0; i < MaxPictures; i++)
                (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1[i]))->BeginInit();

4) make similar changes throughout the program

Edited 6 Years Ago by Ancient Dragon: n/a

Hey again,

I've been trying to use the above solution and for some reason I keep on getting the following exceptions when it's debugged:

An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll

Additional information: Object reference not set to an instance of an object.

What does this mean exactly and what is it being caused by?

This article has been dead for over six months. Start a new discussion instead.