将属性作为用户窗体控件的参数传递 [英] Passing a property as an argument for userform controls

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

问题描述

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

I'm creating a function that allows me to transition a user-form controls property 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. See simplified example below:

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方法,但似乎无法使其正常工作.尝试时可能没有正确使用它.

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?

推荐答案

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

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成员来读取当前值,然后添加increment,然后然后调用Property Let成员以 write 新值.

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屋!

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