如何将属性作为参数传递给用户窗体控件? [英] How to pass a property as an argument for userform controls?

查看:20
本文介绍了如何将属性作为参数传递给用户窗体控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a function that allows me to transition properties of controls, that are in a userform, from one state to another, creating an animation effect such as a custom drop-down list.

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

However, I can't seem to figure out if there is a way to pass the specific property as an argument, to keep the function more dynamic.

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

我已经尝试查看 VBA callbyname 方法,但似乎无法让它工作.可能是我尝试的时候没有正确使用它?

I've tried looking at the VBA callbyname method, but can't seem to get it to work. Perhaps I didn't use it correctly when trying it?

我很乐意就如何将控件属性传递给函数提供任何反馈或帮助?

I'm happy to take any feedback or help in any direction in how to pass control properties to a function?

推荐答案

让我们看看这里调用了什么:

Let's look at what's being invoked here:

Obj.objProperty = Obj.objProperty + Increment

忽略您非法将 String 本地/参数视为 Obj 的成员这一事实,您将 Obj.objProperty赋值的两边,所以在语法上,我们有:

Ignoring the fact that you're illegally treating a String local/parameter as a member of Obj, you have Obj.objProperty on both sides of the assignment, so syntactically, we have:

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

换句话说,没有办法可以使用单个 CallByName 调用:您需要首先计算分配的 RHS,为此您需要调用 Property Get 成员读取当前值,然后添加增量,然后然后调用Property Let 成员来写入新值.

In other words, there's no way that can work with a single CallByName invocation: you need to first compute the RHS of the assignment, and in order to do that you need to invoke the Property Get member to read the current value, then add the increment, and then invoke the Property Let member to write the new value.

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

这就是阅读部分.然后你可以做write部分:

So that's the read part. Then you can do the write part:

CallByName obj, objProperty, vbLet, currentValue + increment

不确定 Prop 本地 Variant 变量的用途.

Not sure what the Prop local Variant variable would be used for.

这篇关于如何将属性作为参数传递给用户窗体控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆