Monday, May 10, 2010

Creating a Calender in VB6

Creating a calender in VB6 is a relatively simple task. There is no need to write your own code. What you do is insert the calender component of an ActiveX Control that comes with VB6 into the form. However, the ActiveX control is not included into the toolbox by default, you have to add it with the following steps:
1. From the Project menu, choose Components
2. In the Components dialog, select Microsoft Windows Common Controls 2-6.0 and click OK.
The Calender control will be added to the toolbox. 
3. Select the MonthView control icon from the Visual Basic ToolBox 
4. Drag the MonthView control into the form. 
Now run the program and there you have your very own calender!
 

There is another way to create a calender in VB. This time you use the Microsoft Calender Control 11. As this is also an ActiveX control, it is not launched into the toolbox by default, you need to add it in using the method similar to the above example.
1. From the Project menu, choose Components
2. In the Components dialog, select Microsoft Calender Control 11 and click OK.
Now the calender control will appear in the toolbox. Drag it into the form and press F5 to run the program, and you have your calender as shown below:
 
Try them out yourself. Everyone can do it!

Saturday, May 8, 2010

Function to calculate the sum of numbers between 1 and 100 that are divisible by 4

Function SumOfNum(ByVal x As Integer) As Integer 

Dim i As Integer
SumOfNum = 0
For i = 1 To 100
If i Mod x = 0 Then
SumOfNum = SumOfNum + i
Else
SumOfNum = SumOfNum
End If
Next i 

End Function 

Private Sub Command1_Click()
Dim w As Integer
x = Text1.Text
w = SumOfNum(x)
Label1.Caption = w 

End Sub

Friday, May 7, 2010

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

Wednesday, May 5, 2010

Changing Background and Foreground Color at Run Time for VB6

It is very easy to change background color and foreground color at run time, just use the code below:
Private Sub Form_Load()
Form1.Show
Form1.BackColor = vbRed
Form1.ForeColor = vbBlue
Form1.Print "Foreground"
End Sub