notes multiple form interfaces
TRANSCRIPT
Development Software 2 – DOS200S
Compiled By WH. Olivier ©2013 Cape Peninsula University of Technology
Window Form Applications Using Visual C++
Multiple Form Interfaces
Objectives and Learning Areas:
Introduction Sometimes, when designing an application, you would want to include more than one Form in your application. You
would also want to pass values between these forms. Understanding how this is accomplished in Visual C++ is very
important. Here we will be looking at adding methods (functions) and include parameters. The methods and their
parameters will be like setting up a channel between two forms where by data can flow from one Form to another.
Let us first see how this looks like when a application with this capability executes:
We are looking at a simple application where by the user enters a sales amount (Sales Made) and it calculates a
commission amount based on some criteria.
1. The user enters a Sales Made amount.
2. When the user clicks the ‘Calculate Commission’ button, an event is initiated.
3. The commission is calculated using the Sales amount and storing it in a variable which is passed to
the next Form.
4. When the second Form loads, it displays the commission result in a label on the Form.
In this lesson you will be taught the following:
1. How to add more than one form to your Application Project
2. How to call a form from another form
3. What is Modal and Modeless
4. How to pass values from Form1 to Form2
5. How to return values from Form2 to Form1
Development Software 2 – DOS200S
Compiled By WH. Olivier ©2013 Cape Peninsula University of Technology
Add a second form For this illustration; let’s assume that we have created our application with the initial Form1, and we adding a
second Form as shown below.
1. Select ‘Add New Item’ to add a new Form to your project
2. From the ‘Add New Item’ dialog box, make sure that ‘Visual C++’ is selected under Install
Templates’ and then select ‘Windows Form’.
3. In the ‘Name’ textbox, enter the name for the Form. You must give it a meaningful name to
describe what the form is about. For this illustration we will only call it ‘Form2’.
4. Press the ‘Add’ button to complete the ‘Add New Item’ process.
5. A new Blank Form will appear. You can now add the required controls to the Form.
Development Software 2 – DOS200S
Compiled By WH. Olivier ©2013 Cape Peninsula University of Technology
Adding code to call the 2nd Form Now that the second Form has been added, you will have to add the code to call the Form2 from within the Form1.
You will therefore have to include the header file (Form2.h) which contains that definition code (the Form2 class
definition) into Form1 as shown below:
1. In the Form1 design, Right-Click on the Form and select ‘View Code’ and go to the top of the code.3
2. Add an include statement just below ‘#pragma once’ line as shown above.
3. Select the Form1 design again and double-click on the ‘Calculate’ button to add code to the
button1_Click method.
4. Add the code to create an instance of Form2 and call Form2 from within Form1.
Development Software 2 – DOS200S
Compiled By WH. Olivier ©2013 Cape Peninsula University of Technology
Adding code to Form2 to receive the value
1. In the design view of Form2, Right-Click on the Form and select ‘View Code’
2. Add a method just below the Form2() constructor. This method will have a value parameter, which
will allow the program to pass a value from the calling Form to the called Form. The will be as
creating a channel by which the value will be passed from one Form to another.
3. While still in the code editor, scroll down to the ‘Required designer variable’ section and add the
global variable which will receive the value as shown in the newly added method.
4. Go back to the design view of Form2 and double click the Form to add code to the Form_Load
method.
5. Add the code which will convert the double variable to a String and assign it to the Text property of
the label2 on the Form.
Development Software 2 – DOS200S
Compiled By WH. Olivier ©2013 Cape Peninsula University of Technology
Finalize the code in Form1
To finalize this process:
1. Go back to the design view of Form1 and double-click on the ‘Calculate’ button
2. In the code editor – in the button1_click method, complete the code. The program must convert
the textBox values to doubles and assign them to the appropriate variables declared. The
calculated value will then be passed as a parameter using the method call to pass to value to
Form2.
Receiving values from Form2 and send it back to Form1. Returning a value back to the calling Form is just the reverse of sending it. Here we need to add an additional
method in Form2. The parameters (if the value is returned through the parameter list) must reference parameters.
The method to return the values will be called after the ‘ShowDialog’ method inside Form1.
=== THE END ===