Object Oriented Programming Graphical User Interfaces Dr. Mike Spann

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Visual Studio 2010 and Controls.
Advertisements

 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
© by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Visual Basic 2010 How to Program Reference: Instructor: Maysoon Bin Duwais slides Visual Basic 2010 how to program by Deitel © by Pearson Education,
.NET Class 4 – Windows-based Application. WinForm Application Homogeny programming model. Rich class library Classes are shared by all.NET languages.
Graphical User Interface (GUI) A GUI allows user to interact with a program visually. GUIs are built from GUI components. A GUI component is an object.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
1 Windows Printing. 2 Objectives You will be able to Output text and graphics to a printer. Print multipage documents. Use the standard Windows print.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
1 Graphical User Interfaces Part 2 Outline Multiple Document Interface (MDI) Windows Visual Inheritance User-Defined Controls.
Dr Dat Tran - Week 4 Lecture Notes 1 MenuStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
IE 411/511: Visual Programming for Industrial Applications
CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Lecture 7: WinForms & Controls, Part MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Visual Studio.NET ships with a wealth.
Object Oriented Programming Graphics and Multimedia Dr. Mike Spann
BIM211 – Visual Programming Objects, Collections, and Events 1.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
1 Working with Menus and Dialog Boxes. 2 Objectives You will be able to Create and edit menus for Windows Forms applications. Write code to handle menu.
Chapter 9 - VB.Net by Schneider1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes, Combo Boxes, and the File-Opening Control The List Box Control.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
BIL528 – Bilgisayar Programlama II Introduction 1.
Introduction to Windows Programming
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
C# GUI - Basics. Objectives.NET supports two types: WinForms, traditional, desktop GUI apps. WebForms – newer, for Web apps. Visual Studio.NET supports.
Chapter 2 – Introduction to the Visual Studio .NET IDE
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Developing Windows Applications with Visual Studio Windows Forms Controls and Coding Jim Warren – COMPSCI 280 S Enterprise.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Forms and Controls Part 3 dbg --- Naming conventions, Button, Event handlers, Label.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
Lecture Set 7 Procedures and Event Handlers Part B - The Structure of an Application Event Handlers.
Appendix A: Windows Forms. 2 Overview Describe the structure of a Windows Forms application –application entry point –forms –components and controls Introduce.
1 Windows Forms II Chapter RadioButton / GroupBox Controls Used to solicit a multiple choice input. Radio buttons work as a group. Selecting one.
User Interface Programming in C#: Basics and Events Chris North CS 3724: HCI.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
 2002 Prentice Hall. All rights reserved. 1 Introduction to the Visual Studio.NET IDE Outline Introduction Visual Studio.NET Integrated Development Environment.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Dr. Abraham Professor UTPA Graphical User Interface.
 You won’t write a single line of program code.  Instead, you’ll use visual programming techniques.  Visual Studio processes your actions (such as mouse.
Windows form programming. namespace MyNamespace { public class MyForm : System.Windows.Forms.Form { public MyForm() { this.Text = "Hello Form"; }
Dive Into® Visual Basic 2010 Express
Visual Basic .NET BASICS
Computing with C# and the .NET Framework
Microsoft Visual Basic 2005 BASICS
CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming UTPA – Fall 2012 This set of slides is revised from lecture.
Object Oriented Programming
Chapter 2 – Introduction to the Visual Studio .NET IDE
3.01 Apply Controls Associated With Visual Studio Form
3.01 Apply Controls Associated With Visual Studio Form
Reference: COS240 Syllabus
Windows Desktop Applications
The University of Texas – Pan American
Self Study GUI Properties & Events
Object Oriented Programming
6. WinForms 2003 C# GUI - Basics.
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Presentation transcript:

Object Oriented Programming Graphical User Interfaces Dr. Mike Spann

Contents Introduction Introduction Creating a simple GUI Creating a simple GUI Visual programming Visual programming Example – A simple image viewer Example – A simple image viewer Mouse event handling Mouse event handling Using Help Using Help Summary Summary

Introduction Most modern applications come with a sophisticated user interface comprising Most modern applications come with a sophisticated user interface comprising  Push buttons  Selection boxes  Dialog boxes  Pull down menus  etc This lecture focuses on the use of the classes found in the System.Windows.Forms namespace in the FCL to create these user interfaces This lecture focuses on the use of the classes found in the System.Windows.Forms namespace in the FCL to create these user interfaces  These are the classes that you will use if you are writing client-side GUI applications with the.NET Framework

Introduction For even moderately sophisticated GUI’s, it is advisable to use visual programming techniques under Visual Studio For even moderately sophisticated GUI’s, it is advisable to use visual programming techniques under Visual Studio  A lot of code is automatically generated  Easy placement of GUI components  Extensive use of properties to customize our GUI components  Essentially all we need to do is write the event handlers However, it is important to understand how it all works and how we could write GUI’s ourselves from scratch However, it is important to understand how it all works and how we could write GUI’s ourselves from scratch

Introduction The Form class is a basic outer container which then holds GUI components such as menus, buttons and checkboxes The Form class is a basic outer container which then holds GUI components such as menus, buttons and checkboxes Usually GUI’s comprise a class derived from Form to create a specialization that fits the need of the application Usually GUI’s comprise a class derived from Form to create a specialization that fits the need of the application using System.Windows.Forms; class App { public static void Main() { Application.Run(new Form()); }

Introduction This programs produces a simple outer container with basic windows functionality that we are familiar with This programs produces a simple outer container with basic windows functionality that we are familiar with  Maximize button  Minimize button  etc We can easily add components to this to specialize its behaviour We can easily add components to this to specialize its behaviour

Creating a simple GUI We can create a GUI containing a button and add an event handler to provide some functionality We can create a GUI containing a button and add an event handler to provide some functionality Normally much of this code would be generated automatically using visual programming techniques Normally much of this code would be generated automatically using visual programming techniques  We would just have to add the event handling code to specify the behaviour we require  In this case a simple pop up message box is displayed

Creating a simple GUI using System; using System.Windows.Forms; class App { public static void Main() { Application.Run(new MySimpleForm()); } class MySimpleForm : Form { public MySimpleForm() { Button button = new Button(); button.Text = "My Button"; button.Click += new EventHandler(OnClick); this.Controls.Add(button); } void OnClick(Object sender, EventArgs args) { MessageBox.Show("The Button Was Clicked!"); }

Creating a simple GUI Demos\Simple Form\MySimpleForm.exe Demos\Simple Form\MySimpleForm.exe Demos\Simple Form\MySimpleForm.exe Demos\Simple Form\MySimpleForm.exe

Visual programming Visual programming allows us to create GUI’s in designer view using Visual Studio Visual programming allows us to create GUI’s in designer view using Visual Studio It involves dragging and dropping GUI components from a toolbox onto an outer Form container It involves dragging and dropping GUI components from a toolbox onto an outer Form container If we select the Windows Application project template, we automatically get the project up in designer view with a small amount of pre-generated code If we select the Windows Application project template, we automatically get the project up in designer view with a small amount of pre-generated code We can drag GUI components from the toolbox onto our form and position them as we wish We can drag GUI components from the toolbox onto our form and position them as we wish Clicking on a component (eg a button), adds an event handler for that component and the view changes from designer view to code view allowing us to add code for the event handler Clicking on a component (eg a button), adds an event handler for that component and the view changes from designer view to code view allowing us to add code for the event handler

Visual programming For example we can re-create our simple GUI and only have to write 1 line of code!! For example we can re-create our simple GUI and only have to write 1 line of code!! Also we can easily customize the look of the GUI by updating the properties Also we can easily customize the look of the GUI by updating the properties  Button text  Background colour  Button placement  etc

Visual programming using System; using System.Windows.Forms; namespace MySimpleForm_VP { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // User code here!! MessageBox.Show("The Button Was Clicked!"); }

Visual programming Automatically generated code is put in the xxxx.Designer.cs file Automatically generated code is put in the xxxx.Designer.cs file  The InitializeComponent method involves creating the GUI components and their properties and registering event handlers Also a main method is automatically generated which calls Application.Run(...) Also a main method is automatically generated which calls Application.Run(...)

namespace MySimpleForm_VP { partial class Form1 {. #region Windows Form Designer generated code private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(-1, 0); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "My button"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; }

using System; using System.Windows.Forms; namespace MySimpleForm_VP { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }

Visual programming The code is automatically updated when we customize our GUI appearance using the VS properties window The code is automatically updated when we customize our GUI appearance using the VS properties window

Visual Designer demo

Example – A simple image viewer We can create a simple image viewer for loading and displaying images We can create a simple image viewer for loading and displaying images We will use visual programming and write a small amount of code We will use visual programming and write a small amount of code This example will introduce the following GUI components This example will introduce the following GUI components  Menus  File dialogue boxes

Example – A simple image viewer Menus can be easily added to a Form in design view Menus can be easily added to a Form in design view A MenuStrip is dragged across from the toolbox to create the menu bar A MenuStrip is dragged across from the toolbox to create the menu bar Textboxes are then available to type in the names of the menu’s and menu items Textboxes are then available to type in the names of the menu’s and menu items  This can be repeated for sub menu’s  Shortcuts can be added by entering an & before the name

menustrip icon menu menu items

Example – A simple image viewer We can add an OpenFileDialog box by dragging it from the toolbox We can add an OpenFileDialog box by dragging it from the toolbox We also automatically add function headers for each event handler by clicking on the menu items and the OpenFileDialog icon We also automatically add function headers for each event handler by clicking on the menu items and the OpenFileDialog icon  The only code we need are the function bodies of the event handlers

public partial class Form1 : Form { private readonly string initialImageDirectory= "C:\\Documents and Settings\\spannm\\Desktop\\Personal Stuff\\Images"; private Image loadedImage = null; public Form1() { InitializeComponent(); openFileDialog1.InitialDirectory=initialImageDirectory; } private void openToolStripMenuItem_Click(object sender, EventArgs e) { // Open image file dialog box and load an image openFileDialog1.ShowDialog(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { // Exit application Application.Exit(); } private void closeToolStripMenuItem_Click(object sender, EventArgs e) { // Close the image } private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { string fileName = openFileDialog1.FileName; if (fileName.Length != 0) { // Load the image }

Example – A simple image viewer We need to decide how we wish to display the image We need to decide how we wish to display the image  We could drag a PictureBox component inside the form  This then displays the image inside the Form container  It’s much more realistic to have the image in a completely independent window  This requires us to build a separate ImageForm class

Example – A simple image viewer We can create a separate windows application comprising a Form containing a PictureBox We can create a separate windows application comprising a Form containing a PictureBox  The PictureBox class has an Image property which is the image it is displaying  The Load() event handler displays the image when the form is created  All very easy to do in design view of VS

Example – A simple image viewer public partial class ImageForm : Form { private Image image; public ImageForm(Image im) { image = im; InitializeComponent(); } private void ImageForm_Load(object sender, EventArgs e) { pictureBox1.Image = image; }

Example – A simple image viewer The only real code we have to write is the event handler for loading and displaying an image from file The only real code we have to write is the event handler for loading and displaying an image from file  This appears in the openFileDialog1_FileOk() event handler which is called when the open file dialog box ‘OK’ button is pressed  An ImageForm object is created and the loaded image passed to its constructor  Minimal code for a fairly sophisticated application

Example – A simple image viewer public partial class Form1 : Form { private Image loadedImage = null; private ImageForm imageForm = null;. private void closeToolStripMenuItem_Click(object sender, EventArgs e) { // Close the image if (imageForm != null) imageForm.Close(); } private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { string fileName = openFileDialog1.FileName; if (fileName.Length != 0) { // Display the loaded image loadedImage = Image.FromFile(fileName); imageForm = new ImageForm(loadedImage); imageForm.Show(); }

Example – A simple image viewer Demos\Basic Image Viewer\Image Viewer Basic.exe Demos\Basic Image Viewer\Image Viewer Basic.exe Demos\Basic Image Viewer\Image Viewer Basic.exe Demos\Basic Image Viewer\Image Viewer Basic.exe

Example – A simple image viewer It’s easy to change both the look and the behaviour of the tool without additional code It’s easy to change both the look and the behaviour of the tool without additional code  We simply change properties  For example, we can change how the image is displayed by altering the SizeMode property of the PictureBox  SizeMode=Normal - displays image without scaling in the top left hand corner of the PictureBox  SizeMode=AutoSize – resizes the PictureBox to hold the image  etc

Example – A simple image viewer SizeMode=AutoSize SizeMode=Normal

Mouse event handling Mouse events occur when the user clicks, presses and moves the mouse Mouse events occur when the user clicks, presses and moves the mouse Being able to handle mouse events can lead to highly interactive applications Being able to handle mouse events can lead to highly interactive applications Information about the mouse event is passed to an event handling method through an object of class MouseEventArgs or EventArgs Information about the mouse event is passed to an event handling method through an object of class MouseEventArgs or EventArgs  Typically this might be the x and y coordinates of the position of the mouse cursor The delegate used to create the event handlers is MouseEventHandler The delegate used to create the event handlers is MouseEventHandler

Mouse event handling We can summarise mouse events and event properties as follows: We can summarise mouse events and event properties as follows:

Mouse event handling For example, we can paint graphics on the image as displayed in our image viewer For example, we can paint graphics on the image as displayed in our image viewer We make use of the MouseDown, MouseUp and MouseMove events We make use of the MouseDown, MouseUp and MouseMove events With the ImageForm in design view, we click on the mouse events for which we wish to add event handlers With the ImageForm in design view, we click on the mouse events for which we wish to add event handlers  Automatic code is generated for the empty functions  We need to fill in the function bodies

For example, we can combine the MouseDown and MouseMove event handler to draw freehand graphics as we move the mouse For example, we can combine the MouseDown and MouseMove event handler to draw freehand graphics as we move the mouse Or we can just select coordinates on the image using MouseDown and draw the rectangle through opposing vertices Or we can just select coordinates on the image using MouseDown and draw the rectangle through opposing vertices  Useful in selecting a region of interest in an image processing application Mouse event handling

public partial class ImageForm : Form { private Image image; private bool shouldPaint = false; public ImageForm(Image im) { image = im; InitializeComponent(); } private void ImageForm_MouseMove(object sender, MouseEventArgs e) { if (shouldPaint) { Graphics graphics = CreateGraphics(); graphics.FillEllipse(new SolidBrush(Color.Blue), e.X, e.Y, 4, 4); graphics.Dispose(); } private void ImageForm_MouseDown(object sender, MouseEventArgs e){shouldPaint = true;} private void ImageForm_MouseUp(object sender, MouseEventArgs e) {shouldPaint = false;} private void ImageForm_Paint(object sender, PaintEventArgs e) { Graphics graphics = e.Graphics; graphics.DrawImage(image, new Point(20,20)); }

Mouse event handling Demos\Image Viewer Free Draw\Image Viewer.exe Demos\Image Viewer Free Draw\Image Viewer.exe Demos\Image Viewer Free Draw\Image Viewer.exe Demos\Image Viewer Free Draw\Image Viewer.exe

Mouse event handling public partial class ImageForm : Form { private Image image; private int startX=-1; private int startY, endX, endY; public ImageForm(Image im) { image = im; InitializeComponent(); } private void ImageForm_MouseDown(object sender, MouseEventArgs e) { if (startX < 0) { startX = e.X; startY = e.Y; } else { endX = e.X; endY = e.Y; Graphics graphics = CreateGraphics(); Pen whitePen = new Pen(Color.White, 2); graphics.DrawRectangle(whitePen, new Rectangle(startX,startY,endX-startX,endY-startY)); } private void ImageForm_Paint(object sender, PaintEventArgs e) { Graphics graphics = e.Graphics; graphics.DrawImage(image, new Point(20,20)); }

Mouse event handling Demos\Image Viewer ROI\Image Viewer.exe Demos\Image Viewer ROI\Image Viewer.exe Demos\Image Viewer ROI\Image Viewer.exe Demos\Image Viewer ROI\Image Viewer.exe

Mouse event handling Both these examples use the Graphics object and other objects in the System.Drawing namespace such as Pen and SoilidBrush Both these examples use the Graphics object and other objects in the System.Drawing namespace such as Pen and SoilidBrush  More about these concepts in the lecture on graphics and multimedia We use the Graphics.DrawImage() method to draw the image in the Paint() event handler We use the Graphics.DrawImage() method to draw the image in the Paint() event handler  Paint() is called automatically whenever the form needs repainting  For example when the form is moved or resized

Summary We have seen how we can create a simple GUI and add an event handler to determine the behaviour of the GUI We have seen how we can create a simple GUI and add an event handler to determine the behaviour of the GUI We have also seen how we can do the same thing much simpler using visual programming techniques We have also seen how we can do the same thing much simpler using visual programming techniques We have looked at a simple image viewer application which uses a menu and a file input dialog box We have looked at a simple image viewer application which uses a menu and a file input dialog box We have looked at how we can handle mouse events We have looked at how we can handle mouse events