430 likes | 446 Views
Step-by-step guide to creating a Windows application, including renaming controls, setting properties, handling events, and writing code in Visual Basic 2005.
E N D
Visual Basic 2005Hello World Fall 2005 T. Blum
Start/All Programs/Microsoft Visual Studio 2005 Beta 2/Microsoft Visual Studio 2005 Beta 2
First time: select option you are most likely to use and click Start
Click on Project next to Create in the Recent Project panel or go to File/New Project
Choose Windows Application as the Template and give the project a name (e.g. MyHelloWorld). Click OK
With the “form” highlighted, go to the Properties Window and change the (Name) property (e.g. to “FrmHelloWorld”) Properties Window: If you don’t have the Properties Window showing, you can get it by going to View/ Properties Window on the menu bar
Right click on the Form file shown in the Solution Explorer and select Rename from the Context sensitive menu
Enter a name for the file (e.g. FrmHelloWorld.vb) containing the file containing your Visual Basic code
Save Project Dialog box: Name project and solution and choose location for them (clicking Browse opens the Project Location dialog box)
Click Browse, then use the Project Location dialog box to choose a place for the project. Then click Open.
Place the mouse over the Toolbox tab, the toolbox emerges. To hold it in place click on the tack icon.
Click on the Button button and then when the mouse is moved over the form it turns into a cross hairs icon. Drag from the upper left to lower right hand corner of where you would like the button placed.
The Button can be moved by using the clicking on Pointer and then placing the mouse in the center (obtaining the four-arrow icon) and dragging.
Resizing • To change the size move the mouse to the edge of the button to obtain the two headed icon and then drag. • One can also change the Location and Size by editing the appropriate settings in the Properties Window.
I prefer my properties to be alphabetized, so I clicked the Alphabetical button. You can go back to categorized by clicking that button.
Naming Conventions • The items placed on the form are known as “controls.” • You should rename any control that you are going to have appear in your code. • It is traditional to give VB control names a prefix indicating the type of control (in this case “btn” for button). • The rest of the name should describe the purpose of the control and typically starts with a capital letter (in this case “Message” because clicking the button causes a message to be displayed).
Change the BackColor property of the button by clicking on the drop down list, selecting the Web tab and then choosing a color from the list.
Click in the region next to Font and then on the ellipsis button that appears.
Change the Visible property to False. You can still see the label in “design” but the user will not see it at run time.
The form during run time does not display the “Hello World” message. Click the Close (X) button to stop running the program.
Our goal • Our goal is to have the message “Hello World” display when the user clicks the button. • The user’s clicking of the button is said to raise an event – the click event. • We have to write code that handles that event (instructs our program what to do when the event is raised – in this case, make the message visible).
Double clicking on the button takes over to Code View and creates the boiler plate for our method that will handle the button’s click event.
Note some of the key words: Public, Class, Private, Sub, ByVal The name of the “class” is the name we gave our form. The name of the subroutine is btnMessage_Click derived from the control and its event.
Rest of it While the name of the subroutine was derived from the control and its event, that is only a name (and “A rose by any other name …”) but the code shown above is what really associates the action of clicking the button with the execution of the code in this subroutine.
Between Private Sub … and End Sub lines type the code. After typing the code name of the label and a dot, IntelliSense provides a dropdown list of label properties and events.
Type Visible or find it on the drop down list, then type “=,” after that IntelliSense provides another drop down list.
Line of code and a comment (anything after an apostrophe). Comments do not affect execution of code. They help explain code.
Comments are also used to take credit for code. Always have a comment with your name at the top of every program submitted for credit.