Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 1 9.1 Developing a Simple Graphical User Interface (GUI)

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

1 Application Software Course Simulink By: Mahdi Akbari 2010.
Getting Started with Microsoft Office 2007
Chapter 16 Graphical User Interfaces
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Lists, Loops, Validation, and More
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Copyright © 2003 Pearson Education, Inc. Slide 1.
BASIC SKILLS AND TOOLS USING ACCESS
Writing Pseudocode And Making a Flow Chart A Number Guessing Game
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Mike Scott University of Texas at Austin
Microsoft®.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Windows XP Project An Introduction to Microsoft Windows XP and Office 2003.
6. WinForms: GUI Programming in.NET. 2 Microsoft Objectives.NET supports two types of form-based apps, WinForms and WebForms. WinForms are the traditional,
View-Based Application Development Lecture 1 1. Flows of Lecture 1 Before Lab Introduction to the Game to be developed in this workshop Comparison between.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
1 NatQuery 3/05 An End-User Perspective On Using NatQuery To Extract Data From ADABAS Presented by Treehouse Software, Inc.
1 Northwind Traders Order Entry. 2 Northwind Traders Call Center Add an Order Entry capability to the Northwind Traders Call Center application. Start.
Data Structures Using C++
Yong Choi School of Business CSU, Bakersfield
1 1 Mechanical Design and Production Dept, Faculty of Engineering, Zagazig University, Egypt. Mechanical Design and Production Dept, Faculty of Engineering,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
XP New Perspectives on Introducing Microsoft Office 2003 Tutorial 1 1 Using Common Features of Microsoft Office 2003 Tutorial 1.
INTRODUCTION Lesson 1 – Microsoft Word Word Basics
Microsoft Office Illustrated Fundamentals Unit C: Getting Started with Unit C: Getting Started with Microsoft Office 2010 Microsoft Office 2010.
Chapter 11: The X Window System Guide To UNIX Using Linux Third Edition.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
COMPUTER INTERFACES.
1 Welcome to SAPS Webmail. 2 Things we will learn about: 1. Login to mail.
4 Oracle Data Integrator First Project – Simple Transformations: One source, one target 3-1.
Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.
© 2012 National Heart Foundation of Australia. Slide 2.
Macromedia Dreamweaver MX 2004 – Design Professional Dreamweaver GETTING STARTED WITH.
Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 8 Improving the User Interface
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Creating Windows Store Apps. 1. Overview of Windows Store applications 2. Worked example 3. Adding controls and event handlers Contents 2.
Chapter 2 –Visual Basic, Controls, and Events
© by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Automating Tasks With Macros
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Microsoft Visual Basic 2005: Reloaded Second Edition
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Working with Unmanaged Code.
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
Tutorial 121 Creating a New Web Forms Page You will find that creating Web Forms is similar to creating traditional Windows applications in Visual Basic.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
Object Oriented Software Development 9. Creating Graphical User Interfaces.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Controls. Adding Controls to Form -You can pick controls from the toolbox. -To add the controls from Toolbox to the Form You have be in design view. -To.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
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.
CSC 157 (Blum)1 Hello World. CSC 157 (Blum)2 Start/Programs/Microsoft Visual Studio.NET 2003/Microsoft Visual Studio.NET 2003.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
Managing C++ CHRIS DAHLBERG MID-TIER DEVELOPER SCOTTRADE.
Graphical User Interface Components Version 1.1. Obectives Students should understand how to use these basic Form components to create a Graphical User.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Developing a Simple Graphical User Interface (GUI)
CIS16 Application Development Programming with Visual Basic
CIS16 Application Development and Programming using Visual Basic.net
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Presentation transcript:

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Developing a Simple Graphical User Interface (GUI)

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 2 C++/Common Language Infrastructure (C++/CLI) So far the C++ programs we have learnt essentially follow ANSI standard (Native C++) – It means that those programs should be able to be compiled and run in any computer platform, including Windows, UNIX, Linux, Mac OS, etc. Recently, Microsoft has developed in its Visual C a new set of extension keywords (known as the C++/CLI) to generate code that targets the.NET Framework, which is also called managed code Hence the codes that do not use these extension keywords are known as unmanaged code. Native C++: Unmanaged code C++/CLI: Managed code

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 3 When developing an application using Visual C , it can be either a managed or unmanaged application An unmanaged application runs directly under Windows  No extra software is needed to run the application A managed application however needs to run in Common Language Runtime (CLR: Microsoft's implementation of CLI)  Your computer needs to install the Microsoft.NET Framework in order to run a managed application. Unmanaged application Managed C++.NET framework classes Common Language Runtime

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 4 So why do we want to develop managed application?  Most new Windows systems have included the.NET Framework – It is installed when installing Windows or its Updates  More importantly, Microsoft provides a set of powerful.NET class libraries that can be used only by managed applications One of the important tasks that greatly benefits from.NET class libraries is the development of graphical user interface (GUI).

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 5 Graphical User Interface User-friendliness is one of the important criteria to evaluate the merit of a computer system Particularly for home users, a user friendly operating system is very important Command-line interface is gradually replaced by Graphical User Interface (GUI) for personal computers Graphical User Interface  Using a graphical approach, through icons, menus, windows, etc. to interact with users. Mouse vs Keyboard Console application

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 6 A typical desktop of a personal computer – full of icons, menus, windows, etc. to communicate with user

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 7 Creating Windows Forms For Windows, GUI controls are contained in a form. Hence developing Windows Forms is the same as developing GUIs For unmanaged applications, developing Forms requires the full knowledge of MFC or ATL libraries, which are rather difficult to use and NOT supported by C++/CLI. Visual C provides Rapid Application Development (RAD) support for building managed Windows Forms applications Managed applications allow GUI to be incorporated in a program following a simple click-and-pick procedure  Something similar to using Visual Basic Managed Windows Forms applications can also use unmanaged C++ libraries  Allow GUI to be introduced into our previously developed unmanaged applications. MFC: Microsoft Foundation Library ATL: Active Template Library Partially known Vs Fully known window layout and controls System::Windows::Forms

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 8 Developing a simple GUI Step 1: Start the Visual Studio.NET. Click Create: Project...

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 9 Step 2 Select Windows Forms Application

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 10 A plain form will be created for you to fill in the details

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 11 Using the Toolbox to Build the User Interface The toolbox is a tabbed window that contains all GUI control elements If you cannot see it, click view → ToolBox Try to click the pin icon, see what happen The toolbox is a tabbed window that contains all GUI control elements If you cannot see it, click view → ToolBox Try to click the pin icon, see what happen Step 3

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 12 Step 3 From the toolbox, drag a label, a textbox and a button to the form

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 13 Step 4 You can find the property of each component in the Properties window (If cannot find it, select: View-> Other Windows -> Properties Window ) Try to modify the label to " Please enter your name: " and the label of the button to " OK " by modifying the Text item in their Properties You can find the property of each component in the Properties window (If cannot find it, select: View-> Other Windows -> Properties Window ) Try to modify the label to " Please enter your name: " and the label of the button to " OK " by modifying the Text item in their Properties

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 14 You can find in the Properties window the name of each control. e.g. the name for this text box is textBox1 It is important for developing program codes for them You can find in the Properties window the name of each control. e.g. the name for this text box is textBox1 It is important for developing program codes for them

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 15 Try to enlarge the form. You will find that the size of the textbox will not change accordingly. By default, all components are anchored with respect to the top and left boundaries of the form. Hence, not only the size, but the position of all components will not change when resizing the form Try to enlarge the form. You will find that the size of the textbox will not change accordingly. By default, all components are anchored with respect to the top and left boundaries of the form. Hence, not only the size, but the position of all components will not change when resizing the form

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 16 Step 5 Resize the form to the original Click the textbox On the Properties window, change the Anchor property to “ Top Left Right ” Try to enlarge your form. You will see the size of the textbox changes with the form size Resize the form to the original Click the textbox On the Properties window, change the Anchor property to “ Top Left Right ” Try to enlarge your form. You will see the size of the textbox changes with the form size

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 17 Writing Event Handler A major task that a GUI designer needs to do is to determine what will happen when a GUI is invoked Every GUI component may generate different kinds of “events” when a user makes access to it using his mouse or keyboard E.g. if a user moves his mouse on top of a button, an event of that button will be generated to the Windows system E.g. if the user further clicks, then another event of that button will be generated (actually it is the click event) For any event generated, the system will first check if there is an event handler, which defines the action for that event For a GUI designer, he needs to develop the event handler to determine the action that he wants Windows to take for that event. Writing C++ programs

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 18 Any event? Is there an event handler for that event? Run event handler Yes No Yes No The part that a GUI designer needs to develop

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 19 Step 6 – Add Event Handler Double-click the OK button such that the default event handler button1_Click will be introduced in Form1.h Add the following codes to the function button1_Click() System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ message = "Hello "; // ^ specifies a tracking handle String^ title = "Welcome"; // String is a managed class MessageBox::Show(message,title, MessageBoxButtons::OK); } // A tracking handle has similarities to a native C++ pointer When the OK button is clicked, a message box will be invoked to show a message.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 20 – The symbol “ ^ ” identifies a tracking handle of class String – A tracking handle is similar to a native C++ pointer which stores an address, but the address it contains is automatically updated if the object it references is moved – No address arithmetic or casting is allowed for tracking handles – Tracking handle is for referencing objects created in CLR heap, including objects that are of reference class type (e.g. the String class) String^ message = "Hello ";

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 21 MessageBox::Show(message,title, MessageBoxButtons::OK); Show() is a static member function of the MessageBox Class. We can call any member function of a class without instantiating an object of that class message is the message to be displayed in the message box. It is of the managed class String^ title is the title of the message box. It is of the managed type String^ OK is a static member enumeration of the class MessageBoxButtons. If OK is used, only the OK button will be shown. There can be other buttons, such as, YesNo, OKCancel, AbortRetryIgnore, etc. Use Help of Visual C to learn MessageBox and MessageBoxButtons

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 22 Step 7 – Build and run the program

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 23 Managed data types - Common Type System (CTS) All GUI inputs are of managed type, data received from these GUIs may be different from the unmanaged version For example, the string received from the textbox is of managed type It needs conversion if we want to use it with unmanaged codes char abc[] = textBox1->Text; unmanaged type Text is the input made by user in textBox1. It is of managed type

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 24 Conversion between managed type and unmanaged type: String^  char * using namespace System::Runtime::InteropServices; // must add #include // Due to strcat private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { char message[100] = "Hello "; //Unmanaged type String ^ tbstr = textBox1->Text; char *name = (char*)Marshal::StringToHGlobalAnsi(tbstr).ToPointer(); strcat(message,name); //Combine "Hello " with name String^ Mmge=Marshal::PtrToStringAnsi((IntPtr)message); String^ title = "Welcome"; MessageBox::Show(Mmge,title,MessageBoxButtons::OK); Marshal::FreeHGlobal((IntPtr)name); // just like delete }

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 25 using namespace System::Runtime::InteropServices; The class name such as Marshal is defined under the System::Runtime::InteropServices namespace The above statement needs to be added at the beginning of Form1.h with the other namespace definitions. i.e. the full name of the class is System::Runtime::InteropServices::Marshal String ^ tbstr = textBox1->Text; For C++/CLI, any string should be defined as a handle to an object of the System::String class Here, textBox1->Text is a managed string. That’s why it should be assigned to the handle tbstr as above.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 26 char *name = (char*)Marshal:: StringToHGlobalAnsi(tbstr).ToPointer(); : Marshal::FreeHGlobal((IntPtr)name); To convert a managed string to an unmanaged string, we need to make use of the function StringToHGlobalAnsi(), which is a static member function of the class Marshal The function will return an object of class IntPtr We need to call the member function ToPointer() of IntPtr to convert the object to a pointer, but we haven’t specified what kind of pointer it is Finally, casting the pointer to a character pointer As some memory is used in the unmanaged heap (pointed by name ), we should free it using the function FreeHGlobal(). will return an object of type IntPtr HGlobal = Global heap

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 27 – strcat() combines the string "Hello " in message with the string the user entered into the textbox, and stores it in message – Marshal::PtrToStringAnsi((IntPtr)message) allocates a managed String and copies the unmanaged ANSI string message into it. strcat(message,name); String^ Mmge=Marshal::PtrToStringAnsi((IntPtr)message);

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 28 Conversion between managed type and unmanaged type: String^  char * – Notice that both native C++ and managed C++ codes can be present in the same C++/CLI program However, the heap for the native C++ variables is the unmanaged heap, which need to be freed after use. In the above, the memory pointed by name has to be freed. The heap for the managed C++ variables is the CLR heap, which is also termed garbage-collected heap. We do NOT need to worry about the management and clean-up of this heap. To allocate memory in the CLR heap, use the operator gcnew – The above example is for illustrating how to convert between managed and unmanaged types A simpler pure managed C++ program can do the same job

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 29 Step 8 – Receive TextBox Input Modify the function button1_Click() as follows: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { String ^ message = "Hello "; String ^ title = "Welcome"; Char a='!'; //A managed character MessageBox::Show(message + textBox1->Text + a, title, MessageBoxButtons::OK); } // A text or string can easily be joined to another string by direct "adding", if they are of managed type - String^, Char, etc. Char in fact is the System::Char base class for CLR, it is equivalent to __wchar_t in C++/CLI

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 30 Enter a name into textBox1 Step 9 – Build and run the program

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 31 Conversion between managed types: int  String^ System::Void button1_Click(System::Object * sender, System::EventArgs * e) { String^ title = "Welcome"; String^ message = "The number is "; int a=12; String^ no =""+a;//int converted to string automatically MessageBox::Show(message+no,title,MessageBoxButtons::OK); } String^ no; no=no+a; We can replace the above by:

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 32 Modify the form as shown below. When a user enters 2 numbers and click Add, a message box will be shown to display the sum of the 2 numbers. Exercise 9.1 Don't forget to change the title of the form

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 33 Hint 1 : To convert an unmanaged string to an integer, you may include the library and use int atoi (char *abc) // Return an integer converted, if // possible, from the string abc Hint 2: If unmanaged type is NOT used, you may consider using the managed member function of the Convert class: int System::Convert::ToInt32(String) Exercise 9.1 (cont)