This is from a training project. So the objective is to learn how to do a specific task, though there may be better ways to do the same thing.
Okay, the background of the problem at hand:
It's from my training Mastermind clone. The user has chosen his colours and on mouse click those colours will appear in a separate form (as oval shapes from Microsoft.VisualBasic.PowerPacks) with two additional shapes, yet in specific distinct colours and a number telling the user how many well-placed slots he has found (and of course how many colours he has right as well, you know the game).
Each of these shapes will be created when the need arises.
Copying the user-chosen colours works fine:
for (int i = 0; i < GlobalVariables.configsetup[0]; i++)
{
GlobalVariables.countresultslots += 1;
ResultForm.ClientSize = new System.Drawing.Size(((GlobalVariables.configsetup[0] + 2) * 60) + 4, 362);
ResultForm.Panel.Size = new System.Drawing.Size((GlobalVariables.configsetup[0] + 2) * 60, 362 - 24);
ResultForm.SlotField[GlobalVariables.countresultslots] = new System.Windows.Forms.Panel();
ResultForm.SlotContainer[GlobalVariables.countresultslots] = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
ResultForm.SlotForm[GlobalVariables.countresultslots] = new Microsoft.VisualBasic.PowerPacks.OvalShape();
ResultForm.Panel.Controls.Add(ResultForm.SlotField[GlobalVariables.countresultslots]);
ResultForm.SlotField[GlobalVariables.countresultslots].Location = new Point(i * 60, (GlobalVariables.guesscounter - 1) * 60);
ResultForm.SlotField[GlobalVariables.countresultslots].Controls.Add(ResultForm.SlotContainer[GlobalVariables.countresultslots]);
ResultForm.SlotField[GlobalVariables.countresultslots].Margin = new System.Windows.Forms.Padding(5);
ResultForm.SlotField[GlobalVariables.countresultslots].Name = "SlotField" + (GlobalVariables.countresultslots + 1).ToString();
ResultForm.SlotField[GlobalVariables.countresultslots].Size = new System.Drawing.Size(50, 50);
ResultForm.SlotField[GlobalVariables.countresultslots].TabIndex = 0;
ResultForm.SlotField[GlobalVariables.countresultslots].TabStop = false;
ResultForm.SlotContainer[GlobalVariables.countresultslots].Location = new System.Drawing.Point(0, 0);
ResultForm.SlotContainer[GlobalVariables.countresultslots].Margin = new System.Windows.Forms.Padding(0);
ResultForm.SlotContainer[GlobalVariables.countresultslots].Name = "SlotContainer" + (GlobalVariables.countresultslots + 1).ToString();
ResultForm.SlotContainer[GlobalVariables.countresultslots].Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] { ResultForm.SlotForm[GlobalVariables.countresultslots] });
ResultForm.SlotContainer[GlobalVariables.countresultslots].Size = new System.Drawing.Size(50, 50);
ResultForm.SlotContainer[GlobalVariables.countresultslots].TabIndex = 0;
ResultForm.SlotContainer[GlobalVariables.countresultslots].TabStop = false;
ResultForm.SlotForm[GlobalVariables.countresultslots].BorderColor = System.Drawing.Color.Black;
ResultForm.SlotForm[GlobalVariables.countresultslots].BorderWidth = 2;
ResultForm.SlotForm[GlobalVariables.countresultslots].Enabled = false;
ResultForm.SlotForm[GlobalVariables.countresultslots].FillColor = System.Drawing.Color.FromArgb(BasicFunctions.SetColour(GlobalVariables.tempcolorinput[i])[0], BasicFunctions.SetColour(GlobalVariables.tempcolorinput[i])[1], BasicFunctions.SetColour(GlobalVariables.tempcolorinput[i])[2]);
ResultForm.SlotForm[GlobalVariables.countresultslots].FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid;
ResultForm.SlotForm[GlobalVariables.countresultslots].Location = new System.Drawing.Point(5, 5);
ResultForm.SlotForm[GlobalVariables.countresultslots].Name = "SlotForm" + (GlobalVariables.countresultslots + 1).ToString();
ResultForm.SlotForm[GlobalVariables.countresultslots].Size = new System.Drawing.Size(38, 38);
}
...where GlobalVariables.configsetup[0] stocks the number of colours (let's say 4), GlobalVariables.countresultslots will count the number of shapes created and thus help identify each shape, GlobalVariables.tempcolorinput has temporarily stocked the colours chosen by the user, and BasicFunctions.SetColour is a function that will provide the RGB codes for each color.
Up until now the code does exactly what I wanted it to do. Now I created a second loop right after the other to create two more shapes with a number in them, set in different colours, but right next to the others.
for (int i = 0; i < 2; i++)
{
GlobalVariables.countresultslots += 1;
ResultForm.SlotField[GlobalVariables.countresultslots] = new System.Windows.Forms.Panel();
ResultForm.SlotContainer[GlobalVariables.countresultslots] = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
ResultForm.SlotForm[GlobalVariables.countresultslots] = new Microsoft.VisualBasic.PowerPacks.OvalShape();
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i] = new System.Windows.Forms.Label();
ResultForm.Panel.Controls.Add(ResultForm.SlotField[GlobalVariables.countresultslots]);
ResultForm.Panel.Controls.Add(ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i]);
ResultForm.SlotField[GlobalVariables.countresultslots].Location = new Point((GlobalVariables.configsetup[0] + i)* 60, (GlobalVariables.guesscounter - 1) * 60 + 20);
ResultForm.SlotField[GlobalVariables.countresultslots].Controls.Add(ResultForm.SlotContainer[i]);
ResultForm.SlotField[GlobalVariables.countresultslots].Margin = new System.Windows.Forms.Padding(5);
ResultForm.SlotField[GlobalVariables.countresultslots].Name = "SlotField" + (GlobalVariables.countresultslots + 1).ToString();
ResultForm.SlotField[GlobalVariables.countresultslots].Size = new System.Drawing.Size(50, 50);
ResultForm.SlotField[GlobalVariables.countresultslots].TabIndex = 0;
ResultForm.SlotField[GlobalVariables.countresultslots].TabStop = false;
ResultForm.SlotContainer[GlobalVariables.countresultslots].Location = new System.Drawing.Point(0, 0);
ResultForm.SlotContainer[GlobalVariables.countresultslots].Margin = new System.Windows.Forms.Padding(0);
ResultForm.SlotContainer[GlobalVariables.countresultslots].Name = "SlotContainer" + (GlobalVariables.countresultslots + 1).ToString();
ResultForm.SlotContainer[GlobalVariables.countresultslots].Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] { ResultForm.SlotForm[GlobalVariables.countresultslots] });
ResultForm.SlotContainer[GlobalVariables.countresultslots].Size = new System.Drawing.Size(50, 50);
ResultForm.SlotContainer[GlobalVariables.countresultslots].TabIndex = 0;
ResultForm.SlotContainer[GlobalVariables.countresultslots].TabStop = false;
ResultForm.SlotForm[GlobalVariables.countresultslots].BorderWidth = 2;
ResultForm.SlotForm[GlobalVariables.countresultslots].Enabled = false;
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i].AutoSize = true;
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i].Location = new Point((GlobalVariables.configsetup[0] + i)* 60, (GlobalVariables.guesscounter - 1) * 60);
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i].Size = new System.Drawing.Size(42, 20);
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i].TabIndex = 0;
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i].Enabled = false;
if (i == 0)
{
ResultForm.SlotForm[GlobalVariables.countresultslots].FillColor = System.Drawing.Color.Green;
ResultForm.SlotForm[GlobalVariables.countresultslots].BorderColor = System.Drawing.Color.DarkGreen;
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i].Name = "CH" + GlobalVariables.guesscounter.ToString() + "." + (i + 1).ToString();
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i].Text = GlobalVariables.position[GlobalVariables.guesscounter, GlobalVariables.configsetup[0]].ToString();
}
else
{
ResultForm.SlotForm[GlobalVariables.countresultslots].FillColor = System.Drawing.Color.Green;
ResultForm.SlotForm[GlobalVariables.countresultslots].BorderColor = System.Drawing.Color.DarkGreen;
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i].Name = "CC" + GlobalVariables.guesscounter.ToString() + "." + (i + 1).ToString();
ResultForm.ResultLabel[(GlobalVariables.countresultslots / (GlobalVariables.configsetup[0] + 2)) + i].Text = GlobalVariables.position[GlobalVariables.guesscounter, GlobalVariables.configsetup[0] + 1].ToString();
}
ResultForm.SlotForm[GlobalVariables.countresultslots].FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid;
ResultForm.SlotForm[GlobalVariables.countresultslots].Location = new System.Drawing.Point(5, 5);
ResultForm.SlotForm[GlobalVariables.countresultslots].Name = "SlotForm" + (GlobalVariables.countresultslots + 1).ToString();
ResultForm.SlotForm[GlobalVariables.countresultslots].Size = new System.Drawing.Size(38, 38);
}
Theoretically it should work, but a funny thing happens. Once the program is finished, it should display 4 shapes with the colours the user has chosen (4 because we have agreed on this much, remember) and two to the right in a different colour and a number in them.
But what I actually get is that the first two shapes are missing, shapes 3 and 4 are displayed alright, and the the last two shapes (that should have been in standard Green, in my code so far) will display the colours of shapes 1 and 2, respectively. And I can't think of any reason why this should be the case.