Sei sulla pagina 1di 5

VB.

Net Lab 1
Objective:
1) To become familiar with the operation of VB.Net
2) To get a simple understanding of even based programming.

Visual Basic.NET is one part of an enormous application called Microsoft Visual Studio.NET. Each programme that you write
consists of a number of files in a Visual Basic Project. In order to create a new Visual Basic.NET Project you need to

1) Open Visual Studio.NET

2) Create a new Visual Basic.NET project

It is very important that you make sure that your project has the correct name and is in the right directory.

3) Thats all the easy stuff done. Now we have to write the programme. This lab will focus on the process of creating a windows
program without going in to the details of the programming language.

1
VB.Net Development Environment - the main features.
VB.Net has the following components.

The Toolbox: this contains a selection of controls that can be added to a Visual Basic form
Form Layout window: a graphical and editable representation of how the applications form will look when the project is complete.
Properties window: This window displays and allows you to edit the properties of the control that is currently highlighted.
Solution explorer: similar to Windows explorer except it shows only files from the current VB.Net Project.

Toolbox Form Layout Window Properties window Solution Explorer

Figure 1: VB.Net Integrated Development Environment (IDE)

VB.Net is an event-based programming language. It provides you with both an interface (a window with controls of your choosing)
and events to respond to (user clicked a button...etc).

VB.Net requires a different thought process than C or other procedural programming languages, and allows you to focus more on
accomplishing a task. C requires you to first build code to display an interface (a screen) and then more code to get input via mouse and
keyboard. Once that is out of the way, you can start programming your application. It is much easier to create a useful program in
VB.Net than it is in C. In the simpler applications, what you generally do is add a control to the form you are designing. You then select
the event to which you will attach your code and then you write the code. The example on the next page will illustrate this.

2
Procedure
1. Start VB.Net
2. Select the Windows application option in the New Project box, name the application <your_name>Lab1
3. Position the mouse on each of the icons in the General toolbox, noting the name of each.
4. Place two Buttons and a Picture Box in the Form1 box. It should look something like this.

5. Click on a blank point of the Form1 box and then in the Properties window, change Text Shrinking and Growing
name to frmResizer

3
6. Similarly, Click Button1 and change its Text to Bigger. And its name to cmdBigger
7. Change the Button2 Text property to Smaller and its name to cmdSmaller
8. Click the picture box and change its name to picBMP
9. Click on the picture box object in the Form1 box and then in the Properties menu, use the Image property to import a bitmap
picture. Use the picture Gone Fishing.bmp in the C:\Pictures folder.
10. Save the project on your A-drive and run the program.
Note - it is good practice to save each project in a different folder
Your application should look somewhat as follows.

11. Double Click Button1 to display the code view option for this control.
Two lines of code are displayed:
Private Sub cmdBigger_Click(ByVal sender As System.Object

End Sub

12. Between these two line of code, insert the following code:
picBMP.Height = picBMP.Height + 10
picBMP.Width = picBMP.Width + 10

13. Similarly, insert the following code into the Button_Click()


subroutine:

picBMP.Height = picBMP.Height - 10
picBMP.Width = picBMP.Width - 10

The code view window should appear as follows


Public Class frmResizer

Private Sub cmdBigger_Click(ByVal sender As System.Object..


picBMP.Height = picBMP.Height + 10
picBMP.Width = picBMP.Width + 10
End Sub

Private Sub cmdSmaller_Click(ByVal sender As System.Object..


picBMP.Height = picBMP.Height - 10
picBMP.Width = picBMP.Width - 10
End Sub
End Class

15. Modify the program to include two more command buttons called 'hide' and 'restore'
When the hide-button is clicked on, the image should disappear and when the restore button is clicked on, the image should reappear.

Note: Everything you need to write a VB.net program can be found in the help including useful code examples to get you started.
Refer to image properties in the help menu for some hints on how to tackle the problem of hiding images.

4
16. Add a new label to the bottom of the form.
17. Use the properties window to change the value the following properties of label1

name lblWarning
text <blank>
font size 12
minimum size width 150
minimum size height 30

18. Modify the code for the Bigger and Smaller command buttons to the following:

Public Class frmResizer


Private Sub cmdBigger_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
cmdBigger.Click

If picBMP.Height < 125 Then


picBMP.Height = picBMP.Height + 10
picBMP.Width = picBMP.Width + 10
lblWarning.Text = ""
Else
lblWarning.Text = "The Image is too big"
End If
End Sub
Private Sub cmdSmaller_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
cmdSmaller.Click
If picBMP.Height > 10 Then
picBMP.Height = picBMP.Height - 10
picBMP.Width = picBMP.Width - 10
lblWarning.Text = ""
Else
lblWarning.Text = "The image is too small"
End If
End Sub

End Class

Potrebbero piacerti anche