28
1 Working with Controls at Run Time

Working with Controls at Run Time

  • Upload
    hisano

  • View
    40

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Working with Controls at Run Time

1

Working with Controls at Run Time

Page 2: 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.

Page 3: Working with 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.

Page 4: Working with Controls at Run 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.

Page 5: Working with Controls at Run Time

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

Page 6: Working with Controls at Run Time

6

The Game of Chomp

Page 7: Working with Controls at Run Time
Page 8: Working with Controls at Run Time

8

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.

Page 9: Working with Controls at Run Time

9

Getting Started

Create a new C# Windows Forms project

Page 10: Working with Controls at Run Time

10

An Example Button

We can copy from Visual Studio's generated code.

Page 11: Working with Controls at Run Time

11

An Example Button

Double click on the button to add an event handler.

Page 12: Working with Controls at Run Time

12

Generated Code

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

Page 13: Working with Controls at Run Time

13

Form1

public partial class Form1 : Form

{

int number_of_rows = 5;

int number_of_cols = 6;

int button_size = 50;

Page 14: Working with Controls at Run Time

14

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.

Page 15: Working with Controls at Run Time

15

Form Load Event Handler

Page 16: Working with Controls at Run Time

16

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);

}

}

}

Page 17: Working with Controls at Run Time

17

Program Running

Page 18: Working with Controls at Run Time

18

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;

}

Page 19: Working with Controls at Run Time

19

Form with Poisoned Button

Page 20: Working with Controls at Run Time

20

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");

}

Page 21: Working with Controls at Run Time

21

Click Lower Right Corner

Page 22: Working with Controls at Run Time

22

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"

Page 23: Working with Controls at Run Time

23

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);

}

Page 24: Working with Controls at Run Time

24

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 ");

}

Page 25: Working with Controls at Run Time

25

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

Page 26: Working with Controls at Run Time

26

Initial Form

Page 27: Working with Controls at Run Time

27

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;

}

Page 28: Working with Controls at Run Time

28

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);

}

}