Windows Forms

advertisement
Windows Forms
GUI Programming for Windows
1
Objectives
You will be able to:
 Create a Windows Forms project in
Visual Studio .NET
 Develop a small Windows Forms
application with the traditional look and
feel of the Windows GUI.
 Write code to handle events resulting
from user actions on Windows forms.
2
Windows Presentation Foundation

New framework for GUI development.




Covered in textbook chapters 22 – 24
Produced by the same people who brought
you Vista.
Won’t be covered in this course.
Fortunately the old framework is still
supported in Visual Studio 2008.

“Windows Forms”
Creating a Windows Forms Application

In Visual Studio 2008

Use the File menu to create a new project

New Project dialog box comes up.
4
Creating a Windows Forms Application






Under Project Types select Visual C# Windows Project
Under Templates select Windows Application
For name, say Hello
Click Browse and navigate to folder where you want to
put your project.
Click OK.
Visual Studio creates a Windows Forms project
with a single blank form called Form1.
5
New Project Dialog Box
Hello, World Windows Form
7
Controls

Controls make up the visible content of
the form.




What the user sees.
What the user can do.
Normally placed on the form at design
time.
Can also be added, deleted, and
modified by the program at run time.
8
Designing a Windows Form


Let’s add some controls to the initial
blank form to create a Hello, World
program.
Display the toolbox.


View -> Toolbox
Expand “Common Controls”

Drag and drop controls from the toolbox
to the form.
9
Designing a Windows Form
10
The Label Control

Simple way to put text anywhere on the form.

One way to do output to the user.

Text property specifies what it says.

Other properties you may want to set:

Font (Font family, size, bold, etc.)

Size, Location
Normally set interactively using the designer

ForeColor

TextAlign

Visible

By default, gets a name of the form “label1.”

No reason to change name unless you want to change the text
at run time from inside your program.

Set it to a meaningful name if the program will modify anything
about the label.
11
Adding a Label



Drag a Label control from the Toolbox
to the form.
Right click the label to open its
Properties window.
Set its Text property to “Hello, World!”
12
Adding a Label
13
Building and Running

Use the Build menu to compile.


Build Hello
Click the “Play” button to run.

Or use the Debug menu
14
Hello, World!
15
Changing the Appearance

Set properties of label1 to adjust its
appearance.



Font
Size
ForeColor
16
Hello Again
17
Look at the Code


Double click on the form to see the
program code.
View the Solution Explorer window to
see the file name.

Form1.cs


Code that we add will go here.
Form1.Designer.cs



Generated automatically by Visual Studio based
on what we do with the design surface.
Expand “Windows Form Designer generated
code” to see details.
18
Don’t modify this file directly.
The TextBox Control
A convenient way to get text input from a user.
Important Properties:
 Text – the content





Normally set blank initially.
User’s input will be available here.
ReadOnly
 Normally false
 Set to true to prevent user from changing content.
Location, Size
Appearance properties
19
Add a Textbox

Drag a TextBox control from the
Toolbox to the form.
The TextBox Control

Right click and select Properties

Set the name to tbName.


A textbox control is almost always used by
program code.
You need a meaningful name so that the code
makes sense.
Example:
lblMessage.Text = "Hello, " + tbName.Text;
What the user typed into the TextBox
21
The Button Control


Normal way for user to request an action.
Important properties:





Text
(Appears on the button.)
Size/Location
 Normally set interactively via designer
Enabled
Visible
Font, ForeColor, BackColor, etc.
22
Add a Button

Drag a Button control from the
Toolbox to the form.
The Button Control


Right click and select Properties.
Set the name to a meaningful value
rather than using the default.



Do this immediately!
Name this button btnOK.
Set the Text property to OK.
24
Adding Controls to a Form


Set the name of the existing “Hello,
world” label to lblMessage.
Drag another label to the form, just
above the TextBox, and set its text
property to:
Please enter your name
25
Hello, World Windows Form
tbName
btnOK
lblMessage
26
Adding code to handle a button click

In a Windows Forms program, most work
is done in response to user actions.




Event driven programming
In the designer view, just double click on
the button.
This will add a event handler stub for clicks
on that button and take you to that point
in the code window.
Fill in the code to handle the button click.
27
The Button Click Event Handler
Adding code to handle a button click
private void btnOK_Click(object sender, System.EventArgs e)
{
lblMessage.Text = "Hello, " + tbName.Text;
}
Add your own code to
respond to the button
click.
This is created automatically
by Visual Studio when you
double click the button in
designer mode.
Normally the function arguments are not used.
29
Build and Run
Initial form:
30
Build and Run
After typing name in text box and clicking on OK:
31
Avoiding a Click

The form has a property called
AcceptButton.


Event handler for this button is invoked
when user presses the Enter key.
Also a CancelButton property.

Event handler invokded when user presses
the ESC key.
Avoiding a Click

Set the form's AcceptButton property to
btnOK.

Build and run.

Try pressing Enter after typing name.
Using a MessageBox

MessageBox is a built-in component


Use to display a message to the user


Like a popup window.
Optionally get a response
Simple Example:
MessageBox.Show("Hello, World!");

An easy way to make our button click
function do something visible.
34
Using a Message Box
private void btnOK_Click (object sender, System.EventArgs e)
{
MessageBox.Show("Hello, " + tbName.Text);
}
35
Using a Message Box
36
Message Box Options
37
Message Box Options
38
Message Box Options
Message Box Return Value

When there are multiple buttons, you
can check which on the user clicked.

One way to provide an “Are you sure?”
dialog.
40
Message Box Return Value
41
Requiring Confirmation
Using Message Box Return Value
private void btnOK_Click(object sender, System.EventArgs e)
{
DialogResult result =
MessageBox.Show("Are you sure you want to reformat the disk?",
"Caution", MessageBoxButtons.OKCancel);
}
if (result == DialogResult.Cancel)
{
MessageBox.Show ("Operation canceled");
}
else
{
MessageBox.Show("Reformatting disk. Please wait");
// Call reformat function here.
this.Close();
}
43
Using Message Box Return Value
44
Using Message Box Return Value
45
Cancel Clicked
46
Always Provide a Way Out

Typically a Cancel button.
Cancel Event
private void btnCancel_Click(object sender, EventArgs e)
{
MessageBox.Show("Operation canceled");
this.Close();
}
Set Form's CancelButton
Cancel
Try the ESC key.
Assignment

Try the examples from this class for
yourself.
51
Download