By Kardi Teknomo, PhD.


< Previous | Next | VB Tutorial | Contents >

Form Controller

Form controller is the controller of your program. We are going to give the user easy way to control the drawing of your simple program. Click here to download the code

  1. In the Project Window, do Right Click and it show a pop up menu, choose Add > Form. In the dialog window, press Open button
  2. Change the Name of the form into frmController and the Caption property into Controller. Change the MinButton and MaxButton properties to False. It is mean we will not resize the form.
  3. In the VB menu, select Project > Project1 Properties. In the General Tab, StartUp Object, select frmController, then OK . This is to make VB run from the frmController instead of frmMap.
  4. Drag and drop two Frame control into the frmController. Arrange the two frames so that Frame1 is in the top of Frame2
  5. Change the Caption property of Frame1 into Draw, and the Caption property of Frame2 into Auto Drawing.
  6. Drag and drop two Option button into Frame1. Change the Caption of Option1 into Manually, and the Name into optManual. Change the Caption of Option1 into Automatically, and the name into optAutomatic.
  7. Change the Value property of optManual into True.
  8. Drag and drop one Checkbox control, one Label control, one horizontal Scrollbar and one Listbox control into Frame2.
  9. Change the Checkbox Caption into Animate, and the Name into chkAnimate
  10. Change the Label1 Caption into Max Line, and the AutoSize property into True.
  11. Change the Name property of Scrollbar into ScrlMaxLine. The Max property into 1000, Min property into 10 and Value property into 200.
  12. Arrange the controls so that it looks like this figure. You can try to run the program and feel how it looks like.After we finish arrange the components and giving the name, captions and change some properties, we can start the programming.
  13. In the frmController load event , we initialize the Listbox using code below:
Private Sub Form_Load()
' fill the content of ListBox
List1.List(0) = "Star"
List1.List(1) = "Messy"
List1.List(2) = "Chain"
List1.List(3) = "Rays"

' initialize the first value
List1.Text = "Star"
' call the other form
frmMap.Show
End Sub
Examine the width and height properties of your form with the Frame2 and without Frame2. For example: with Frame2 the width is 2150 and the height is 3480, while without Frame2, the width is the same, but the height is 1320. Then we fixed the size of the form using the following code:
Private Sub Form_Resize()
Me.Width = 2150 ' change the width as your form width
If optManual.Value = True Then
Me.Height = 1320
Else 'If optAutomatic.Value = True Then
Me.Height = 3480
End If
End Sub

15. Double click the option button one by one and type code below

Private Sub optAutomatic_Click()
Call Form_Resize
End Sub
Private Sub optManual_Click()
Call Form_Resize
End Sub

16. Double click the checkbox and type the following code:

Private Sub chkAnimate_Click()
If chkAnimate.Value Then
frmMap.Timer1.Interval = 1
frmMap.Timer1.Enabled = True
Screen.MousePointer = vbCrosshair
Else
frmMap.Timer1.Enabled = False
Screen.MousePointer = vbDefault
End If
End Sub
  1. The last code contains a call to a timer that we haven’t put yet into the control. Go to frmMap, add Timer control into frmMap. Double click the Timer and type the following code:
Private Sub Timer1_Timer()
Dim PointColor As Long
Dim LineColor As Long
Dim maxLine As Integer
Dim DrawingType As String

DrawingType = frmController.List1.Text
maxLine = frmController.ScrlMaxLine.Value

Randomize
PointColor = Rnd * 16777215 ' rnd * maximum color number
LineColor = Rnd * 16777215

Call AutoDraw(DrawingType, PointColor, LineColor, maxLine)
End Sub

Notice that the procedure AutoDraw does not exist yet, but we can already specify the input and the output of this procedure.


18. Now we code the sub procedure AutoDraw as the following code:

Sub AutoDraw(DrawingType As String, _
PointColor As Long, LineColor As Long, _
maxLine As Integer)
Dim X As Single, Y As Single
Dim X2 As Single, Y2 As Single
Dim count As Integer
Static LastingColor As Long
X = Rnd * picMap.ScaleWidth
Y = Rnd * picMap.ScaleHeight
Select Case DrawingType
Case "Star"
If num Mod maxLine = 0 Then
picMap.Cls
num = 1
X1 = X: Y1 = Y
Else
num = num + 1
X2 = X: Y2 = Y
picMap.DrawWidth = 5
picMap.PSet (X, Y), PointColor
picMap.DrawWidth = 1
picMap.Line (X1, Y1)-(X2, Y2), LineColor
End If
Case "Messy"
If num Mod maxLine = 0 Then
picMap.Cls
num = 1
Else
num = num + 1
X2 = X: Y2 = Y
picMap.DrawWidth = 5
picMap.PSet (X2, Y2), PointColor
picMap.DrawWidth = 1
picMap.Line (X1, Y1)-(X2, Y2), LineColor
X1 = X2: Y1 = Y2
End If
Case "Chain"
If num Mod maxLine = 0 Then
picMap.Cls
num = 1
X1 = X: Y1 = Y
Else
num = num + 1
count = num Mod 4
Select Case count
Case 0:
X2 = 0.1 * X + X1
Y2 = 0.1 * Y + Y1
Case 1:
X2 = -0.1 * X + X1
Y2 = -0.1 * Y + Y1
Case 2:
X2 = 0.1 * X + X1
Y2 = -0.1 * Y + Y1
Case 3:
X2 = -0.1 * X + X1
Y2 = 0.1 * Y + Y1
End Select

picMap.DrawWidth = 5
picMap.PSet (X2, Y2), PointColor
picMap.DrawWidth = 1
picMap.Line (X1, Y1)-(X2, Y2), LineColor
X1 = X2: Y1 = Y2
End If
Case "Rays"
If num Mod maxLine = 0 Then
picMap.Cls
num = 1
X1 = X: Y1 = Y
LastingColor = LineColor
Else
num = num + 1
X2 = X: Y2 = Y
picMap.DrawWidth = 1
picMap.Line (X1, Y1)-(X2, Y2), LastingColor - num
End If
End Select
End Sub


19. Run the program

< Previous | Next | VB Tutorial | Contents >

Rate this tutorial or give your comments about this tutorial

This tutorial is copyrighted.

Preferable reference for this tutorial is

Teknomo, Kardi. Visual Basic Tutorial . http:\\people.revoledu.com\kardi\tutorial\VB