Você está na página 1de 12

How to use MDI Form in VB?

The Form that you have been using so far are single form. In Visual Basic you can use
Multiple Document Interface (MDI) Form, a form that can contain multiple forms. Here
is how to create MDI Form:

1. Start Visual Basic Standard Exe project.


2. In the Project Window, do Right Click and it show a pop up menu, choose Add >
MDI Form. In the dialog window, press Open button
3. Go to Form1. Change the MDI Child Property of Form1 (and all other forms if
exist) into True. This will make Form1 as child form instead of parent
4. In the VB menu, select Project > Project1 Properties. In the General Tab,
StartUp Object, select MDI Form1, then OK. This is to make VB run for the first
time by calling the MDI Form.
5. Run the program
<Previous lesson | Table of Content | Content of this Chapter | Next lesson>

1. Continue the MDI Form project, in the Project Window, double click the MDI
Form1 to put the MDI Form in front
2. In the Menu of VB, click Tools > Menu
Editor or you can use Ctrl-E. The menu
Editor will appear.
3. In the Caption, type &File then in the name,
type mnuFile then click Next button
4. In the Caption, type &Exit then in the name,
type mnuExit then click right arrow button.
In the Shortcut list, choose Ctrl-X, then Next
button
5. In the Caption, type &Window then in the
name, type mnuWindow then click the
Window List checkbox, and then the Next
button.
6. In the Caption, type &Calendar then in the
name, type mnuCalendar then click right
arrow button. In the Shortcut list, choose Ctrl-L, then Next button
7. In the Caption, type &Controller then in the name, type mnuController then click
right arrow button. In the Shortcut list, choose F6, then Next button
8. The menu editor will be the same as figure on the right. Press OK
9. Now you have menu in the MDI Form, click File>Exit and enter
10. In the mnuExit_Click procedure, type

Private Sub mnuExit_Click()

End
End Sub

11. In the menu of MDI form, select Window > Controller and click to wrie the
subsequent code

Private Sub mnuController_Click()

If mnuController.Checked = True Then


'now is checked, make it unchecked
frmController.Hide
mnuController.Checked = False
Else ' now is unchecked, make it checked
frmController.Show
mnuController.Checked = True
End If
End Sub

12. Run the program, and look at the Window menu and try the Exit menu.

Adding Component into ToolBox


1. In the Toolbox, Right click and a pop up menu
will be shown. You choose Components…A
dialog window will appear
2. In the control Tab of the dialog, scroll down and
find Microsoft Calendar Control. Check the
checkbox and press OK
3. See that the Calendar control is added to your toolbox
4. In the Project Window, do Right Click and it show a pop up menu, choose Add >
Form. In the dialog window, select New Tab and Form, then press Open button
5. Change the Name property of the new form into frmCalendar. Change the Caption
property into Calendar.
6. Drag and drop Calendar Control from the Toolbox into frmCalendar. Arrange it so
that look nice. For example, if the nice looking of calendar has width of 5000 and
height of 4000, then we add 350 for the height of form and 50 to the width of
form. Type the following code in the Form_Resize of frmCalendar to fixed the
size of the Calendar form.

Private Sub Form_Resize()

Calendar1.Width = 5000
Calendar1.Height = 4000
Me.Width = 5050
Me.Height = 4350
End Sub

7. Add a command button into frmCalendar. Change the Name property to


cmdToday, the Caption property to Today and resize it small enough to put in the
right corner of the calendar. Type the following code into the click event of the
command button:

Private Sub cmdToday_Click()

Calendar1.Today

End Sub

8. In the project window, double click MDI Form to put the MDI form in front.
9. In the Menu of VB, click Tools > Menu Editor or you can use Ctrl-E. The menu
Editor will appear.
10. Click the space below &Window then in the Caption, type &Calendar then in the
name, type mnuCalendar . Check the Checked checkbox then click the right
arrow. After that, click Next button and press OK button.
11. Now in the MDI Form, you select the menu Window > Calendar and press, then
type the code below:

Private Sub mnuCalendar_Click()

If mnuCalendar.Checked = True Then


'now is checked, make it unchecked
frmCalendar.Hide
mnuCalendar.Checked = False
Else ' now is unchecked, make it checked
frmCalendar.Show
mnuCalendar.Checked = True
End If
End Sub

12. Add code below to call the Calendar Form from the MDI form at the first time
and to maximize the MDI form:

Private Sub MDIForm_Load()

frmCalendar.Show
MDIForm1.WindowState = vbMaximized
End Sub
13. Run the program. Select the menu Window > Calendar several times to see the
effect of Checked menu.

VERSION 4.00
Begin VB.Form Calculator
BorderStyle = 1 'Fixed Single
Caption = "Calculator"
ClientHeight = 2970
ClientLeft = 2580
ClientTop = 1485
ClientWidth = 3270
ClipControls = 0 'False
BeginProperty Font
name = "System"
charset = 1
weight = 700
size = 9.75
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 3375
Icon = "CALC.frx":0000
Left = 2520
LinkMode = 1 'Source
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 2970
ScaleWidth = 3270
Top = 1140
Width = 3390
Begin VB.CommandButton Number
Caption = "7"
Height = 480
Index = 7
Left = 120
TabIndex = 7
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "8"
Height = 480
Index = 8
Left = 720
TabIndex = 8
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "9"
Height = 480
Index = 9
Left = 1320
TabIndex = 9
Top = 600
Width = 480
End
Begin VB.CommandButton Cancel
Caption = "C"
Height = 480
Left = 2040
TabIndex = 10
Top = 600
Width = 480
End
Begin VB.CommandButton CancelEntry
Caption = "CE"
Height = 480
Left = 2640
TabIndex = 11
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "4"
Height = 480
Index = 4
Left = 120
TabIndex = 4
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "5"
Height = 480
Index = 5
Left = 720
TabIndex = 5
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "6"
Height = 480
Index = 6
Left = 1320
TabIndex = 6
Top = 1200
Width = 480
End
Begin VB.CommandButton Operator
Caption = "+"
Height = 480
Index = 1
Left = 2040
TabIndex = 12
Top = 1200
Width = 480
End
Begin VB.CommandButton Operator
Caption = "-"
Height = 480
Index = 3
Left = 2640
TabIndex = 13
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "1"
Height = 480
Index = 1
Left = 120
TabIndex = 1
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "2"
Height = 480
Index = 2
Left = 720
TabIndex = 2
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "3"
Height = 480
Index = 3
Left = 1320
TabIndex = 3
Top = 1800
Width = 480
End
Begin VB.CommandButton Operator
Caption = "X"
Height = 480
Index = 2
Left = 2040
TabIndex = 14
Top = 1800
Width = 480
End
Begin VB.CommandButton Operator
Caption = "/"
Height = 480
Index = 0
Left = 2640
TabIndex = 15
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "0"
Height = 480
Index = 0
Left = 120
TabIndex = 0
Top = 2400
Width = 1080
End
Begin VB.CommandButton Decimal
Caption = "."
Height = 480
Left = 1320
TabIndex = 18
Top = 2400
Width = 480
End
Begin VB.CommandButton Operator
Caption = "="
Height = 480
Index = 4
Left = 2040
TabIndex = 16
Top = 2400
Width = 480
End
Begin VB.CommandButton Percent
Caption = "%"
Height = 480
Left = 2640
TabIndex = 17
Top = 2400
Width = 480
End
Begin VB.Label Readout
Alignment = 1 'Right Justify
BackColor = &H0000FFFF&
BorderStyle = 1 'Fixed Single
Caption = "0."
BeginProperty Font
name = "MS Sans Serif"
charset = 1
weight = 700
size = 12
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
ForeColor = &H00000000&
Height = 375
Left = 120
TabIndex = 19
Top = 105
Width = 3000
End
End
Attribute VB_Name = "Calculator"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
'
------------------------------------------------------------------------
' Copyright (C) 1994 Microsoft Corporation
'
' You have a royalty-free right to use, modify, reproduce and distribute
' the Sample Application Files (and/or any modified version) in any way
' you find useful, provided that you agree that Microsoft has no
warranty,
' obligations or liability for any Sample Application Files.
'
------------------------------------------------------------------------
Option Explicit
Dim Op1, Op2 ' Previously input operand.
Dim DecimalFlag As Integer ' Decimal point present yet?
Dim NumOps As Integer ' Number of operands.
Dim LastInput ' Indicate type of last keypress event.
Dim OpFlag ' Indicate pending operation.
Dim TempReadout

' Click event procedure for C (cancel) key.


' Reset the display and initializes variables.
Private Sub Cancel_Click()
ReadOut = Format(0, "0.")
Op1 = 0
Op2 = 0
Form_Load
End Sub

' Click event procedure for CE (cancel entry) key.


Private Sub CancelEntry_Click()
ReadOut = Format(0, "0.")
DecimalFlag = False
LastInput = "CE"
End Sub

' Click event procedure for decimal point (.) key.


' If last keypress was an operator, initialize
' readout to "0." Otherwise, append a decimal
' point to the display.
Private Sub Decimal_Click()
If LastInput = "NEG" Then
ReadOut = Format(0, "-0.")
ElseIf LastInput <> "NUMS" Then
ReadOut = Format(0, "0.")
End If
DecimalFlag = True
LastInput = "NUMS"
End Sub

' Initialization routine for the form.


' Set all variables to initial values.
Private Sub Form_Load()
DecimalFlag = False
NumOps = 0
LastInput = "NONE"
OpFlag = " "
ReadOut = Format(0, "0.")
Decimal.Caption = Format(0, ".")
End Sub

' Click event procedure for number keys (0-9).


' Append new number to the number in the display.
Private Sub Number_Click(Index As Integer)
If LastInput <> "NUMS" Then
ReadOut = Format(0, ".")
DecimalFlag = False
End If
If DecimalFlag Then
ReadOut = ReadOut + Number(Index).Caption
Else
ReadOut = Left(ReadOut, InStr(ReadOut, Format(0, ".")) - 1) +
Number(Index).Caption + Format(0, ".")
End If
If LastInput = "NEG" Then ReadOut = "-" & ReadOut
LastInput = "NUMS"
End Sub

' Click event procedure for operator keys (+, -, x, /, =).


' If the immediately preceeding keypress was part of a
' number, increments NumOps. If one operand is present,
' set Op1. If two are present, set Op1 equal to the
' result of the operation on Op1 and the current
' input string, and display the result.
Private Sub Operator_Click(Index As Integer)
TempReadout = ReadOut
If LastInput = "NUMS" Then
NumOps = NumOps + 1
End If
Select Case NumOps
Case 0
If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
ReadOut = "-" & ReadOut
LastInput = "NEG"
End If
Case 1
Op1 = ReadOut
If Operator(Index).Caption = "-" And LastInput <> "NUMS" And
OpFlag <> "=" Then
ReadOut = "-"
LastInput = "NEG"
End If
Case 2
Op2 = TempReadout
Select Case OpFlag
Case "+"
Op1 = CDbl(Op1) + CDbl(Op2)
Case "-"
Op1 = CDbl(Op1) - CDbl(Op2)
Case "X"
Op1 = CDbl(Op1) * CDbl(Op2)
Case "/"
If Op2 = 0 Then
MsgBox "Can't divide by zero", 48, "Calculator"
Else
Op1 = CDbl(Op1) / CDbl(Op2)
End If
Case "="
Op1 = CDbl(Op2)
Case "%"
Op1 = CDbl(Op1) * CDbl(Op2)
End Select
ReadOut = Op1
NumOps = 1
End Select
If LastInput <> "NEG" Then
LastInput = "OPS"
OpFlag = Operator(Index).Caption
End If
End Sub

' Click event procedure for percent key (%).


' Compute and display a percentage of the first operand.
Private Sub Percent_Click()
ReadOut = ReadOut / 100
LastInput = "Ops"
OpFlag = "%"
NumOps = NumOps + 1
DecimalFlag = True
End Sub

Você também pode gostar