如何将属性作为用户窗体控件的参数传递?

罗伯特·托达尔

我的问题

我正在创建一个函数,使我可以将用户窗体中控件的属性从一种状态转换到另一种状态,从而创建动画效果,例如自定义下拉列表。

但是,我似乎无法弄清楚是否有办法将特定属性作为参数传递,以使函数保持动态。

简化示例

Private Sub test()
    ' Transition the height of Frame1 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)
    
    ' 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
    
    'Dim Prop As Variant
    'Prop = CallByName(Obj, objProperty, VbLet)
    
    Dim index As Long
    For index = 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 index
End Function

我尝试查看VBAcallbyname方法,但似乎无法使其正常工作。也许我尝试时没有正确使用它?

我很乐意在如何将控件属性传递给函数方面获得任何反馈或帮助。

马修·金登(Mathieu Guindon)

让我们看一下这里被调用的内容:

Obj.objProperty = Obj.objProperty + Increment

忽略了你非法治疗的事实,String本地/参数作为成员Obj,你必须Obj.objProperty对分配的两侧,所以语法上,我们有:

[object].[property-let] = [object].[property-get] + [parameter]

换句话说,无法通过单个CallByName调用进行工作:您需要首先计算分配的RHS,然后,您需要调用该Property Get成员以读取当前值,然后添加increment然后调用Property Let成员以写入新值。

Dim currentValue As Double
currentValue = CallByName(obj, objProperty, vbGet)

这就是阅读的部分。然后,您可以执行部分:

CallByName obj, objProperty, vbLet, currentValue + increment

不确定Prop局部Variant变量将用于什么

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章