< 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
- In the Project Window, do Right Click and it show a pop up menu, choose Add > Form. In the dialog window, press Open button
- 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.
- 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.
- Drag and drop two Frame control into the frmController. Arrange the two frames so that Frame1 is in the top of Frame2
- Change the Caption property of Frame1 into Draw, and the Caption property of Frame2 into Auto Drawing.
- 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.
- Change the Value property of optManual into True.
- Drag and drop one Checkbox control, one Label control, one horizontal Scrollbar and one Listbox control into Frame2.
- Change the Checkbox Caption into Animate, and the Name into chkAnimate
- Change the Label1 Caption into Max Line, and the AutoSize property into True.
- Change the Name property of Scrollbar into ScrlMaxLine. The Max property into 1000, Min property into 10 and Value property into 200.
- 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.
- In the frmController load event , we initialize the Listbox using code below:
Private Sub Form_Load()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:' fill the content of ListBoxEnd Sub
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
Private Sub Form_Resize()Me.Width = 2150 ' change the width as your form widthEnd Sub
If optManual.Value = True ThenMe.Height = 1320Else 'If optAutomatic.Value = True ThenMe.Height = 3480End If
15. Double click the option button one by one and type code below
Private Sub optAutomatic_Click()Call Form_ResizeEnd Sub
Private Sub optManual_Click()Call Form_ResizeEnd Sub
16. Double click the checkbox and type the following code:
Private Sub chkAnimate_Click()If chkAnimate.Value ThenEnd SubfrmMap.Timer1.Interval = 1Else
frmMap.Timer1.Enabled = True
Screen.MousePointer = vbCrosshairfrmMap.Timer1.Enabled = FalseEnd If
Screen.MousePointer = vbDefault
- 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 LongEnd Sub
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)
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 SingleEnd Sub
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 DrawingTypeCase "Star"If num Mod maxLine = 0 ThenCase "Messy"picMap.ClsElse
num = 1
X1 = X: Y1 = Ynum = num + 1End If
X2 = X: Y2 = Y
picMap.DrawWidth = 5
picMap.PSet (X, Y), PointColor
picMap.DrawWidth = 1
picMap.Line (X1, Y1)-(X2, Y2), LineColorIf num Mod maxLine = 0 ThenCase "Chain"picMap.ClsElse
num = 1num = num + 1End If
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 = Y2If num Mod maxLine = 0 ThenCase "Rays"picMap.ClsElse
num = 1
X1 = X: Y1 = Ynum = num + 1End If
count = num Mod 4
Select Case countCase 0:End SelectX2 = 0.1 * X + X1Case 1:
Y2 = 0.1 * Y + Y1X2 = -0.1 * X + X1Case 2:
Y2 = -0.1 * Y + Y1X2 = 0.1 * X + X1Case 3:
Y2 = -0.1 * Y + Y1X2 = -0.1 * X + X1
Y2 = 0.1 * Y + Y1
picMap.DrawWidth = 5
picMap.PSet (X2, Y2), PointColor
picMap.DrawWidth = 1
picMap.Line (X1, Y1)-(X2, Y2), LineColor
X1 = X2: Y1 = Y2If num Mod maxLine = 0 ThenEnd SelectpicMap.ClsElse
num = 1
X1 = X: Y1 = Y
LastingColor = LineColornum = num + 1End If
X2 = X: Y2 = Y
picMap.DrawWidth = 1
picMap.Line (X1, Y1)-(X2, Y2), LastingColor - num
19. Run the program
< Previous | Next | VB Tutorial | Contents >
Rate this tutorial or give your comments about this tutorial
Preferable reference for this tutorial is
Teknomo, Kardi. Visual Basic Tutorial . https:\\people.revoledu.com\kardi\tutorial\VB