280 likes | 375 Views
Working with Controls at Run Time. Objectives. You will be able to Add controls to a Windows form at run time. Modify controls at run time. Setting Up Controls at Run Time. Why do this? May not know exactly what we need at design time.
E N D
Objectives You will be able to • Add controls to a Windows form at run time. • Modify controls at run time.
Setting Up Controls at Run Time • Why do this? • May not know exactly what we need at design time. • With a large number of controls, it might be easier to write code to set up the controls than to create them manually at design time.
The Controls Collection • The Windows Form class has a Controls collection. • Everything that we see on the form. • Implements interface IList • Accessible at run time as the form's Controls property. • We can instantiate control objects and add them to the collection. • Also modify existing controls.
Example: The Game of Chomp • The game of Chomp was described in a Math Trek column in Science News: • http://sciencenews.org/view/generic/id/3683/ title/Math_Trek__Chomping_to_Win
Implementing Chomp • Let's create a Windows Form for a game of chomp with five rows and six columns of squares. • Each square will be a button. • When a button is clicked, it and all buttons above it and to its right will disappear. • Create all buttons at run time. • Modify them at run time as users play.
Getting Started • Create a new C# Windows Forms project
An Example Button We can copy from Visual Studio's generated code.
An Example Button • Double click on the button to add an event handler.
Generated Code Note statement to hook up the Event Handler. (line 43)
Form1 public partial class Form1 : Form { int number_of_rows = 5; int number_of_cols = 6; int button_size = 50;
Add_Button() Add to class Form private void Add_Button(int row, int col) { Button btn = new Button(); btn.BackColor = System.Drawing.Color.Blue; btn.Location = new System.Drawing.Point(col*button_size, row*button_size); btn.Name = "btn" + row + col; btn.Size = new System.Drawing.Size(button_size, button_size); btn.UseVisualStyleBackColor = false; btn.Click += new System.EventHandler(btn00_Click); Controls.Add(btn); } Delete the example button.
Form1_Load() private void Form1_Load(object sender, EventArgs e) { for (int row = 0; row < number_of_rows; ++row) { for (int col = 0; col < number_of_cols; ++col) { Add_Button(row, col); } } }
Set up the "poisoned" button private void Add_Button(int row, int col) { Button btn = new Button(); if ((row == number_of_rows - 1) && (col == 0)) { btn.BackColor = System.Drawing.Color.Red; btn.Enabled = false; } else { btn.BackColor = System.Drawing.Color.Blue; btn.Enabled = true; }
Initial Click Handler private void btn00_Click(object sender, EventArgs e) { Button btn = (Button)sender; int row = btn.Name[3] - '0'; int col = btn.Name[4] - '0'; MessageBox.Show("Row " + row + " Col " + col + " clicked"); }
Update_Buttons private void Update_Buttons(int row, int col) { for (int i = 0; i < Controls.Count; ++i) { Control c = Controls[i]; Button btn = c as Button; if (btn == null) continue; int btn_row = btn.Name[3] - '0'; int btn_col = btn.Name[4] - '0'; if ((btn_row <= row) && (btn_col >= col)) { btn.BackColor = Color.White; btn.Enabled = false; } } } Note "as"
Update_Buttons private void btn00_Click(object sender, EventArgs e) { Button btn = (Button)sender; int row = btn.Name[3] - '0'; int col = btn.Name[4] - '0'; //MessageBox.Show("Row " + row + " Col " + col + " clicked"); Update_Buttons(row, col); }
Keep track of the players public partial class Form1 : Form { int current_player = 1; ... private void Form1_Load(object sender, EventArgs e) { for (int row = 0; row < number_of_rows; row++) { for (int col = 0; col < number_of_cols; col++) { Add_Button(row, col); } } MessageBox.Show("Player 1 "); }
Keep track of the players private void btn00_Click(object sender, EventArgs e) { Button btn = (Button)sender; int row = btn.Name[3] - '0'; int col = btn.Name[4] - '0'; //MessageBox.Show("Row " + row + " Col " + col + " clicked"); Update_Buttons(row, col); current_player = current_player == 1? 2 : 1; MessageBox.Show("Player " + current_player); } Build and run
Check for Game Over private bool Game_Over() { for (int i = 0; i < Controls.Count; ++i) { Control c = Controls[i]; Button btn = c as Button; if (btn == null) continue; if (btn.Enabled) { return false; } } return true; }
Check for Game Over private void btn00_Click(object sender, EventArgs e) { Button btn = (Button)sender; int row = btn.Name[3] - '0'; int col = btn.Name[4] - '0'; //MessageBox.Show("Row " + row + " Col " + col + " clicked"); Update_Buttons(row, col); if (Game_Over() ) { MessageBox.Show("Game Over! \n" + "Player " + current_player + " wins."); } else { current_player = current_player == 1? 2 : 1; MessageBox.Show("Player " + current_player); } }