我正在创建一个函数,允许我将用户表单控件属性从一种状态转换为另一种状态,从而创建动画效果,例如自定义下拉列表。
但是,我似乎无法弄清楚是否有办法将特定属性作为参数传递,以保持函数更具动态性。请参阅下面的简化示例:
Private Sub test()
'Transition Frame1s height an additional 150
Transition Frame1, "Height", 13, 0.2, 150
End Sub
Private Function Transition (Obj As Object, objProperty As String, _
framesPerSec As Integer, sec As Double, Increment As Double)
Dim I As Integer
'Dim Prop As Variant
'CALCULATE INCREMENT STEPS\TIME STEPS
Increment = Increment / framesPerSec
sec = (sec * 1000) / framesPerSec
'I tried the code below. Might be misunderstanding how CallByName works.
'I would use this in place of objProperty below
'Prop = CallByName(Obj, objProperty, VbLet)
For I = 1 To framesPerSec
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
' HERE IS THE EXAMPLE OF WHAT I'M TRYING TO ACCOMPLISH
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
Obj.objProperty = Obj.objProperty + Increment
'API SLEEP FUNCTION (MILLISECONDS)
Sleep sec
Next I
End Function
我已经尝试过查看VBA callbyname方法,但似乎无法让它发挥作用。也许我在尝试时没有正确使用它。
我很乐意向任何方向提供反馈或帮助吗?
答案 0 :(得分:1)
让我们看一下这里调用的内容:
Obj.objProperty = Obj.objProperty + Increment
忽略您将String
本地/参数作为Obj
成员非法处理的事实,您在作业的两边都有Obj.objProperty
,所以语法上,我们有:
[object].[property-let] = [object].[property-get] + [parameter]
换句话说,没有办法可以使用单个CallByName
调用:您需要先计算赋值的RHS,为了做到这一点,您需要调用{ {1}}成员读取当前值,然后添加Property Get
,然后调用increment
成员 write < / em>新值。
Property Let
这就是读取部分。然后你可以做写部分:
Dim currentValue As Double
currentValue = CallByName(obj, objProperty, vbGet)
不确定CallByName obj, objProperty, vbLet, currentValue + increment
本地Prop
变量的用途。
答案 1 :(得分:0)
(感谢Mat's Mug)
Private Function Transition(Obj As Object, objProperty As String,
framesPerSec As Integer, _
sec As Double, Increment As Double)
Dim I As Integer
Dim currentValue As Double
'CALCULATE INCREMENT STEPS\TIME STEPS
Increment = Increment / framesPerSec
sec = (sec * 1000) / framesPerSec
For I = 1 To framesPerSec
'GET CURRENTVALUE
currentValue = CallByName(Obj, objProperty, VbGet)
'INCREMENT PROPERTY VALUE
CallByName Obj, objProperty, VbLet, currentValue + Increment
'API SLEEP FUNCTION (MILLISECONDS)
Sleep sec
Next I
End Function