'一个CallByName的例子 '把如下的代码用记事本保存为frmMain.frm (不带txt后缀名) VERSION 5.00 Begin VB.Form frmMain Caption = "CallByName - Tutorial 1" ClientHeight = 915 ClientLeft = 60 ClientTop = 345 ClientWidth = 6645 LinkTopic = "Form1" ScaleHeight = 915 ScaleWidth = 6645 StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdExecute Caption = "Calculate" Default = -1 'True Height = 390 Left = 1080 TabIndex = 4 Top = 495 Width = 1695 End Begin VB.ComboBox cmbAction Height = 315 Left = 1080 TabIndex = 2 Top = 135 Width = 1695 End Begin VB.TextBox txtValue2 Height = 300 Left = 2865 TabIndex = 1 Top = 135 Width = 1035 End Begin VB.TextBox txtValue1 Height = 285 Left = 75 TabIndex = 0 Top = 135 Width = 930 End Begin VB.Label lblResult BackStyle = 0 'Transparent Caption = "=" BeginProperty Font Name = "MS Sans Serif" Size = 13.5 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 345 Left = 3960 TabIndex = 3 Top = 135 Width = 2640 End End Attribute VB_Name = "frmMain" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option ExplicitPrivate Sub cmdExecute_Click() lblResult.Caption = "= " & CallByName(frmMain, cmbAction.Text, VbMethod, txtValue1, txtValue2) End SubPrivate Sub Form_Load() With cmbAction .AddItem "Multiply" .AddItem "Minus" .AddItem "DivideBy" .AddItem "Plus" .ListIndex = 0 End With End SubPublic Function Multiply(lngValue1 As Long, lngValue2 As Long) As Long Multiply = lngValue1 * lngValue2 End FunctionPublic Function Minus(lngValue1 As Long, lngValue2 As Long) As Long Minus = lngValue1 - lngValue2 End FunctionPublic Function DivideBy(lngValue1 As Long, lngValue2 As Long) As Long DivideBy = lngValue1 / lngValue2 End FunctionPublic Function Plus(lngValue1 As Long, lngValue2 As Long) As Long Plus = lngValue1 + lngValue2 End Function
to xiaotanglx 我所要执行的函数没有对象,列如 msgbox callbyname 必须的有对象名
Case Mousedown :Call 控件名_Mousedown(参数)
Case MouseMove :Call 控件名_MouseMove(参数)
end Select
object 必需的;变体型(对象)。函数将要执行的对象的名称。
procedurename 必需的;变体型(字符串)。一个包含该对象的属性名称或者方法名称的字符串表达式。
calltype 必需的;常数。一个 vbCallType 类型的常数,代表正在被调用的过程的类型。
arguments() 可选的:变体型(数组)。
说明CallByName 函数用于获取或者设置一个属性,或者在运行时使用一个字符串名称来调用一个方法。在下面的例子中,第一行使用 CallByName 来设置一个文本框的 MousePointer 属性,第二行得到 MousePointer 属性的值,第三行调用 Move 方法来移动文本框:CallByName Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName (Text1, "MousePointer", vbGet)
CallByName Text1, "Move", vbMethod, 100, 100
'把如下的代码用记事本保存为frmMain.frm (不带txt后缀名)
VERSION 5.00
Begin VB.Form frmMain
Caption = "CallByName - Tutorial 1"
ClientHeight = 915
ClientLeft = 60
ClientTop = 345
ClientWidth = 6645
LinkTopic = "Form1"
ScaleHeight = 915
ScaleWidth = 6645
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdExecute
Caption = "Calculate"
Default = -1 'True
Height = 390
Left = 1080
TabIndex = 4
Top = 495
Width = 1695
End
Begin VB.ComboBox cmbAction
Height = 315
Left = 1080
TabIndex = 2
Top = 135
Width = 1695
End
Begin VB.TextBox txtValue2
Height = 300
Left = 2865
TabIndex = 1
Top = 135
Width = 1035
End
Begin VB.TextBox txtValue1
Height = 285
Left = 75
TabIndex = 0
Top = 135
Width = 930
End
Begin VB.Label lblResult
BackStyle = 0 'Transparent
Caption = "="
BeginProperty Font
Name = "MS Sans Serif"
Size = 13.5
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 345
Left = 3960
TabIndex = 3
Top = 135
Width = 2640
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option ExplicitPrivate Sub cmdExecute_Click()
lblResult.Caption = "= " & CallByName(frmMain, cmbAction.Text, VbMethod, txtValue1, txtValue2)
End SubPrivate Sub Form_Load()
With cmbAction
.AddItem "Multiply"
.AddItem "Minus"
.AddItem "DivideBy"
.AddItem "Plus"
.ListIndex = 0
End With
End SubPublic Function Multiply(lngValue1 As Long, lngValue2 As Long) As Long
Multiply = lngValue1 * lngValue2
End FunctionPublic Function Minus(lngValue1 As Long, lngValue2 As Long) As Long
Minus = lngValue1 - lngValue2
End FunctionPublic Function DivideBy(lngValue1 As Long, lngValue2 As Long) As Long
DivideBy = lngValue1 / lngValue2
End FunctionPublic Function Plus(lngValue1 As Long, lngValue2 As Long) As Long
Plus = lngValue1 + lngValue2
End Function
我所要执行的函数没有对象,列如 msgbox callbyname 必须的有对象名