creating an array of Labels (Windows Forms)

G

Guest

I need to create many labels to fit on a windows forms and instead of using
the designer to create a large number of text labels I just wanted to make an
array of them and then loop through them to set their properties.

when trying this code
Label labelArray[,] = new Label[8,8];

I get an error of invalid type for __gc array element
what would be the correct way of creating this, if it is possible.

*note I need so many text labels because I am doing a graphical solution to
the Knights tour. Knights tour is a program that tries to find a way that a
knight can move to each position on the board only once. It will show the
"move number" in each square on a chess board that the knight has visited.
 
B

Bruno van Dooren

I need to create many labels to fit on a windows forms and instead of using
the designer to create a large number of text labels I just wanted to make
an
array of them and then loop through them to set their properties.

when trying this code
Label labelArray[,] = new Label[8,8];

use this:
Label *labelArray[,] = new Label*[8,8];

You need an array of label references. You cannot create an array of not
label objects.
You then have to initialize each array element in a for loop:

for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
labelArray[i,j] = new Label();
//initialize label i,j
}
}

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top