Friday, December 19, 2008

Reading and Writing Text File in VB2008

To be able to open a file and read the data from a storage unit of a computer, such as a hard drive and able to save the data into the storage unit are important functions of a computer program. In fact, the ability to store, retrieve and modify data makes a computer a powerful tool in database management.

Reading and writing to a text file in VB2008 required the use of the StreamReader class and the StreamWriter class respectively. StreamReader is a tool that enables the streaming of data by moving it from one location to another so that it can be read by the user. For example, it allows the user to read a text file that is stored in a hard drive. On the other hand, the StreamWriter class is a tool that can write data input by the use to a storage device such as the hard drive.

To learn further, please view our new Vb2008 lesson at:

http://www.vbtutor.net/vb2008/vb2008_lesson21.html

Monday, December 15, 2008

Counting items in a Combo Box

In Visual Basic, the combo box is a control that can store a list of items. Is it possible to count the number of items that is stored in the combo box? The answer is yes. The keyword to use is the ListCount property. However, in order to count the items according to categories, we have to use the For....Next loop to accomplish the task.

I have written a program to count the number of students who score A, B, C and D respectively. In this program, the user click the "Add Item" button to enter the grades from an Input Box. After entering all the grades, the user can get the statistics by pressing the 'Count" button.

The code is as follows:


Private Sub Command1_Click()
Dim item As String

item = InputBox("Enter an item")

Combo1.AddItem item



End Sub




Private Sub Command2_Click()
Dim count, i As Integer
Dim item1, item2, item3, item4 As Variant

count = Combo1.ListCount

For i = 0 To count

Combo1.Text = Combo1.List(i)

If Combo1.Text = "A" Then
item1 = item1 + 1
ElseIf Combo1.Text = "B" Then
item2 = item2 + 1
ElseIf Combo1.Text = "C" Then
item3 = item3 + 1
ElseIf Combo1.Text = "D" Then
item4 = item4 + 1
End If
Next

Lbl_A.Caption = item1
Lbl_B.Caption = item2
Lbl_C.Caption = item3
Lbl_D.Caption = item4



End Sub

The Output Screen:





Friday, December 12, 2008

Errors Handling in Visual Basic 2008

Dear Readers

I have written about errors handling in Visual Basic 6 the other day. Now, I have written errors handling in VB2008. In this new lesson, you will learn how to use the Try...Catch....End try statement to manage exceptions or errors in a VB2008 program. For detail info, please view

http://www.vbtutor.net/vb2008/vb2008_lesson20.html

Wednesday, December 10, 2008

Revamp of my website

Dear Vbtutor.net readers,

I have carried out quite a major revamp to the Vbtutor website. It looks much more streamlined and systematic now. It has also becoming easier to navigate. Please check out the lessons at

http://www.vbtutor.net/vbtutor.html

Monday, December 8, 2008

Error Handling in Visual Basic

HTML clipboardError handling is an essential procedure in Visual Basic programming because it can help make the program error-free. An error-free program can run smoothly and efficiently, and the user does not have to face all sorts of problems such as program crash or system hang.
ML clipboard

Errors often occur due to incorrect input from the user. For example, the user might make the mistake of attempting to ask the computer to divide a number by zero which will definitely cause system error. Another example is the user might enter a text (string) to a box that is designed to handle only numeric values such as the weight of a person, the computer will not be able to perform arithmetic calculation for text therefore will create an error. These errors are known as synchronous errors.


Learn how to write errors handling procedure in Visual Basic in our new lesson at

http://www.vbtutor.net/lesson28.html


Thursday, December 4, 2008

Chinese Visual Basic Tutorial

If you have Chinese friends who is interested in learning Visual Basic programming but can't understand the concepts in English, please ask them to visit the Chinese version of vbtutor.net at

http://www.vbtutor.net/chinese/vbtutor_Chinese.html

Wednesday, December 3, 2008

Temperature Converter


With winter around and daily temperature change has become a concern for many people especially those who travel a lot. They are constantly checking out temperature readings everyday However, different parts of the world often use different temperature scales, some of them use Fahrenheit and some use Celsius, so the readings can be confusing. Therefore, it is nice if we have a program that can convert Celsius to Fahrenheit and vice versa. And I have done just that using Visual Basic 6. The code is like this:

Private Sub Command1_Click()
Dim cel, Fah As Single
cel = Val(txtC.Text)
Fah = Val(txtF.Text)

If txtC.Text <> "" And txtF = "" Then
Fah = ((9 / 5) * cel + 32)
txtF.Text = Round(Fah, 1)
Else
cel = (5 / 9) * (Fah - 32)
txtC.Text = Round(cel, 1)
End If

End Sub

Private Sub Command2_Click()
txtC.Text = ""
txtF.Text = ""

End Sub