Department of Information Engineering INFORMATION TECHNOLOGY l C

  • Slides: 14
Download presentation
Department of Information Engineering INFORMATION TECHNOLOGY l C++ programming in Windows environment Visual components,

Department of Information Engineering INFORMATION TECHNOLOGY l C++ programming in Windows environment Visual components, menus, controls l Exposition of task l Completing the menu l Writing program code for Exit menu item l Creating controls l Writing further event handlers dr. László Dudás 34. /0.

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /1. Task: Let us

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /1. Task: Let us make a C++ Builder program for conversion of a byte given in binary form to decimal and back. The program has a help in English and Hungarian that gives the minimal information needed for usage. A previous idea of program window is the next:

Department of Information Engineering INFORMATION TECHNOLOGY Completing the menu 1. Place a Main. Menu

Department of Information Engineering INFORMATION TECHNOLOGY Completing the menu 1. Place a Main. Menu icon 2. Double click on the menu editor: on the form! component to display 3. Give the name and caption for the selected main menu item in the Object Inspector: 4. Give the name and caption for the submenu items of Selection main menu item: dr. László Dudás 34. /2.

Department of Information Engineering INFORMATION TECHNOLOGY 5. Select the second item of main menu

Department of Information Engineering INFORMATION TECHNOLOGY 5. Select the second item of main menu and name it to Help in the Object Inspector: 6. To create the submenu in Help select the empty submenu item and name it to Description in the Object Inspector: dr. László Dudás 34. /3.

Department of Information Engineering INFORMATION TECHNOLOGY 7. For creation of submenu of Description mark

Department of Information Engineering INFORMATION TECHNOLOGY 7. For creation of submenu of Description mark the Description and push the right mouse button! A popup menu appears, choose the Create Submenu item: 8. After selecting the originating submenu item give English name to it in the Object Inspector, and Hungarian to the empty menu item below it! 9. Having accomplished these steps the menu is ready. dr. László Dudás 34. /4.

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /5. Writing program code

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /5. Writing program code for Exit menu item 10. Double click on the Exit menu item of the menu editor! The code editor comes to the top and the cursor flashes in the newly created event handler function skeleton of the Exit-click. 11. Write the code of method call for exiting: The program can be run and the Exit menu item can be tried. 12. Place on the form the necessary other components: one radio group, eight check boxes, seven buttons and two labels for displaying the result!

Department of Information Engineering INFORMATION TECHNOLOGY Creating controls 13. The raw form of the

Department of Information Engineering INFORMATION TECHNOLOGY Creating controls 13. The raw form of the user interface is the next: 14. For creation of two radio buttons in the radio group select the radio group component then choose the Items property in the Object Inspector. A double click on it will bring up the String List editor window. Give the caption strings for the two radio buttons: dr. László Dudás 34. /6.

Department of Information Engineering INFORMATION TECHNOLOGY 15. Give the Caption of radio group: Direction

Department of Information Engineering INFORMATION TECHNOLOGY 15. Give the Caption of radio group: Direction of conversion 16. Give the captions for check boxes, buttons and labels: 17. Choose a bigger font size for displaying the decimal value (Label 1 ->Font and Label 2 ->Font = 10, Bold ): dr. László Dudás 34. /7.

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /8. Writing further event

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /8. Writing further event handlers 18. Create a variable for storing the decimal value and set it to zero! For this insert the next line into the Unit 1. cpp file after the TForm 1 * Form 1; line: int decimal = 0; 19. Write the program code for decreasing and increasing buttons! For these double click on, e. g. Button 1 and decrease the value of decimal with the value represented by the button! Avoid decreasing under zero! Similarly avoid increasing above 255! Display the changed value as the new value of Label 2. E. g. : the appropriate event handler function for Button 1: void __fastcall TForm 1: : Button 1 Click(TObject *Sender) { decimal -= 1; if (decimal < 0) decimal = 0; Label 2 ->Caption = decimal; } Compile and run the program! The decreasing and increasing work!

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /9. 20. We have

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /9. 20. We have arrived at the most interesting part: The event handler of Conversion button has to be accomplished. This button will perform the conversion from binary to decimal, or in opposite direction, depending on the status of radio buttons. The binary input value is given by the status of check boxes. Every check box corresponds to one of the bits of a byte, if it is checked then the value of the bit is 1, else 0. The solution: void __fastcall TForm 1: : Button 7 Click(TObject *Sender) { if (Radio. Group 1 ->Item. Index==0) //Bin-> Dec {decimal = 0; if (Check. Box 1 ->Checked) decimal += 1; if (Check. Box 2 ->Checked) decimal += 2; if (Check. Box 3 ->Checked) decimal += 4; if (Check. Box 4 ->Checked) decimal += 8; if (Check. Box 5 ->Checked) decimal += 16; if (Check. Box 6 ->Checked) decimal += 32; if (Check. Box 7 ->Checked) decimal += 64; if (Check. Box 8 ->Checked) decimal += 128; Label 2 ->Caption= decimal; } //continued

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /10. else //Dec ->

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /10. else //Dec -> Bin {int buff= decimal; if (buff >= 128) {Check. Box 8 ->Checked=true; buff -= 128; } else Check. Box 8 ->Checked = false; if (buff >= 64) {Check. Box 7 ->Checked=true; buff -= 64; } else Check. Box 7 ->Checked = false; if (buff >= 32) {Check. Box 6 ->Checked=true; buff -= 32; } else Check. Box 6 ->Checked = false; if (buff >= 16) {Check. Box 5 ->Checked=true; buff -= 16; } else Check. Box 5 ->Checked = false; if (buff >= 8) {Check. Box 4 ->Checked=true; buff -= 8; } else Check. Box 4 ->Checked = false; if (buff >= 4) {Check. Box 3 ->Checked=true; buff -= 4; } else Check. Box 3 ->Checked = false; if (buff >= 2) {Check. Box 2 ->Checked=true; buff -= 2; } else Check. Box 2 ->Checked = false; if (buff >= 1) {Check. Box 1 ->Checked=true; buff -= 1; } else Check. Box 1 ->Checked = false; } }

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /11. 21. The program

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /11. 21. The program is not yet perfect because there is not any radio button selected! Do select one in the constructor of the Form! __fastcall TForm 1: : TForm 1(TComponent* Owner) : TForm(Owner) { Radio. Group 1 ->Item. Index = 0; } 22. The Calculation does not work from the menu items. Selecting this menu point accomplishing the same task is required than is made with the Conversion button. How can be this achieved? There is a possibility to cut out the body of the Conversion event handler and insert into an individual function and then the Calculation menu item and the Conversion button could call this function. But this is unnecessary because the Calculation call the event handler of the Conversion as shown in the next: void __fastcall TForm 1: : Calculation. Click(TObject *Sender) { Button 7 Click(Application); }

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /12. 23. The event

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /12. 23. The event handlers of Help submenu items are missing yet. The English and Hungarian description of the work of program can be shown in two similar message windows: void __fastcall TForm 1: : English. Click(TObject *Sender) { Show. Message("The program makes conversions between binary and decimal numbers. Check Boxes represent one of the bits of a byte. The Conversion can be started choosing the Selection/Calculation menu item or clicking on the Conversion button. "); } The giving of Hungarian help can be made likewise.

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /13. 24. At last

Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34. /13. 24. At last it may be carried out some refinement actions: The window of the application may have own title overwriting the Form 1 ->Caption property. The icon of Borland can be changed to an icon designed by us. The components having automatically generated names can be renamed to more pleasant names, and other refinements can be made, e. g. it can be checked if the program can work without mouse, with keyboard only. It could be experienced that it can.