021 Windows Forms Intro
021 Windows Forms Intro
1
Objectives
2
Windows Presentation Foundation
In Visual Studio
Use the File menu to create a new project
New Project dialog box comes up.
4
Creating a Windows Forms Application
5
New Project Dialog Box
Hello, World Windows Form
7
Controls
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
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
Location, Size
Appearance properties
19
Add a Textbox
Drag a TextBox control from the
Toolbox to the form.
The TextBox Control
Example:
lblMessage.Text = "Hello, " + tbName.Text;
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.
24
Adding Controls to a Form
Set the name of the existing “Hello,
world” label to lblMessage.
25
Hello, World Windows Form
tbName
btnOK
lblMessage
26
Adding code to handle a button click
27
The Button Click Event Handler
Adding code to handle a button click
Initial form:
30
Build and Run
31
Avoiding a Click
The form has a property called
AcceptButton.
Event handler for this button is invoked
when user presses the Enter key.
34
Using a Message Box
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.
40
Message Box Return Value
41
Requiring Confirmation
Using Message Box Return Value
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
51