UTG (3) UTG - Online Poker Forum · r r r r r4 r r r r4 c>4 r r c>4 c>4 c>4 c>4
4.C#
-
Upload
raghu-nath -
Category
Technology
-
view
247 -
download
1
Transcript of 4.C#
Introduction to Windows Programming
Chapter Objectives
Differentiate between the functions of Windows applications and console applications
Learn about graphical user interfacesBecome aware of some elements of good designUse 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 1namespace 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
PropertiesAuto-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
PropertiesProperty 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 eventthis.Closing += new
System.ComponentModel.CancelEventHandler (this.Form1_Closing);
Code automatically added for method headingprivate void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e){}
You can add statement to event-handler method bodyMessageBox.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 objectLabel 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 FormAdd 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 objectTextBox 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
ButtonEnables 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 boxMenu strip (Call Sample Salary Form)List boxDate Time pickerCheck boxRadio buttonCheck box listError 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 ??