UserForm 对象UserForm object

用户窗体对象是构成应用程序用户界面的一部分的窗口或对话框。A UserForm object is a window or dialog box that makes up part of an application's user interface.

用户窗体集合是一个集合, 其元素表示应用程序中的每个加载的用户窗体The UserForms collection is a collection whose elements represent each loaded UserForm in an application. 用户窗体集合具有**Count** 属性、 Item 方法和**Add** 方法。The UserForms collection has a Count property, an Item method, and an Add method. Count specifies the number of elements in the collection; Item (the default member) specifies a specific collection member; and Add places a new UserForm element in the collection.Count specifies the number of elements in the collection; Item (the default member) specifies a specific collection member; and Add places a new UserForm element in the collection.

语法Syntax

用户窗体****用户窗体[ .Item ] (index)UserForm UserForms [ .Item ] (index)

占位符_索引_表示一个范围为0到用户窗体-1 之间的整数。The placeholder index represents an integer with a range from 0 to UserForms.Count - 1. Item is the default member of the UserForms collection and need not be specified.Item is the default member of the UserForms collection and need not be specified.

备注Remarks

可使用“用户窗体”**** 集合循环访问应用程序中加载的所有用户窗体。You can use the UserForms collection to iterate through all loaded user forms in an application. 它识别名为“用户窗体”**** 的固有全局变量It identifies an intrinsic global variable named UserForms. 可以将用户窗体(index) 传递给一个函数, 该函数的参数被指定为用户窗体类。You can pass UserForms(index) to a function whose argument is specified as a UserForm class.

用户窗体具有确定诸如位置、大小和颜色等外观和其行为的方面的属性User forms have properties that determine appearance such as position, size, and color; and aspects of their behavior.

用户窗体还可用户发起或系统触发的事件。User forms can also respond to events initiated by a user or triggered by the system. 例如,可编写“用户窗体”**** 的“初始化”**** 事件过程中的代码,以在显示“用户窗体”**** 前初始化模块级 变量。For example, you can write code in the Initialize event procedure of the UserForm to initialize module-level variables before the UserForm is displayed.

除了属性和事件之外, 还可以使用方法通过使用代码操作用户窗体。In addition to properties and events, you can use methods to manipulate user forms by using code. 例如, 可以使用**Move** 方法更改用户窗体的位置和大小。For example, you can use the Move method to change the location and size of a UserForm.

在设计用户窗体时, 将**BorderStyle** 属性设置为 "定义边框", 并将**Caption** 属性设置为将文本放在标题栏中。When designing user forms, set the BorderStyle property to define borders, and set the Caption property to put text in the title bar. 在代码中, 可以使用**Hide** 和**Show** 方法使用户窗体运行时不可见或可见。In code, you can use the Hide and Show methods to make a UserForm invisible or visible at run time.

“用户窗体”**** 是对象数据类型UserForm is an Object data type. 可在将变量设置为在设计时声明的“用户窗体”的类型的实例前,将其声明为类型“用户窗体”********。You can declare variables as type UserForm before setting them to an instance of a type of UserForm declared at design time. 同样地,可将参数作为类型“用户窗体”**** 传递给过程Similarly, you can pass an argument to a procedure as type UserForm. 可通过使用 DimSetStatic 语句中的“新建”**** 关键字在代码中创建用户窗体的多个实例。You can create multiple instances of user forms in code by using the New keyword in Dim, Set, and Static statements.

您可以使用**controls** 集合访问用户窗体上的控件集合。You can access the collection of controls on a UserForm by using the Controls collection. 例如, 若要隐藏用户窗体上的所有控件, 请使用与以下内容类似的代码。For example, to hide all the controls on a UserForm, use code similar to the following.

For Each Control in UserForm1.Controls
    Control.Visible = False
Next Control

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.