SlideShare a Scribd company logo
1 of 5
Download to read offline
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 ===

More Related Content

Viewers also liked

Quarti di coppa italia new real volley gioia 3 1 diper mand'or jolly cinq...
Quarti di coppa italia     new real volley gioia 3 1 diper mand'or jolly cinq...Quarti di coppa italia     new real volley gioia 3 1 diper mand'or jolly cinq...
Quarti di coppa italia new real volley gioia 3 1 diper mand'or jolly cinq...redazione gioianet
 
Sheetmetal 150503052320-conversion-gate02
Sheetmetal 150503052320-conversion-gate02Sheetmetal 150503052320-conversion-gate02
Sheetmetal 150503052320-conversion-gate02divyesh bhanderi
 
Проектирование классификаторов технико экономической информации
Проектирование классификаторов технико экономической информацииПроектирование классификаторов технико экономической информации
Проектирование классификаторов технико экономической информацииadam93
 
16° giornata volley locorotondo castellana 0 3 new real volley gioia
16° giornata    volley locorotondo castellana 0 3 new real volley gioia16° giornata    volley locorotondo castellana 0 3 new real volley gioia
16° giornata volley locorotondo castellana 0 3 new real volley gioiaredazione gioianet
 

Viewers also liked (8)

Club de vie
Club de vieClub de vie
Club de vie
 
Prova 2
Prova 2Prova 2
Prova 2
 
Quarti di coppa italia new real volley gioia 3 1 diper mand'or jolly cinq...
Quarti di coppa italia     new real volley gioia 3 1 diper mand'or jolly cinq...Quarti di coppa italia     new real volley gioia 3 1 diper mand'or jolly cinq...
Quarti di coppa italia new real volley gioia 3 1 diper mand'or jolly cinq...
 
Sheetmetal 150503052320-conversion-gate02
Sheetmetal 150503052320-conversion-gate02Sheetmetal 150503052320-conversion-gate02
Sheetmetal 150503052320-conversion-gate02
 
Проектирование классификаторов технико экономической информации
Проектирование классификаторов технико экономической информацииПроектирование классификаторов технико экономической информации
Проектирование классификаторов технико экономической информации
 
Invito concerto 16 marzo 1
Invito concerto 16 marzo 1Invito concerto 16 marzo 1
Invito concerto 16 marzo 1
 
16° giornata volley locorotondo castellana 0 3 new real volley gioia
16° giornata    volley locorotondo castellana 0 3 new real volley gioia16° giornata    volley locorotondo castellana 0 3 new real volley gioia
16° giornata volley locorotondo castellana 0 3 new real volley gioia
 
Normas en ava
Normas en avaNormas en ava
Normas en ava
 

Similar to Notes multiple form interfaces

Software engineering modeling lab lectures
Software engineering modeling lab lecturesSoftware engineering modeling lab lectures
Software engineering modeling lab lecturesmarwaeng
 
C# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesC# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesSami Mut
 
PresentationCrystaljoiuouoouououoooiuiouiououoioiuoiuouo
PresentationCrystaljoiuouoouououoooiuiouiououoioiuoiuouoPresentationCrystaljoiuouoouououoooiuiouiououoioiuoiuouo
PresentationCrystaljoiuouoouououoooiuiouiououoioiuoiuouohrhedgewarhospital
 
Visual basics Express Project
Visual basics Express ProjectVisual basics Express Project
Visual basics Express ProjectIftikhar Ahmed
 
How to transfer activity codes from one programme to another
How to transfer activity codes from one programme to anotherHow to transfer activity codes from one programme to another
How to transfer activity codes from one programme to anotherAmr Morsy
 
How to create your own Project on Comidor
How to create your own Project on ComidorHow to create your own Project on Comidor
How to create your own Project on ComidorComidor
 
Devry proj 586 week 4 update microsoft project new
Devry proj 586 week 4 update microsoft project newDevry proj 586 week 4 update microsoft project new
Devry proj 586 week 4 update microsoft project newuopassignment
 
How to create document approval workflows?
How to create document approval workflows?How to create document approval workflows?
How to create document approval workflows?Revv
 
BBA I Semester IT All assignments
BBA I Semester IT All assignmentsBBA I Semester IT All assignments
BBA I Semester IT All assignmentsRoshan Kumar
 
SLMQ1Senior HS EmpTechModule 4.2 Spreadsheet
SLMQ1Senior HS EmpTechModule 4.2 SpreadsheetSLMQ1Senior HS EmpTechModule 4.2 Spreadsheet
SLMQ1Senior HS EmpTechModule 4.2 SpreadsheetNicoleBitgue
 
PT1420 Modules in Flowchart and Visual Basic .docx
PT1420 Modules in Flowchart and Visual Basic             .docxPT1420 Modules in Flowchart and Visual Basic             .docx
PT1420 Modules in Flowchart and Visual Basic .docxamrit47
 
COSC 1436 Project Page 1 Pre Project Example Secti.docx
COSC 1436 Project Page 1  Pre Project Example Secti.docxCOSC 1436 Project Page 1  Pre Project Example Secti.docx
COSC 1436 Project Page 1 Pre Project Example Secti.docxfaithxdunce63732
 
CIS 170 Become Exceptional--cis170.com
CIS 170 Become Exceptional--cis170.comCIS 170 Become Exceptional--cis170.com
CIS 170 Become Exceptional--cis170.comclaric131
 
FDMEE script examples
FDMEE script examplesFDMEE script examples
FDMEE script examplesAmit Sharma
 

Similar to Notes multiple form interfaces (20)

Software engineering modeling lab lectures
Software engineering modeling lab lecturesSoftware engineering modeling lab lectures
Software engineering modeling lab lectures
 
C# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesC# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slides
 
PresentationCrystalReport.ppt
PresentationCrystalReport.pptPresentationCrystalReport.ppt
PresentationCrystalReport.ppt
 
PresentationCrystaljoiuouoouououoooiuiouiououoioiuoiuouo
PresentationCrystaljoiuouoouououoooiuiouiououoioiuoiuouoPresentationCrystaljoiuouoouououoooiuiouiououoioiuoiuouo
PresentationCrystaljoiuouoouououoooiuiouiououoioiuoiuouo
 
Visual basics Express Project
Visual basics Express ProjectVisual basics Express Project
Visual basics Express Project
 
How to transfer activity codes from one programme to another
How to transfer activity codes from one programme to anotherHow to transfer activity codes from one programme to another
How to transfer activity codes from one programme to another
 
How to create your own Project on Comidor
How to create your own Project on ComidorHow to create your own Project on Comidor
How to create your own Project on Comidor
 
Intake 37 9
Intake 37 9Intake 37 9
Intake 37 9
 
Visual C# 2010
Visual C# 2010Visual C# 2010
Visual C# 2010
 
Devry proj 586 week 4 update microsoft project new
Devry proj 586 week 4 update microsoft project newDevry proj 586 week 4 update microsoft project new
Devry proj 586 week 4 update microsoft project new
 
Introduction
IntroductionIntroduction
Introduction
 
Intake 38 9
Intake 38 9Intake 38 9
Intake 38 9
 
How to create document approval workflows?
How to create document approval workflows?How to create document approval workflows?
How to create document approval workflows?
 
BBA I Semester IT All assignments
BBA I Semester IT All assignmentsBBA I Semester IT All assignments
BBA I Semester IT All assignments
 
SLMQ1Senior HS EmpTechModule 4.2 Spreadsheet
SLMQ1Senior HS EmpTechModule 4.2 SpreadsheetSLMQ1Senior HS EmpTechModule 4.2 Spreadsheet
SLMQ1Senior HS EmpTechModule 4.2 Spreadsheet
 
PT1420 Modules in Flowchart and Visual Basic .docx
PT1420 Modules in Flowchart and Visual Basic             .docxPT1420 Modules in Flowchart and Visual Basic             .docx
PT1420 Modules in Flowchart and Visual Basic .docx
 
COSC 1436 Project Page 1 Pre Project Example Secti.docx
COSC 1436 Project Page 1  Pre Project Example Secti.docxCOSC 1436 Project Page 1  Pre Project Example Secti.docx
COSC 1436 Project Page 1 Pre Project Example Secti.docx
 
enhancements.ppt
enhancements.pptenhancements.ppt
enhancements.ppt
 
CIS 170 Become Exceptional--cis170.com
CIS 170 Become Exceptional--cis170.comCIS 170 Become Exceptional--cis170.com
CIS 170 Become Exceptional--cis170.com
 
FDMEE script examples
FDMEE script examplesFDMEE script examples
FDMEE script examples
 

Recently uploaded

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutionsmonugehlot87
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 

Recently uploaded (20)

Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutions
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 

Notes multiple form interfaces

  • 1. 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
  • 2. 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.
  • 3. 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.
  • 4. 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.
  • 5. 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 ===