using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form1 : Form { #region Fields List TopBoxes = new List(); List BottomBoxes = new List(); List WildCard = new List(); int horizontalspacing = 5; //used to stagger pictureboxes int verticalspacing = 3; //used stagger pictureboxes #endregion Fields #region Constructors public Form1() { InitializeComponent(); SetupGameboard(); } #endregion Constructors #region Methods //create the picture box list void GenerateBoxes(List WhichList, int MaxBoxes) { for (int Index = 0; Index < MaxBoxes; Index++) { PictureBox NewBox = new PictureBox(); // NewBox.Size = new Size(25, 25); //these don't work during box creation // NewBox.BorderStyle = BorderStyle.FixedSingle; //these don't work during box creation // NewBox.BackColor = BoxColor; //these don't work during box creation WhichList.Add(new PictureBox()); } } //place the picture box list void PlaceBoxes(List WhichList, Control Where, Color BoxColor, int xlocation, int ylocation) { for (int Index = 0; Index < WhichList.Count; Index++) { Point NewLocation = new Point( (Index * horizontalspacing) + xlocation, (Index * verticalspacing) + ylocation); Where.Controls.Add(WhichList[Index]); WhichList[Index].Location = NewLocation;// Set it at the time of placement, not creation WhichList[Index].Size = new Size(25, 25); //moved the code here from box creation WhichList[Index].BorderStyle = BorderStyle.FixedSingle; //moved the code here from box creation WhichList[Index].BackColor = BoxColor; //moved the code here from box creation WhichList[Index].BringToFront(); } } private void button1_Click(object sender, EventArgs e) { FlipCoin(); } private void FlipCoin() { int randomDestination = RandomNumber(1, 100); if (randomDestination >= 50) { splitContainer2.Panel1.Controls.Add(pictureBox1); //add the control pictureBox1.Location = new System.Drawing.Point(70, 40); //move the control } else { splitContainer2.Panel2.Controls.Add(pictureBox1); //add the control pictureBox1.Location = new System.Drawing.Point(70, 40); //move the control } } // generate a random number private int RandomNumber(int min, int max) { Random random = new Random(DateTime.Now.Second); return random.Next(min, max); } void SetupGameboard() { TopBoxes.Clear(); BottomBoxes.Clear(); // Disconnect events so we don't react to new Picture being added during setup splitContainer2.Panel1.ControlAdded -= new ControlEventHandler(Panel1_ControlAdded); splitContainer2.Panel2.ControlAdded -= new ControlEventHandler(Panel2_ControlAdded); GenerateBoxes(TopBoxes, 5); GenerateBoxes(BottomBoxes, 5); GenerateBoxes(WildCard, 3); PlaceBoxes(TopBoxes, splitContainer2.Panel1, Color.Green, 10, 20); PlaceBoxes(BottomBoxes, splitContainer2.Panel2, Color.Red, 10,20); PlaceBoxes(WildCard,splitContainer2.Panel2, Color.Yellow, 10, 70); pictureBox1.Location = new Point(10, 10); // Connect to events so we react to the Tardis moving into this space during play splitContainer2.Panel1.ControlAdded += new ControlEventHandler(Panel1_ControlAdded); splitContainer2.Panel2.ControlAdded += new ControlEventHandler(Panel2_ControlAdded); label1.Text = "Top remaining: " + TopBoxes.Count; label2.Text = "Bottom remaining: " + BottomBoxes.Count; } // React when the Tardis enters this panel void Panel1_ControlAdded(object sender, ControlEventArgs e) { if (TopBoxes.Count > 0) { TopBoxes.RemoveAt(0); //remove from list splitContainer2.Panel1.Controls.RemoveAt(0); //remove from panel label1.Text = "Top remaining: " + TopBoxes.Count; } } // React when the Tardis enters this panel void Panel2_ControlAdded(object sender, ControlEventArgs e) { if (BottomBoxes.Count > 0) { BottomBoxes.RemoveAt(0); //remove from list splitContainer2.Panel2.Controls.RemoveAt(0); //remove from panel label2.Text = "Bottom remaining: " + BottomBoxes.Count; } } #endregion Methods } }