tarqs tarqs - 1 year ago 61
C# Question

Can't get extended label class to show in windows forms application

I currently have a class that extends the

System.Windows.Forms.Label
class. I'm looking to program minesweeper for a knowledge assignment. It basically adds some functionality to the regular label and then gets instantiated in the form1.cs part.

public partial class Cell : System.Windows.Forms.Label
{
private bool hasBomb;
private bool isRevealed;
private int neighbourBombCount;
static int BombAmount;

// Properties
public bool HasBomb
{
get { return hasBomb; }
set { hasBomb = value; }
}

public bool IsRevealed
{
get { return isRevealed; }
set { isRevealed = value; }
}

public int NeighbourBombCount
{
get { return neighbourBombCount; }
set { neighbourBombCount = value; }
}


// constructors
public Cell()
{
hasBomb = false;
isRevealed = false;
neighbourBombCount = 0;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Size = new System.Drawing.Size(50, 50);

}


and then theres the form1.cs:

public partial class Form1 : Form
{
// Public variable declarations
int chance;
Random rand1 = new Random();
static int bombAmount;

public Form1()
{
InitializeComponent();
this.AutoSize = false;
this.Width = 420;
this.Height = 420;
createGrid();
}

public void createGrid()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{

grid[i][j] = new Cell();
grid[i][j].Name = "grid" + i.ToString() + j.ToString();
grid[i][j].Location = new System.Drawing.Point(i * 49, j * 49);
grid[i][j].Size = new System.Drawing.Size(50, 50);
grid[i][j].TabIndex = 0;


chance = rand1.Next(0, 6);
if (chance % 6 == 0 && bombAmount < 10)
{
grid[i][j].HasBomb = true;
bombAmount++;
}
}
}

for (int i = 0; i < 8; i++)
{
for (int j = 0; j <8; j++)
{
this.Controls.Add(grid[i][j]);
}
}
}
private Cell[][] grid = new Cell[8][];


which has some more logic beneath which is unimportant for now. The problem is that nothing is showing up, what am I missing?

Answer Source

You aren't initializing the second part of your jagged array.

Try adding this:

public void createGrid() {
  for (int i = 0; i < 8; i++) {
    grid[i] = new Cell[8];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download