This lesson will walk you through building your first Visual Basic 6 (VB6) program.
- Start a .exe project.
- Place a CommandButton on the form from ToolBox.
- Click on Form and edit the caption property of the form. Write “My First VB Program”.
- Edit the caption property of the CommandButton from the Properties Window. Write “Print”.
- Double-Click the CommandButton to open the Code Window. Write the following code.
QuickHint:Write code between the two lines ‘Private Sub Command1_Click()’ and ‘End Sub’. These two lines will be created automatically after double-clicking the CommandButton.
Explanation
‘Sub’ means Sub-Routine or Sub-Procedure or Function. ‘Command1_Click()’ is the sub-procedure name where ‘Command1’ is a control and ‘Click’ is an event. ‘( )’ this sign indicates that ‘Command1_Click()’ is a function. ‘Private’ is the Scope. You will learn about scope in the next chapters. ‘Print’ is a Method.
You could also write Form1.print, where Form1 is an object and print is its method. ‘End Sub’ indicates the end of the sub-procedure. You will later learn about sub-procedures.
Now you are done !
Press F5 to run the program or click the start icon button from the toolbar. The code will be executed and the string “Welcome to www.fortypoundhead.com” will be displayed on the form as shown in the picture below.
Writing comments
Comments are the lines of text that are not executed but used for the advantage of the programmers. Comments are written so that the other programmers can easily understand your program and you can better understand the code in case that it becomes complex or large. When you write comments, it becomes easy to maintain the code of your application. Commenting is a part of Documentation and it is a good practice to write comments.
How to write comments ?
Comments are written using the apostrophe ( ‘ ). That means if you write anything after apostrophe that becomes your comment and text color of the comments becomes green. See the picture below.