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
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
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.
9
Getting Started
Create a new C# Windows Forms project
10
An Example Button
We can copy from Visual Studio's generated code.
11
An Example Button
Double click on the button to add an event handler.
12
Generated Code
Note statement to hook up the Event Handler. (line 43)
13
Form1
public partial class Form1 : Form
{
int number_of_rows = 5;
int number_of_cols = 6;
int button_size = 50;
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.
15
Form Load Event Handler
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);
}
}
}
17
Program Running
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;
}
19
Form with Poisoned Button
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");
}
21
Click Lower Right Corner
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"
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);
}
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 ");
}
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
26
Initial Form
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;
}
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);
}
}