Ok, I thought I was close, but I just can't work around this bug. This should be all the relevant code that I am working on:
Code:
public string [] GameName =
new string [] { "Game 0","Game 1","Game 2","Game 3","Game 4","Game 5","Game 6","Game 7","Game 8"};
public int[,] resarea =
new int[,] { {8, 32}, {272, 32}, {536, 32}, {8, 112}, {272, 112}, {536, 112}, {8, 192}, {272, 192}, {536, 192}};
public int GuiItem = 0;
public System.Windows.Forms.GroupBox [] gBox = new System.Windows.Forms.GroupBox[9];
public void SpawnGuiItems(int gamenum)
{
try
{
// Add the groupbox to the form
this.Controls.Add(gBox[GuiItem]);
// Set intial properties for the groupbox
gBox[GuiItem].Size = new System.Drawing.Size(256, 72);
gBox[GuiItem].Location = new System.Drawing.Point(resarea[GuiItem,0],resarea[GuiItem,1]);
gBox[GuiItem].Text = GameName[GuiItem];
gBox[GuiItem].Tag = GuiItem;
gBox[GuiItem].Click += new System.EventHandler(ClickHandler);
GuiItem++;
}
catch (Exception ex)
{
// Let the user know what went wrong.
MessageBox.Show(ex.ToString(), "Exception generating the GUI", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void ClickHandler(Object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("You have clicked groupbox " +
((System.Windows.Forms.GroupBox) sender).Tag.ToString());
}
The code is compiling fine, but when it tires to run, it has an exception of "System.NullReferenceException: Object reference not set to an instance of an object." The line it errors on is gBox[GuiItem].Size. Any ideas on this? I have a feeling I am close, but just can't work my way around this bug. I know this is likely a basic OO issue, but I'm still trying to get all the concepts down.