可以在 Visual Basic 中使用ControlType确定或中的的类型。Byte型,可读/写。
expression.ControlType
expression必需。返回“应用于”列表中的一个对象的表达式。
设置
ControlType属性是用于指定控件类型的。
常量
控件
acBoundObjectFrame
acCheckBox
acComboBox
acCommandButton
acCustomControl
acImage
acLabel
acLine
acListBox
acObjectFrame
acOptionButton
acOptionGroup
acPage
acPageBreak
acRectangle
acSubform
acTabCtl
acTextBox
acToggleButton
注释ControlType或中设置,但能在所有视图中查看。
说明
ControlType属性不仅可以用于在代码中检查特定控件的类型,也可以对控件类型进行更改。例如,可以在窗体“设计”视图中将文本框的ControlType属性更改为acComboBox,使文本框变为组合框。
利用ControlType属性还可以根据特定的条件,改变窗体上相似控件的特征。例如,当不想让用户编辑文本框中已有的数据时,可以将所有文本框的属性设置为“平面”,并将窗体的属性设置为“否”。(SpecialEffect属性不影响是否可以编辑数据,它只提供一个控件行为已经更改的视觉提示。)
ControlType还用于在使用方法创建控件时指定控件类型。
示例
以下示例检查窗体上所有控件的ControlType属性。对所有标签和文本框控件,该过程切换其“SpecialEffect”属性。当标签的“特殊效果”属性设置为“凹陷”,且文本框的“特殊效果”属性设置为“普通”,AllowAdditions、AllowDeletions和AllowEdits属性均设置为True,则intCanEdit变量将切换为允许编辑基础数据。
Sub ToggleControl(frm As Form)
Dim ctl As Control
Dim intI As Integer, intCanEdit As Integer
Const conTransparent = 0
Const conWhite = 16777215
For Each ctl in frm.Controls
With ctl
Select Case .ControlType
Case acLabel
If .SpecialEffect = acEffectShadow Then
.SpecialEffect = acEffectNormal
.BorderStyle = conTransparent
intCanEdit = True
Else
.SpecialEffect = acEffectShadow
intCanEdit = False
End If
Case acTextBox
If .SpecialEffect = acEffectNormal Then
.SpecialEffect = acEffectSunken
.BackColor = conWhite
Else
.SpecialEffect = acEffectNormal
.BackColor = frm.Detail.BackColor
End If
End Select
End With
Next ctl
If intCanEdit = IFalse Then
With frm
.AllowAdditions = False
.AllowDeletions = False
.AllowEdits = False
End With
Else
With frm
.AllowAdditions = True
.AllowDeletions = True
.AllowEdits = True
End With
End If
End Sub