Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 1 9.3 Creating a Multiple-Form Interface.

Slides:



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

ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
Chapter 13: Advanced GUI and Graphics
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
DT265-2 Object Oriented Software Development 2 Lecture 3 : Windows Programming Lecturer Pat Browne
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
1 Graphical User Interfaces Part 2 Outline Multiple Document Interface (MDI) Windows Visual Inheritance User-Defined Controls.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface A Brief Introduction to GDI+ S.R.G. Fraser, Pro Visual C++/CLI.
Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Working with Unmanaged Code.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Chapter 8: Writing Graphical User Interfaces
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 4 I Need a Tour Guide.
Dr Dat Tran - Week 1 Lecture Notes 1 Windows Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 - Graphical User Interface Concepts: Part.
BIL528 – Bilgisayar Programlama II Introduction 1.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
1 Chapter Nine Using GUI Objects and the Visual Studio IDE.
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 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 Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 Advanced Computer Programming Lab Calculator Project.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Objects and Classes Continued Engineering 1D04, Teaching Session 10.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Objects and Classes Engineering 1D04, Teaching Session 9.
BIL528 – Bilgisayar Programlama II Methods 1. Contents Methods 2.
Object-Oriented Programming: Inheritance and Polymorphism.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Object-Oriented Application Development Using VB.NET 1 Chapter 11 Using Multiple Forms with Problem Domain Classes.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
Computer Science Up Down Controls, Decisions and Random Numbers.
Dialog Boxes Getting information from user. Types of Windows Five types we’ll be using 1. Main Application Window 2. Child Windows 3. Dialog Boxes 4.
CompSci 230 Software Construction
Visual programming Chapter 3: GUI (Graphical User Interface) Part I
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Developing a Simple Graphical User Interface (GUI)
Multi-form applications and dialogs
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
Lecture Set 11 Creating and Using Classes
Tonga Institute of Higher Education
Object-Oriented Programming: Inheritance and Polymorphism
Constructors, GUI’s(Using Swing) and ActionListner
Group Boxes, Radio buttons and Checked List Boxes
GUI Programming in Visual Studio .NET
Presentation transcript:

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 2 Creating a Multiple-Form Interface It is common that an application may have more than one form In addition, one form may be invoked by another form To add one more form to a project, we need to follow the steps below: 1.Add one more form class to your project 2.Design the form layout 3.Set the properties of the GUIs in the new form 4.Update the program code of the original form. Each form has its own GUI

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 3 Example When the button Show Card is clicked, Form2 will be shown Radio button

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 4 Step 1: Specification of the program 1.A user is asked to select one and only one of the four cards 2.If the user does not check any of the radio button, no card will be shown 3.If the user clicks the Show Card button, another form ( Form2 ) will be shown with the selected card 4.The user cannot access the original form ( Form1 ) until he clicks the OK button of Form2. Form2 is known as the modal form. 5.If the user clicks on the picture, the card will toggle between face-up and face-down.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 5 Step 2: Develop Form1 groupBox All controls inside a group box will be grouped together groupBox All controls inside a group box will be grouped together radioButton For the radio buttons inside a group box, only one of them can be selected radioButton For the radio buttons inside a group box, only one of them can be selected Project name: Chap9p5 Make sure the TabStop property of the RadioButtons are False

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 6 Step 3: Develop Form2 In the Solution Explorer, right click your project name and select Add  New Item... Select Windows Form. Give a name, say Form2, to your new form ( Form2 will also be the class name of the form) You will see another set of files added to your project, e.g. Form2.cpp, Form2.h.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 7 pictureBox pictureBox allows you to put a picture into it. The picture can be obtained from a file pictureBox pictureBox allows you to put a picture into it. The picture can be obtained from a file We can add the controls we wanted to here

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 8 namespace chap9p5// define the namespace of the class { public ref class Form2 : public System::Windows::Forms::Form {// The class name is Form2. public: Form2(void)// The constructor. Define the things {:// that will be done when the class }// is instantiated. TO BE WRITTEN LATER. : private: System::Windows::Forms::Button^ button1; :// The button is defined here private: System::Windows::Forms::PictureBox^ pictureBox1; :// The picture box is defined here private: void InitializeComponent(void) {:// Record the properties of the controls } }; } Form2.h is used mainly for defining the class structure of this form Base class Derived class p. 72 View  Code

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 9 Rather than writing many lines of code, we can simply define the function of a button as OK (i.e. the form will close if it is clicked) in the DialogResult item of the Properties window Returned enumerated values of a dialog box

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 10 Step 4: Implement the handler of Form1 #include "Form2.h" // Add to the beginning of Form1.h private: System::Void button1_Click(System::Object ^ sender, System::EventArgs ^ e) {// Called when button1 of Form1 is clicked int card = 0;// 0 = no card, 1 = sA, 2 = hA // 3 = cA, 4 = dA if (radioButton1->Checked) card = 1; // spade ace selected if (radioButton2->Checked) card = 2;// heart ace selected if (radioButton3->Checked) card = 3;// club ace selected if (radioButton4->Checked) card = 4;// diamond ace selected Form2 ^ F2 = gcnew Form2(card); //Form2 defined in Form2.h F2->ShowDialog(); // Show modal form, return DialogResult value } double-click Show Card

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 11 Form2 ^ F2 = gcnew Form2(card); F2->ShowDialog(); An object of Form2 is created in the gc heap pointed by F2 Use gcnew because Form2 is a reference type under Common Type System (CTS) Since we want to tell Form2 which card a user has selected, the constructor of Form2 has to be modified to accept one input parameter that indicates the selected card To show Form2, we need to call the member function ShowDialog() of the class Form2 If ShowDialog() is called, Form2 will be a modal form i.e. we cannot access other forms unless we close Form2 If Show() is called instead, Form 2 will be a modeless form, i.e. we can access other forms without closing Form2.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 12 Step 5: Implement the constructor and other functions of Form2 Form2(int c)// The constructor is modified by adding {// an input parameter InitializeComponent(); cd_no = c; // 0 = no card, 1 = sA, 2 = hA // 3 = cA, 4 = dA face = 0; // By default, the card is face up (0). // When the card is clicked, the // state will toggle from face-up to // face-down or vice versa ShowCard(); // Show card based on the info in "card" } // Two private variables are added private: int cd_no;// To keep the info of the card selected int face; // See the comment above In Form2.h p. 68

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 13 void ShowCard() { if (cd_no == 0) pictureBox1->Visible = 0; if (cd_no == 1) {pictureBox1->Visible = 1; pictureBox1->Image = Image:: FromFile("e:\\temp\\cards\\s1.gif"); } // codes for other cards follow Visible is a property of pictureBox. If it is 0, the pictureBox will not be visible. Image is another property of pictureBox. It defines the location where the picture can be found FromFile is a static member function of the Image class. It returns a file handler of an image stored at the specified location The file for Spade Ace Add a member function at the end

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 14 Visible property Image property

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 15 if (cd_no == 2) {pictureBox1->Visible = 1; pictureBox1->Image = Image::FromFile("e:\\temp\\cards\\h1.gif"); } if (cd_no == 3) {pictureBox1->Visible = 1; pictureBox1->Image = Image::FromFile("e:\\temp\\cards\\c1.gif"); } if (cd_no == 4) {pictureBox1->Visible = 1; pictureBox1->Image = Image::FromFile("e:\\temp\\cards\\d1.gif"); } \ is a special character for escape sequence. If we want to use it as just the \ character, we have to use \\ The file for Heart Ace The file for Club Ace The file for Diamond Ace

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 16 // Define the thing to be done when pictureBox1 is clicked private: System::Void pictureBox1_Click(System::Object ^ sender, System::EventArgs ^ e) { if (cd_no != 0) // A card is selected {if (face == 0) // face = 0 means face-up { pictureBox1->Image = Image:: FromFile("e:\\temp\\cards\\b1fv.gif"); // Show the card as facing down, actually a // different file is shown face = 1; // Currently face-down } // codes for face==1 follow If user clicks on the pictureBox, it will change from face-up to face-down or vice versa It is controlled by the member variable “ face ” If user clicks on the pictureBox, it will change from face-up to face-down or vice versa It is controlled by the member variable “ face ” Double-click pictureBox1

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 17 else// It is face-down { ShowCard(); face = 0; // currently face-up } Step 6: Build and run the project

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 18 Follow the above steps to build the project. Add one more button in Form1 such that if it is clicked, your program will randomly display one of the four cards, disregarding the selection of the user. Hint: We can generate a random number by using the function int rand(void), which returns a random integer between 0 to 0x7FFF. Remember to include stdlib.h before you use this function. Remark: On using rand(), the random sequence is repeatable on every execution of the program. If we don't want it to be repeatable, what should we do? (Hint: learn srand(unsigned int) ) Exercise 9.3