VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 1B Introduction (Tutorial)
Tutorial: Hello World! Visual C++ Programming 2 Problem Analysis When the user clicks the button “Hello World!” appears in the textbox. Design This program has an interface containing One button One textbox
Interface Design Sketch Visual C++ Programming 3
Development and Preliminary Testing Visual C++ Programming 4 Create a new Visual C++ project called “Hello World” Set up the IDE so that these tabbed windows are available Solution Explorer Toolbox Properties window
Creating a New Project Visual C++ Programming 5
Naming and Saving the Project Visual C++ Programming 6
Setting up the IDE Visual C++ Programming 7
Development and Preliminary Testing (continued) Visual C++ Programming 8 Use the Toolbox to locate controls and place them on the form Use the Properties window to change the attributes of an object Run the program by clicking the “Start Debugging” button
Opening the Toolbox Visual C++ Programming 9
Select the Properties Window Visual C++ Programming 10
Toolbox and Properties Window Visual C++ Programming 11
Repositioning the Properties Window Visual C++ Programming 12
Positioning the Solution Explorer Visual C++ Programming 13
Creating textBox1 Visual C++ Programming 14
Creating button1 Visual C++ Programming 15
Using the Properties Window Visual C++ Programming 16
Changing the Text Property of button1 Visual C++ Programming 17
Changing the Text Property of Form1 Visual C++ Programming 18
Running Your Program Visual C++ Programming 19
Getting Rid of an Unnecessary Dialog Box Visual C++ Programming 20
Your Running Program Visual C++ Programming 21
Development and Preliminary Testing (continued) Visual C++ Programming 22 To create a click event handler double click on a button. The Code Editor window opens and the cursor is positioned within the event handler When you have finished entering your C++ code run the program and test it
The Code Window Visual C++ Programming 23
The button1_Click() Event Handler Visual C++ Programming 24
Example 1-1 Visual C++ Programming 25
The Statement Completion Dropdown Box Visual C++ Programming 26
Completed button1_Click() Event Handler Code Visual C++ Programming 27
Understanding the Code Visual C++ Programming 28 Windows Forms Designer code Created automatically Defines the Form1 class definition Do not modify it or your program may not work Keywords Keywords have a special meaning in Visual C++ and can only be used in their designated context Keywords are automatically colored blue Example: this->BackColor See Appendix B for a list of C++ keywords
Understanding the Code (Continued) Visual C++ Programming 29 Control names and properties Attributes and methods belonging to a control object are referred to using the operator (->) Example: textBox1->Text Strings and delimiters A string of characters is designated using a pair of quotation marks (double quotes) as delimiters Example: “Hello World!” Strings are automatically colored maroon
Example 1-2 Visual C++ Programming 30
Example 1-3 Visual C++ Programming 31
Understanding the Code (Continued) Visual C++ Programming 32 The assignment operator (=) The value on the right side of the = operator is assigned to the location on the left Example: textBox1->Text = “Hello World!”; In this example, the string “Hello World!” is assigned to the Text property of textBox1 Semicolons Almost every C++ statement is followed by a semicolon (;) Forgetting to put in a semicolon will prevent your program from compiling
Assignment Statement Visual C++ Programming 33
Testing the Completed Program Visual C++ Programming 34 Your program should Have one button and one textbox on the form Display “Hello World!” in the textbox when the button is clicked
Hello World Program Visual C++ Programming 35
Debugging a Program Visual C++ Programming 36 Remove the semicolon (;) from the end of the line you entered earlier Now run your program and look at the error messages displaying in the Output window Find the error number and description Find the line at which the error was detected by the compiler Fix the error and run your program again
Program Error Dialog Box Visual C++ Programming 37
Locating an Error Visual C++ Programming 38
Locating an Error Line Visual C++ Programming 39
On Your Own Visual C++ Programming 40 Change the textbox font Add more buttons Create click event handlers for the new buttons Change the Text properties of each button Change the textbox foreground and background colors Change the background color of the form
Font Property in Properties Window Visual C++ Programming 41
Font Dialog Box Visual C++ Programming 42
Colored Text in textBox1 Visual C++ Programming 43
Color Values Visual C++ Programming 44
Completed Project Visual C++ Programming 45