如何将属性作为用户窗体控件的参数传递? [英] How to pass a property as an argument for userform controls?
问题描述
我正在创建一个函数,使我可以将用户窗体中控件的属性从一种状态转换到另一种状态,从而创建动画效果,例如自定义下拉列表.
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 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)
这就是 read 部分.然后,您可以执行 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屋!