1 / 28

Working with Controls at Run Time

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.

hisano
Download Presentation

Working with Controls at Run Time

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Controls at Run Time

  2. Objectives You will be able to • Add controls to a Windows form at run time. • Modify controls at run time.

  3. 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.

  4. 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.

  5. 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

  6. The Game of Chomp

  7. 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.

  8. Getting Started • Create a new C# Windows Forms project

  9. An Example Button We can copy from Visual Studio's generated code.

  10. An Example Button • Double click on the button to add an event handler.

  11. Generated Code Note statement to hook up the Event Handler. (line 43)

  12. Form1 public partial class Form1 : Form { int number_of_rows = 5; int number_of_cols = 6; int button_size = 50;

  13. 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.

  14. Form Load Event Handler

  15. 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); } } }

  16. Program Running

  17. 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; }

  18. Form with Poisoned Button

  19. 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"); }

  20. Click Lower Right Corner

  21. 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"

  22. 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); }

  23. 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 "); }

  24. 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

  25. Initial Form

  26. 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; }

  27. 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); } }

More Related