3

I have a problem. First I'll show you a small code excerpt from my Minesweeper game I'm making.

lbl_grid[mineX, mineY].Text = "*";

Now, what this does is it sets a mine in my grid to look like a *.

What I want instead is for lbl_grid[mineX, mineY].Text to be assigned a value of an Icon. Is this possible?

I believe I may have to use something other than text, since icons aren't text.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135

1 Answers1

1

You are right, you will have to use something other than text.

An option is to make a 2D array of PictureBox.

int rows = 3;
int cols = 4;

List<List<PictureBox>> pictures = new List<List<PictureBox>>();

for(int r = 0; r < rows; r++)
{
    List<PictureBox> pList = new List<PictureBox>();
    for(int c = 0; c < cols; c++)
    {
        //do any positioning you need to do here
        pList.Add(new PictureBox());
    }
    pictures.Add(pList);
}

Then you can access and set the Image Source.

pictures[rVal][cVal].Image = Image.FromFile("<file path>");
gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • As you said, an option is to make a 2D array of PictureBox, but that's not a 2D array of PictureBox. `PictureBox[,] pictures = new PictureBox[rows,cols]();` is a 2D array. – David Yaw Feb 12 '13 at 21:41
  • And by 2d array I mean List of List. I'm a list guy. – gunr2171 Feb 12 '13 at 21:46