VBA中CommandButton的.caption和.text属性之间有什么区别? [英] What is the distinction between a CommandButton's .caption and .text properties in VBA?

查看:699
本文介绍了VBA中CommandButton的.caption和.text属性之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel中有一个CommandButton,发现.Caption.Text属性都返回了按钮的相同可见文本.这是我用来查看值的代码:

I have a CommandButton in Excel and found that both the .Caption and .Text properties return the same visible text of the button. Here's the code I used to see the values:

Debug.Print ActiveSheet.Buttons("My_Command_Button").Caption
Debug.Print ActiveSheet.Buttons("My_Command_Button").Text

这两个属性之间的区别是什么?

What is the distinction between these two properties and does the distinction matter?

推荐答案

技巧问题.如果检查fm20.dll(或VBE的对象浏览器)中的CommandButton共同类定义,您会发现它没有 拥有 一个Text属性:

Trick question. If you examine the CommandButton coclass definition in fm20.dll (or in the VBE's Object Browser, you'll find that it doesn't have a Text property:

ActiveX控件是在运行时通过扩展来组合的,因此,特定于控件如何嵌入到电子表格或UserForm(或其他相关内容)的属性实际上是由驱动程序驱动的. 拥有"对象的OLE容器.如果您考虑一下,这在一定程度上是有意义的-为什么CommandButton应该关心它的LeftTop属性是什么?您永远不需要拥有不是另一个父窗口或容器的子窗口的CommandButton,并且CommandButton来确定它在窗口中的位置也不是责任-这就是窗口的工作.这是他们没有公共构造函数的部分原因:

The ActiveX controls are composited via extension at run-time, so properties that are specific to how the control is embedded in a spreadsheet, or a UserForm, (or anything else for that matter) are actually being driven by the OLE container that "owns" the object. It makes a certain amount of sense if you think about it - why should a CommandButton care what it's Left or Top property is? You shouldn't ever need to have a CommandButton that isn't the child of another parent window or container, and it isn't the responsibility of the CommandButton to determine where it is within the window - that's the window's job. It's part of the reason why they don't have public constructors:

Dim example As CommandButton
Set example = New CommandButton     '<-- Nope.

如果能够实例化无父母的CommandButton,则会发现在按钮上设置文本的唯一方法是设置.Caption属性.这是因为在深入的Windows API级别上,它是一个窗口(因为它具有hWnd),并且约定是Windows具有Caption属性. 显示的方式是特定于实现的.在这种特定情况下,可以将它们互换使用,但是.Caption属性是真实的",而.Text属性反映了它.除非您对它所做的事情极其奇怪(例如获取其hWnd并直接与Windows API结合使用),否则它们在所有意图和目的上都是同一件事.

If you were able to instantiate a parentless CommandButton, you'd find that the only way to set the text on the button would be to set the .Caption property. That's because at a deep down Windows API level, it is a window (in that it has an hWnd), and the convention is for windows to have a Caption property. How that is displayed is implementation specific. In this specific instance, they can be treated interchangeably, but the .Caption property is the "real one", and the .Text property mirrors it. Unless you're doing something extremely bizarre with it (like getting its hWnd and using that with the Windows API directly), they are for all intents and purposes the same thing.

这篇关于VBA中CommandButton的.caption和.text属性之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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