0% found this document useful (0 votes)
46 views

Programming Environment and Writing Programs

This document provides instructions on using Visual Basic to create a simple Windows application with a relational database. It describes how to start a new VB project, add controls like text boxes and buttons to a form, add code to buttons to perform calculations with data from the text boxes, and use implicit and explicit conversions between data types. The conversions include casting between integer, floating point, and string types using methods like CType, CShort, and Convert.ToString.

Uploaded by

Taimoor Hasan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Programming Environment and Writing Programs

This document provides instructions on using Visual Basic to create a simple Windows application with a relational database. It describes how to start a new VB project, add controls like text boxes and buttons to a form, add code to buttons to perform calculations with data from the text boxes, and use implicit and explicit conversions between data types. The conversions include casting between integer, floating point, and string types using methods like CType, CShort, and Convert.ToString.

Uploaded by

Taimoor Hasan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

Relational Database Management systems

Programming Environment and Writing


Programs
Starting a New VB Project

1. To start a new Visual Studio window click on Start  All


Programs  Microsoft Visual Studio 2003 .NET  
Microsoft Visual Studio 2003 .NET.

2. Following window will appear on the screen.

3. In the Start Page window, click the New Project button or


click File  New  Project… or click Ctrl +N.

Page 1 of 20
Relational Database Management systems

4. In the Project types click on Visual Basic Projects.


5. In the Templates side choose Windows Application (or other
whichever required).
6. Type the name of the new project.
7. Chose project location in Location. Click on browse to
navigate to your folder.
8. Now click Open.

9. New Project dialog box appear again.

Page 2 of 20
Relational Database Management systems

10.Click OK button.
11. The Form1 appears on the screen.

12. This is the Design mode of the program.

Page 3 of 20
Relational Database Management systems

13. To view the code window, double click on Form1 or click on


the Solution Explorer and click on the View Code icon in
the Solution Explorer.

Code
Window

14. To close the project click on File  Close Project

NOTE:

• When you are editing code in the code window always


remember to stop the application first.
• A project or program is run by pressing the F5
button or pressing Start button and is stopped
by pressing Stop Debugging button .

Page 4 of 20
Relational Database Management systems

Changing Form Properties from the properties Window

1. Click on Form1 and then click the


Properties window located at the right
side of the Form1.
2. If it is not visible then press F4 or
click on View menu and select Properties
Window.
3. The Text field that contains the name for
Form1 appears highlighted in the
Properties window.
4. If the Text field does not appear, scroll
through the Properties window until you
reach the Text attribute under the
Attributes heading.
5. Double-click the Text
6. Write MyFirstForm in place of Form1.
7. Press Enter.
8. The Text appearing at the title bar of
the form.
9. In the similar way we can change other
form properties.

Page 5 of 20
Relational Database Management systems

Adding Some Common Controls to the Form

1. Switch to the Design View of the form and click on Toolbox


on left the main window.

2. If Toolbox is not visible then in the


menu bar click on View  Toolbox or
press Ctrl+Alt+X to show it.
3. Click on the Windows Forms tab in the
Toolbox.
4. A list of common controls will appear
in the list.
5. Add two TextBox and a Button on the
form.
6. We can do this by dragging and dropping
on the main form or double clicking on
the control.
7. Now arrange the control on the form and
arrange them as shown.
8. Set the text property of the button to
Add and Name as cmdAdd.

Page 6 of 20
Relational Database Management systems

9. Set to text property of the two textboxes as nothing/no


text.
10. Set the name property of the two TextBox as txtNum1 and
txtNum2.
11.Your from should look like this now.

12. Double click on the Add button.


13. This will take us to the Code window of the click event of
the Add button.

Dim a, b As Double
a = Val(txtNum1.Text)
b = Val(txtNum2.Text)
MsgBox("Sum of numbers is " & a + b, , "Addition")

14. First we have declared two variables a and b of data type


Double.
15. In the second and third lines we have assigned them the
values which will be typed in the textboxes.
16. Line four shows a message box which will show the result of
the sum of the two numbers a and b.
17. Now press F5 to run the program.

18. Type the numbers as 45 and 35 and press Add button.


19. A message box will show a message that “The sum of numbers
is 80”.

Page 7 of 20
Relational Database Management systems

Implicit Conversion

1. Start Visual Studio.


2. Create a new Windows Application VB Project.
3. Provide a name of the project as Casting.
4. Place two controls on the Form1, first a Label control and
second a Command Button.
5. Name the button as btnIC.
6. Give the text property as “Implicit Conversion”.
7. Your form should look like this as shown below.

8. Double click the command button and type the following code
in the procedure body.

Dim shortVal As Short = 5


Dim SingleVal As Single = 10.5
Dim doubleResult As Double

doubleResult = SingleVal * shortVal


Label1.Text = "Implicit Conversion->Double:" & SingleVal _

& "*" & shortVal & " = " & doubleResult

9. In the first line we have declared and initialized an


integer type variable as shortVal.
10. In the second line we have declared and initialized a
floating point variable singleVal, and in the third line
doubleResult is declared as double.
11. Next we multiply the first two variables and assign the
result to doubleResult variable in the fourth line.
12. The result is stored as double, because doubleResult is a
double variable.
13. The next line prints the result in the Label1.

Page 8 of 20
Relational Database Management systems

14. To exactly find the data type of the doubleResult, add this
line of the code at the end.

MsgBox(doubleResult.GetType.ToString)

15. Run this application, and press the command button, we get
this result.

16. The message box shows the type of the data which
doubleResult has.

Explicit Conversion

1. Add a new command button on the form.


2. Name it as btnEC.
3. Give it the text property as “Explicit Conversion”.
4. Now Form1 should look like this.

5. Double click the button and type the following code in the
procedure body.

Dim shortResult As Short


Dim SingleVal As Single = 10.5
shortResult = CShort(SingleVal)
Label1.Text = "Explicit, -> short: " & SingleVal & " -> " &
shortResult
MsgBox(shortResult.GetType.ToString)

Page 9 of 20
Relational Database Management systems

6. Now we have changed a floating point variable into an


integer type variable.
7. The next line shows its type in a message box.

Explicit Conversions Using the Convert Commands


1. Place a new control of command button on the same form.
2. Give the text property of the button as “Explicit
Conversions Using the Convert Commands”
3. Give the name property as btnExCnv.
4. Form1 should look like this now.

5. Double click on the button and type the following code in


the procedure body.

Dim doubleVal As Double = 99.999


Dim stringResult As String
Dim boolVal As Boolean = True
stringResult = Convert.ToString(boolVal) &
Convert.ToString(doubleval)
Label1.Text = "Explicit, -> string: " & boolVal & " and "
doubleval & " -> " & stringResult
MsgBox(stringResult.GetType.ToString)

6. We have doubleval as 99.999, boolVal as True.


7. stringResult is a string variable which has been assigned
the converted values of doubleVal and boolVal as string and
both have been joined.
8. The rest of the code does the same work as we have
mentioned before.
9. Run the application
10. Press the btnExCnv button.
11. We will get the result as shown in figures below.

Page 10 of 20
Relational Database Management systems

Mixed Conversions Using the Convert Commands

1. Similarly place a new command button control on the form.


2. Give the text property of the button as “Mixed Conversions
Using the Convert Commands”
3. Give the name property as btnMxdCnv.
4. Form1 should look like this now.

5. Double click the new button and type the following code in
its procedure body.

Dim integerVal As Integer = 67


Dim longResult As Long
Dim stringVal As String = "17"
longResult = integerVal + Convert.ToInt64(stringVal)
Label1.Text = "Mixed, -> long: " & integerVal &
stringVal & " -> " & longResult
MsgBox(longResult.GetType.ToString)

6. Here we have converted the stringVal variable into Long using the convert
method and added the result to the integerVal and saved into the
longResult variable of type Long.
7. To test the result run the application and press the button.

Page 11 of 20
Relational Database Management systems

Calculating the Area of a Rectangle

1. Create a new Windows Application and name it as RectA.


2. Place one command button, two textboxes and four labels on
Form1.
3. Set the Form1 Text as “Area of a Rectangle” from Properties
Window.
4. Similarly set lable1 Text as Length and label2 Text as
Breadth., and label3 text as “Enter Data”
5. Set label4 Text as “Click Button to Calculate Area”, and
from Font set Bold as True.
6. Set Text of textbox1 and textbox2 as empty.
7. Set the Name of button as “cmdAreaR”, and Text as
“Calculate”.
8. Arrange your controls on the form as such they look like
this:

9. Double click the Calculate button.


10. Type the following code in the cmdRectArea method body.

If TextBox1.Text = "" Or TextBox2.Text = "" Then


MsgBox("Please enter complete data!",
MsgBoxStyle.Information, "Incomplete Data")
Else
Label4.Text = "Area of the rectangle is " &
CDbl(TextBox1.Text) * CDbl(TextBox2.Text)
End If

11. The if condition checks that both values of length and


breadth ager supplied by the user.
12. Else contains the calculation of the area with a string
joined with it.

Page 12 of 20
Relational Database Management systems

13. Press F5 to Start the application.


14. Enter the values for Length and Breadth as 25 and 1000.
15. Click the Calculate button.
16. We will get the following result as shown below:

17. Close the application.

Convert Temperature in oF to oC

1. Create a new Windows Application and name it as F2C.


2. Design the Form1 as depicted below.

3. If the temperature in degree Fahrenheit is O


F and
O
temperature in degree centigrade is C, then their
conversion formulae are given as:
O
C = (OF – 32) * 5/9
O
F = 9/5 * OC +32

4. Go to the code window, and from the controls list select


Textbox1.

5. In the events list of the Textbox1 select TextChanged event.


6. Type the following code in the procedure body.

Page 13 of 20
Relational Database Management systems

Try
Dim a As Double
a = Math.Round((Convert.ToDouble(TextBox1.Text) -
32 ) * 5 / 9, 2)
Label2.Text = "Temperature in degree Centigrade = " +
a.ToString()
Catch ex As Exception
TextBox1.Text = 0
End Try

1. Press F5 and enter temperature in the textbox as 220.


2. We shall get the temperature converted into degree
Fahrenheit as shown below.

Convert Temperature in oC to oF

1. Now create a new project and convert temperature from


degree centigrade to degree Fahrenheit yourself.

Finding the Square of a Number using ListBox

1. Create a new Windows Application as SquareNum.


2. Create the interface of the application as depicted below:

Page 14 of 20
Relational Database Management systems

3. Double click the command button “Find Square”.


4. Type the following code in the procedure body.

Try
square()
Catch ex As Exception
TextBox1.Text = 0
End Try

5. Where square() is a procedure which finds the square of a


number. Its coding is:
Public Sub square()
Dim sqr As Double
sqr = Math.Pow(CDbl(TextBox1.Text), 2)
ListBox1.Items.Add("Square of " & TextBox1.Text &
" is " & sqr)
End Sub

6. Now press F5.


7. Type some number in the textbox and press the command
button.
8. We shall get the squares of the numbers as desired.

Page 15 of 20
Relational Database Management systems

Checking Number

1. Create a new Windows Application as NumCheck.


2. Create the interface of the application as depicted below:

3. Set the text property of the button as “Click Me!”.


4. Set the name property of the textbox as txtNo and text
property as 0.
5. Double click the command button and enter the following
code in the procedure body.

If (CInt(txtNo.Text) > 0 And CInt(txtNo.Text) < 11) Then


MsgBox("You have clicked " & txtNo.Text,
MsgBoxStyle.OKOnly, "Number")
Else
MsgBox("You did not enter number from 1 to 10")
End If

6. We have used an if statement to check numbers from 1 to 10


or entered or not.
7. Run this application.
8. Type some number and click the button.
9. Do not enter any other character other than numbers from
0-9 otherwise error will occur.
10. Errors may be controlled by using different other coding
techniques.

A Simple Calculator

1. Create a new Windows Application as Calculator.


2. Create the interface of the application as depicted below:

Page 16 of 20
Relational Database Management systems

3. Set the properties of different controls as appropriate.


4. We have set name properties of radio buttons as rbAdd,
rbSubt, rbMul,and rbDiv for Add, Subtract, Multiply, and
Divide respectively.
5. Set the name property of the label at the bottom as lblRes,
and name as Result:
6. Go to the code window and from the controls’ list, select
rbAdd and select click event from the events.
7. Type the following code in the procedure body.

Try
lblRes.Text = ""
lblRes.Text = "Sum = " & CDbl(txtOp1.Text) + Dbl(txtOp2.Text)
Catch ex As Exception
MsgBox("Enter correct data!")
End Try

8. Run the application.


9. Type the operand’s values in the textboxes as 10 and 20.
10. Click on Add radio button, we will get the result printed
at the bottom as shown in figure.
11. You may code for other click events of the radio buttons
which is left as an exercise for the participants.

Page 17 of 20
Relational Database Management systems

Creating a list of Numbers---Use of For with IF…Else

1. Create a new Windows Application as SquareNum.


2. Create the interface of the application as depicted below:

3. Set the name property of the first textbox box as


txtStart, and text property as 0.
4. Set the name property of the second textbox box as txtEnd,
and text property as 0.

Page 18 of 20
Relational Database Management systems

5. Set the text property of the labels as shown above.


6. Set the Name property of the button as cmdNosList and text
property as “Generate List of Numbers”.
7. Double click the command button and type this code in the
procedure body:

Try
Label3.Text = "List of numbers from " & txtStart.Text & " to " &
txtEnd.Text & " is:"
Dim i As Integer
ListBox1.Items.Clear()
If CInt(txtStart.Text) > CInt(txtEnd.Text) Then
MsgBox("Start Value must be less than the End Value",
MsgBoxStyle.Information, "Enter correct values")
Else
For i = CInt(txtStart.Text) To CInt(txtEnd.Text) Step 1
ListBox1.Items.Add("Count " & i)
Next
End If
Catch ex As Exception
MsgBox("Enter complete data!", MsgBoxStyle.OKOnly, "Incomplete Data")
End Try

8. Code ensures that start value is always less than the end
value by an if condition.
9. We have also used a try … catch block to encounter any
other error which may halt the program execution.
10.Run application by pressing F5.
11.Enter two numbers in the textboxes and press the command
button.
12. Desired list of numbers will be obtained in the listbox.

Summing ten consecutive numbers---Using While statement


1. Create a new Windows Application as SquareNum.
2. Create the interface of the application as depicted below:

Page 19 of 20
Relational Database Management systems

3. Double click the form and type the following code.


4. The program generates the list of number counts and also
cumulative sums of the numbers.

Dim start As Double


Dim total As Double = 0
Dim count As Integer = 0
While count < 10
total += count
ListBox1.Items.Add("Count = " & count & " , total = " &
total)
count += 1
End While

Page 20 of 20

You might also like