You are on page 1of 5

BPB2034 Programming For Business Applications

Lab 1
Step by step to write your first Visual Basic program
1. Go to: Start All Programs Microsoft Visual Studio 2012 Express VS Express
for Desktop

2. Click on File New Project Windows Forms Application.


Name: Name given to your project.
Location: Where you want to save your project related files (e.g.: source files, resources,
etc)

BPB2034 Programming For Business Applications

3. Expand the Common Controls from the Toolbox panel. Select Label control and drag and
drop on the Windows form, as shown below:

Controls
properties
Toolbox Panel

4. Next, select the Button control and drop it on the Windows form. Alternatively, you can
double click on the Button control in the Toolbox and a new button will be generated and
placed on the form. All the controls on the form will be given with default names, e.g.
Button1, Button2, Label1 and Form1 as for the example as shown above.
5. The controls are generated in default sizes. The control can be easily resized by adjusting
the handle surrounding the control you have selected, just like how you able to resize an
image in Microsoft Word.

Handle to adjust
size of the selected
control

6. Moving a control is easy too as well. Move the mouse cursor over the control you would
like to move until you see a
intended location on the form.

cursor, then click and drag and move the control to the

7. Aligning controls can be done by first performs a multiple selection by holding the SHIFT
key on the keyboard and then select the controls. Go to the menu on the top and select

BPB2034 Programming For Business Applications

FORMAT Align [Lefts / Centers / Rights / Tops / Middles / Bottoms] (Try to


explore other available options available under FORMAT menu).
8. To avoid a control from being accidentally moved/resized, you can actually lock the
control. Select the control and perform the right click to activate the Shortcut menu, then
select the Lock Controls option.

Locking a control Button1

A control Button1 is locked

9. Try to modify Text and Name properties of each control on the form. You are encouraged
to use the three-letter prefixes as practice to name the controls. What is the difference
between Text and Name properties?

Object
Form
Button
Label
List box
Text box
Object
Button
Button
Label
Form

Three-letter Prefixes
Prefix
Example
frm
frmExample
btn
btnExit
lbl
lblMessage
lst
lstNames
txt
txtPhone

Default Text & Name


Button1
Button2
Label1
Form1

Text
Show Message
E&xit
Welcome to BPB2034
First VB Program

Name
btnShowMessage
btnExit
lblMessage
frmFirstProgram

10. Double-click on the Show Message button in order to open the code editor. A sub
procedure will be automatically generated for you to handle the CLICK event. Now, it is
your task to tell the computer what to be done (programming!!!) when a user performs a
click on the Show Message button.
Private Sub btnShowMessage_Click() Handles btnShowMessage.Click
insert your code between the Sub End Sub
End Sub

BPB2034 Programming For Business Applications

In this example, you are required to show a Hello World message on the label. Remember
the name given to our Label control: lblMessage? This name will serve as an identifier to
that particular control for us to access and modify various properties of that control.
Private Sub btnShowMessage_Click() Handles btnShowMessage.Click

lblMessage.Text = "Hello World"


End Sub
Identifier

Propertys Name

Message to be displayed

11. Similarly, you can provide code for the btnExit button, which will exit the program when
the button is clicked.
Private Sub btnExit_Click() Handles btnExit.Click
Application.Exit()
End Sub
12. To debug/run your program, click: DEBUG Start Debugging (or use shortcut key F5).

13. Task 1: Add a new button into frmFirstProgram. Align and adjust the width of the newly
created button with respect to the btnShowMessage button, as shown below:

BPB2034 Programming For Business Applications

Modify the Text and Name properties of this new button (Text = Clear Message; Name
= btnClearMessage). Please attempt to provide code for btnClearMessage button which
will clear the message displayed on the lblShowMessage when such button is clicked.
14. Task 2: Modify btnShowMessage to take message input from the user and then display the
message on lblMessage. [Hint: use InputBox() function]
15. Task 3: Add additional three buttons namely: Red, Blue and Black. Code each button to
change the fore color property of the lblMessage when the button is clicked [Hint: select
predefined color e.g. Color.Red].

Supplementary Learning Resources:


https://www.youtube.com/watch?v=BzC16c7bXJU
https://www.youtube.com/watch?v=LqltC7nescM

You might also like