Introduction to Windows
Programming
Chapter Objectives
Differentiate between the functions of Windows
applications and console applications
Learn about graphical user interfaces
Become aware of some elements of good design
Use C# and Visual Studio to create Windows-based
applications
Chapter Objectives (continued)
Create Windows forms and be able to change form
properties
Add control objects such as buttons, labels, and text
boxes to a form
Work through a programming example that illustrates
the chapter’s concepts
Windows Application Basics
 Windows Forms is the event base smart-client component of the
.NET Framework.
 Application run locally on users' computers.Once launched
 Set of managed libraries that enable common application tasks such
as reading and writing to the file system.
 A form is a visual surface on which you display information to the
user. You commonly build Windows Forms applications by placing
controls on forms and developing responses to user actions, such as
mouse clicks or key presses. A control is a discrete user interface
(UI) element that displays data or accepts data input.
Graphical User Interfaces
Interface: front end of a program
 Visual image you see when you run a program
Graphical user interface (GUI) includes:
 Menus
 Text in many different colors and sizes
 Other controls (pictures, buttons, etc.)
Windows Applications
Reference and import System.Windows.Forms
namespace
Class heading definition
 Includes not only the class name, but a colon
followed by another class name
 Derived class (first class)
 Base class (second class)
 public class Form1 : Form
Derived classes inherit from base class
Windows Applications (continued)
Text
 A property for setting/getting title bar caption
Name
 Unique name for all controls
Windows forms/controls offer many properties
including Text, Color, Font, and Location,Size
Execution begins in Main( ) method
 Main( ) is located in Program.cs file for the application
 Call to Run( ) method places application in process loop
using System.Windows.Forms; // Line 1
namespace Windows0
{
public class Form1 : Form // Line 2
{
public Form1( ) // Line 3
{
InitializeComponent();
Text = "Simple Windows Application"; // Line 4
}
}
}
New
namespace
referenced
Constructor
Base class
Sets
title bar
caption
Starts
process
loop
Windows Application (continued)
Figure 8-1 Windows-based form
Output
generated
from sample
from
application
Elements of Good Design
Appearance matters
 Human-computer interaction (HCI) research
Design considerations
 Consistency
 Alignment
 Avoid Clutter
 Color
 Target Audience
Use Visual Studio to Create Windows-
based Applications
Windows
Application
template
Browse
to
location
to store
your
work
Select
File
New
Project
Name
Figure 8-2 Visual Studio New Windows application
Windows-based Applications
Properties
Window
Design View
Toolbox
Switch
between
Design and
Code view
using View
menu
Figure 8-3 Initial design screen
Windows-based Applications (continued)
Figure 8-4 Dockable windows
Properties
Auto-hide
Solution
Explorer
pushpin
Windows Forms
Extensive collection of Control classes
Top-level window for an application is called a Form
Each control has large collection of properties
and methods , Events
 Select property from an alphabetized list (Properties
window)
 Change property by clicking in the box and selecting
or typing the new entry
Windows Form Properties
Properties
Property value
Figure 8-5 Properties window
Categorized
Alphabetical
Events
Windows Form Properties (continued)
Windows Form Events
Add code to respond to events, like button clicks
From the Properties window, select the lightening bolt
(Events)
 Double-click on the event name to generate code
 Registers the event as being of interest
 Adds a heading for event-handler method
Windows Form Properties (continued)
Events
button
selected
Figure 8-6 Form1 events
Windows Form – Closing Event
Code automatically added to register event
this.Closing += new System.ComponentModel.CancelEventHandler
(this.Form1_Closing);
Code automatically added for method heading
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
}
You can add statement to event-handler method
body
MessageBox.Show("Hope you are having fun!");
Simple Windows Application
New with Visual Studio 2010, the IDE separates the
source code into three separate files
 Form1.cs: Normally this is the only one you edit
 Form1.Designer.cs: Holds the auto-generated code
 Program.cs: Contains the Main( ) method, where
execution always begins
Form1.cs and Form1.Designer.cs both include
partial class definitions for the Form1 class
Windows Form Events (continued)
Figure 8-7 Solution Explorer window
Expand Form1.cs
node to reveal the
Form1.Designer.cs
file
Controls
Controls are all classes
 Button, Label, TextBox, ComboBox, MainMenu,
ListBox, CheckBox, RadioButton, and
DateTimePicker …
Each comes with its own predefined properties and
methods
Each fires events
Each is derived from the
System.Windows.Forms.Control class
Controls (continued)
Dots
indicate
other
classes
are
derived
from the
class
Figure 8-9 Control class hierarchy
Standard Controls
Figure 8-10 Windows Forms controls
Creating a form
Properties set for the Form
container
Sample Form with Controls
Figure 8-11 GUI controls
Controls (continued)
Two procedures to place controls
 From Toolbox, double-click on control or drag and drop
Move, resize, and delete controls
Format controls
 Align controls
 Make same size
 Horizontal and vertical spacing
Properties of the Control Class
Methods of the Control Class
Label Objects
Provides descriptive text or labels for other controls
Instantiate object
Label labelName = new Label( );
Add control to Form
this.Controls.Add(labelName);
Set property values (some from Control class)
 Text; TextAlign; Font; Location
Adding Labels to Form
Add Label objects, then set their
properties using the Properties
window
(View Properties window)
TextBox Objects
Used to enter data or display text during run time
 Used for both input and output
Instantiate object
TextBox textBoxName = new TextBox( );
Add control to Form
this.Controls.Add(TextBoxName);
Interesting properties
 MultiLine, ScollBars, MaxLength, PasswordChar,
CharacterCasing
TextBox Objects (continued)
Adding TextBox Objects to Form…
Add TextBox objects,
then set their property
values
Button
Enables user to click button to perform task
 If button has event-handler method and is registered as
an event to which your program is planning to respond,
event-handler method is called automatically when
button clicked
Button object’s properties, methods, and events
 Inherits from Control
 Text, Enabled, Focused, TabIndex
Adding Button Objects to Form
Add Button objects,
then set their property
values
Adding Button Objects to Form (continued)
Figure 8-14 Events
Click to see
list of events
Double-click
to create an
event-handler
method
Add other controls to form
Combo box
Menu strip (Call Sample Salary Form)
List box
Date Time picker
Check box
Radio button
Check box list
Error provider
Sample Salary Calculator
Timer Control
A Timer control raises an event at a given interval.
 If you need to execute some code after certain interval of
time continuously, you can use a timer control.
Windows Forms have a Timer control that can be used at
design time as well as at run-time
Properties
Enabled
 Gets or sets whether the timer is running.
Interval
 Gets or sets the time, in milliseconds, before the Tick event is raised
relative to the last occurrence of the Tick event.
Methods and Event
Method
 Protected method OnTick Raises the Tick event.
 Public method Start Starts the timer.
 Public method Stop Stops the timer.
Event
 Tick
 Occurs when the specified timer interval has elapsed and the timer is
enabled.
Question ??

4.C#

  • 1.
  • 2.
    Chapter Objectives Differentiate betweenthe functions of Windows applications and console applications Learn about graphical user interfaces Become aware of some elements of good design Use C# and Visual Studio to create Windows-based applications
  • 3.
    Chapter Objectives (continued) CreateWindows forms and be able to change form properties Add control objects such as buttons, labels, and text boxes to a form Work through a programming example that illustrates the chapter’s concepts
  • 4.
    Windows Application Basics Windows Forms is the event base smart-client component of the .NET Framework.  Application run locally on users' computers.Once launched  Set of managed libraries that enable common application tasks such as reading and writing to the file system.  A form is a visual surface on which you display information to the user. You commonly build Windows Forms applications by placing controls on forms and developing responses to user actions, such as mouse clicks or key presses. A control is a discrete user interface (UI) element that displays data or accepts data input.
  • 5.
    Graphical User Interfaces Interface:front end of a program  Visual image you see when you run a program Graphical user interface (GUI) includes:  Menus  Text in many different colors and sizes  Other controls (pictures, buttons, etc.)
  • 6.
    Windows Applications Reference andimport System.Windows.Forms namespace Class heading definition  Includes not only the class name, but a colon followed by another class name  Derived class (first class)  Base class (second class)  public class Form1 : Form Derived classes inherit from base class
  • 7.
    Windows Applications (continued) Text A property for setting/getting title bar caption Name  Unique name for all controls Windows forms/controls offer many properties including Text, Color, Font, and Location,Size Execution begins in Main( ) method  Main( ) is located in Program.cs file for the application  Call to Run( ) method places application in process loop
  • 8.
    using System.Windows.Forms; //Line 1 namespace Windows0 { public class Form1 : Form // Line 2 { public Form1( ) // Line 3 { InitializeComponent(); Text = "Simple Windows Application"; // Line 4 } } } New namespace referenced Constructor Base class Sets title bar caption Starts process loop
  • 9.
    Windows Application (continued) Figure8-1 Windows-based form Output generated from sample from application
  • 10.
    Elements of GoodDesign Appearance matters  Human-computer interaction (HCI) research Design considerations  Consistency  Alignment  Avoid Clutter  Color  Target Audience
  • 11.
    Use Visual Studioto Create Windows- based Applications Windows Application template Browse to location to store your work Select File New Project Name Figure 8-2 Visual Studio New Windows application
  • 12.
    Windows-based Applications Properties Window Design View Toolbox Switch between Designand Code view using View menu Figure 8-3 Initial design screen
  • 13.
    Windows-based Applications (continued) Figure8-4 Dockable windows Properties Auto-hide Solution Explorer pushpin
  • 14.
    Windows Forms Extensive collectionof Control classes Top-level window for an application is called a Form Each control has large collection of properties and methods , Events  Select property from an alphabetized list (Properties window)  Change property by clicking in the box and selecting or typing the new entry
  • 15.
    Windows Form Properties Properties Propertyvalue Figure 8-5 Properties window Categorized Alphabetical Events
  • 16.
  • 17.
    Windows Form Events Addcode to respond to events, like button clicks From the Properties window, select the lightening bolt (Events)  Double-click on the event name to generate code  Registers the event as being of interest  Adds a heading for event-handler method
  • 18.
    Windows Form Properties(continued) Events button selected Figure 8-6 Form1 events
  • 19.
    Windows Form –Closing Event Code automatically added to register event this.Closing += new System.ComponentModel.CancelEventHandler (this.Form1_Closing); Code automatically added for method heading private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { } You can add statement to event-handler method body MessageBox.Show("Hope you are having fun!");
  • 20.
    Simple Windows Application Newwith Visual Studio 2010, the IDE separates the source code into three separate files  Form1.cs: Normally this is the only one you edit  Form1.Designer.cs: Holds the auto-generated code  Program.cs: Contains the Main( ) method, where execution always begins Form1.cs and Form1.Designer.cs both include partial class definitions for the Form1 class
  • 21.
    Windows Form Events(continued) Figure 8-7 Solution Explorer window Expand Form1.cs node to reveal the Form1.Designer.cs file
  • 22.
    Controls Controls are allclasses  Button, Label, TextBox, ComboBox, MainMenu, ListBox, CheckBox, RadioButton, and DateTimePicker … Each comes with its own predefined properties and methods Each fires events Each is derived from the System.Windows.Forms.Control class
  • 23.
  • 24.
    Standard Controls Figure 8-10Windows Forms controls
  • 25.
    Creating a form Propertiesset for the Form container
  • 26.
    Sample Form withControls Figure 8-11 GUI controls
  • 27.
    Controls (continued) Two proceduresto place controls  From Toolbox, double-click on control or drag and drop Move, resize, and delete controls Format controls  Align controls  Make same size  Horizontal and vertical spacing
  • 28.
    Properties of theControl Class
  • 29.
    Methods of theControl Class
  • 30.
    Label Objects Provides descriptivetext or labels for other controls Instantiate object Label labelName = new Label( ); Add control to Form this.Controls.Add(labelName); Set property values (some from Control class)  Text; TextAlign; Font; Location
  • 31.
    Adding Labels toForm Add Label objects, then set their properties using the Properties window (View Properties window)
  • 32.
    TextBox Objects Used toenter data or display text during run time  Used for both input and output Instantiate object TextBox textBoxName = new TextBox( ); Add control to Form this.Controls.Add(TextBoxName); Interesting properties  MultiLine, ScollBars, MaxLength, PasswordChar, CharacterCasing
  • 33.
  • 34.
    Adding TextBox Objectsto Form… Add TextBox objects, then set their property values
  • 35.
    Button Enables user toclick button to perform task  If button has event-handler method and is registered as an event to which your program is planning to respond, event-handler method is called automatically when button clicked Button object’s properties, methods, and events  Inherits from Control  Text, Enabled, Focused, TabIndex
  • 36.
    Adding Button Objectsto Form Add Button objects, then set their property values
  • 37.
    Adding Button Objectsto Form (continued) Figure 8-14 Events Click to see list of events Double-click to create an event-handler method
  • 38.
    Add other controlsto form Combo box Menu strip (Call Sample Salary Form) List box Date Time picker Check box Radio button Check box list Error provider
  • 39.
  • 40.
    Timer Control A Timercontrol raises an event at a given interval.  If you need to execute some code after certain interval of time continuously, you can use a timer control. Windows Forms have a Timer control that can be used at design time as well as at run-time
  • 41.
    Properties Enabled  Gets orsets whether the timer is running. Interval  Gets or sets the time, in milliseconds, before the Tick event is raised relative to the last occurrence of the Tick event.
  • 42.
    Methods and Event Method Protected method OnTick Raises the Tick event.  Public method Start Starts the timer.  Public method Stop Stops the timer. Event  Tick  Occurs when the specified timer interval has elapsed and the timer is enabled.
  • 43.