Download presentation
Presentation is loading. Please wait.
Published byJessica Chapman Modified over 9 years ago
1
COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus
2
Lecture Contents: Contrast btw the functions of Windows applications and console applications GUI - graphical user interfaces Windows forms and form properties Control objects such as buttons, labels, and text boxes to a form
3
Contrasting Windows and Console Applications Console applications –Each line in Main( ) method executed sequentially – then the program halts –Method calls might branch to different locations in the program, however control always returns back to Main –Program initiates interaction with the user by calling the OS to get data using ReadLine() method –Program calls OS to output data through method calls like WriteLine() or Write() Console applications run IPO model of computing process
4
Contrasting Windows and Console Applications Windows applications –Instead of the program executing sequential statements from top to bottom, the application, once launched, sits in what is called a process loop and waits for an event –Sits in a process loop, waiting for event to execute Windows applications run Event-driven model of a computing process
5
Contrasting Windows and Console Applications Event: notification from OS that an action, such as the user clicking the mouse or the user pressing a key, has occurred Instead of calling the OS with IO request as console applications, Windows applications receive messages from OS that event has occurred It is must to write methods, called Event Handlers to indicate what should be done when an event occurs
6
Graphical User Interface Windows applications also look different from console applications User Interface: front end of a program –Visual image you see when you run a program –Algorithmic functionality stays behind GUI –Often users of programs actually identify the interface of the program itself, when in reality, the interface is just one facet of the program. –Algorithmic complexity is hidden behind GUI.
7
Graphical User Interface Graphical user interface (GUI) includes: –Menus, labels, text boxes, list boxes, –Other controls (pictures, buttons, etc.) –Text in many different colors and sizes Controls are objects that can display and respond to user interaction. Controls are placed in a container, called form, which is an object as well. It is instance of a class derived from predefined class Form
8
Windows Based Applications Windows Forms Events Controls
9
Chapter 9 Introduction to Windows Programming C# Programming: From Problem Analysis to Program Design 4th Edition
10
C# Programming: From Problem Analysis to Program Design10 Contrasting Windows and Console Applications Windows applications function differently from console applications. Windows applications look differently from console applications.
11
C# Programming: From Problem Analysis to Program Design11 Contrasting Windows and Console Applications by Functionality Console applications –Each line in Main( ) executed sequentially – then the program halts Windows applications –Once launched, sits and waits for an event –Sits in a process loop Event: notification from operating system that an action, such as the user clicking the mouse or pressing a key, has occurred –Write event-handler methods for Windows apps
12
C# Programming: From Problem Analysis to Program Design12 Graphical User Interfaces Windows applications also look different from console applications 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.)
13
C# Programming: From Problem Analysis to Program Design13 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 No multiple inheritance within.NET languages
14
C# Programming: From Problem Analysis to Program Design14 Windows Applications ( continued ) Text - property of the Form class –A property for setting/getting title bar caption –Can be used in constructor Windows forms/controls offer many properties including Text, Color, Font, and Location Execution begins in Main( ) method –Main( ) is located in Program.cs file for the application –Call to Run( ) method places application in process loop
15
C# Programming: From Problem Analysis to Program Design15 // Windows0.cs Author: Doyle using System.Windows.Forms; // Line 1 namespace Windows0 { public class Form1 : Form // Line 2 { public Form1( ) // Line 3 { Text = "Simple Windows Application"; // Line 4 } static void Main( ) { Form1 winForm = new Form1( ); // Line 5 Application.Run(winForm); // Line 6 } New namespace referenced Constructor Base class Sets title bar caption Starts process loop
16
C# Programming: From Problem Analysis to Program Design16 Windows Application ( continued ) Figure 9-1 Windows-based form Output generated from Windows0 application
17
C# Programming: From Problem Analysis to Program Design17 Elements of Good Design Appearance matters –Human-computer interaction (HCI) research Design considerations –Consistency –Alignment –Avoid clutter –Color –Target audience
18
C# Programming: From Problem Analysis to Program Design18 Use Visual Studio to Create Windows-Based Applications Windows Forms Application template Browse to location to store your work Name Figure 9-2 Visual Studio New Windows application Select File New Project
19
C# Programming: From Problem Analysis to Program Design19 Windows-Based Applications Properties Window Toolbox Figure 9-3 Initial design screen Design View
20
C# Programming: From Problem Analysis to Program Design20 Windows-Based Applications ( continued ) Figure 9-4 Dockable windows
21
Windows Based Applications Windows Forms –Properties –Events
22
C# Programming: From Problem Analysis to Program Design22 Windows Forms Extensive collection of Control classes Top-level window for an application is called a Form Each control has collection of properties and methods –Select property from an alphabetized list (Properties window) –Change property by clicking in the box and selecting or typing the new entry at design time. Each control has collection of events.
23
C# Programming: From Problem Analysis to Program Design23 Windows Form Properties Properties Figure 9-5 Properties window Property value
24
Windows Form Properties (change values at run time) Can set properties using program statements –Table 9-1 shows properties set using Properties window Selecting Code on View menu shows associated code C# Programming: From Problem Analysis to Program Design24 Table 9-1 Form1 property changes
25
Windows Form Events C# Programming: From Problem Analysis to Program Design25 Figure 9-6 Form events
26
Inspecting the Code Generated by Visual Studio Three source code files ending with a.cs extension are part of the application C# Programming: From Problem Analysis to Program Design26 Figure 9-7 Solution Explorer window Expand Form1.cs node to reveal the Form1.Designer.cs file
27
C# Programming: From Problem Analysis to Program Design27 Simple Windows Application 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
28
Inspecting the Code - Form1.cs Number of namespaces automatically added, including System.Windows.Forms Constructor calls InitializeComponent( ) method public Form1( ) { // Required for Windows Form Designer support. InitializeComponent( ); } This is the file where event handler methods will be placed C# Programming: From Problem Analysis to Program Design28
29
Inspecting the Code - Form1.Designer.cs InitializeComponent( ) method included here #region Windows Form Designer generated code preprocessor directive –// do not modify the contents of this method with the Code Editor –Keyword “this.” precedes property name Refers to current instance of the class –#endregion // Ends the preprocessor directive C# Programming: From Problem Analysis to Program Design29
30
InitializeComponent( ) Method BackColor = Color.FromArgb (((Byte)(255)), ((Byte)(224)), ((Byte)(192))); ClientSize = new Size(392, 373); Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Point, ((Byte)(0))); ForeColor = Color.Blue; Location = new Point(30, 30); Margin = new Padding(4); MaximizeBox = false; Name = "Form1"; StartPosition = FormStartPosition.CenterScreen; Text = "First Windows Application"; Some of the auto generated code in the method –Added as default values for properties or from changing property values C# Programming: From Problem Analysis to Program Design30
31
Windows Based Applications Events
32
C# Programming: From Problem Analysis to Program Design32 Windows Form Events Add code to respond to events, like button clicks –Code goes into Form1.cs file From the Properties window, select the lightning 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
33
C# Programming: From Problem Analysis to Program Design33 Windows Form Properties ( continued ) Events button selected Figure 9-6 Form1 events
34
C# Programming: From Problem Analysis to Program Design34 Windows Form – Load Event Code automatically added to register event Code automatically added for method heading private void Form1_Load(object sender, EventArgs e) { } You add statement to event-handler method body this.BackColor = Color.Azure;
35
C# Programming: From Problem Analysis to Program Design35 Windows Form – FormClosing Event Code automatically added to register event Code automatically added for method heading private void Form1_FormClosing(object sender, FormClosingEventArgs e) { } You add statement to event-handler method body MessageBox.Show("Hope you are having fun!");
36
Running the Windows Application No changes needed in the file that has Main( ) Run like you do console applications (F5 or Ctrl+F5) C# Programming: From Problem Analysis to Program Design36 Figure 9-8 Output produced when the Close button causes the event-handler method to fire Review FirstWindows Example
37
Windows Based Applications Controls
38
C# Programming: From Problem Analysis to Program Design38 Controls Controls are all classes –Button, Label, TextBox, ComboBox, MainMenu, ListBox, CheckBox, RadioButton, and MonthCalendar Each comes with its own predefined properties and methods Each fires events Each is derived from the System.Windows.Forms.Control class
39
C# Programming: From Problem Analysis to Program Design39 Controls ( continued ) Dots indicate other classes are derived from the class Figure 9-9 Control class hierarchy
40
C# Programming: From Problem Analysis to Program Design40 Standard Controls Figure 9-10 Windows Forms controls
41
C# Programming: From Problem Analysis to Program Design41 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
42
C# Programming: From Problem Analysis to Program Design42 Properties of the Control class Table 9-2 Systems.Windows.Forms.Control class properties
43
Properties of the Control class ( continued ) C# Programming: From Problem Analysis to Program Design43 Table 9-2 Systems.Windows.Forms.Control properties ( continued )
44
Methods of the Control class Control class has over 75 properties and over 100 methods –Not all are useful for every class that derives from it C# Programming: From Problem Analysis to Program Design44 Table 9-3 Systems.Windows.Forms.Control methods Table 9-3 includes a short list of some of the many methods. Explore MSDN documentation for more
45
C# Programming: From Problem Analysis to Program Design45 Controls ( continued ) Figure 9-11 GUI controls
46
Practical Tasks Write a Windows based application – pure empty form with no any user added forms, controls or events
47
Practical Tasks Write a Windows based application – pure empty form with modified Text property at design time using Properties window
48
Practical Tasks Write a Windows based application – pure empty form with two event handlers: Event Load – when the form gets loaded –To modify Text property and BackColor property at run time Event FormClosing – when the form gets closed – prior to app termination –Call Message.Show() method
49
Thank You For Your Attention
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.