可以在 Visual Basic 中使用 ControlType 确定窗体或报表中的控件的类型。Byte 型,可读/写。
expression.ControlType
expression 必需。返回“Applies To”列表中的一个对象的表达式。
ControlType 属性是用于指定控件类型的固有常量。
| 常量 | 控件 | 
|---|---|
| acBoundObjectFrame | 绑定对象框 | 
| acCheckBox | 复选框 | 
| acComboBox | 组合框 | 
| acCommandButton | 命令按钮 | 
| acCustomControl | ActiveX(自定义)控件 | 
| acImage | 图像 | 
| acLabel | 标签 | 
| acLine | 线条 | 
| acListBox | 列表框 | 
| acObjectFrame | 未绑定对象框或图表 | 
| acOptionButton | 选项按钮 | 
| acOptionGroup | 选项组 | 
| acPage | 页 | 
| acPageBreak | 分页符 | 
| acRectangle | 矩形 | 
| acSubform | 子窗体/子报表 | 
| acTabCtl | 选项卡 | 
| acTextBox | 文本框 | 
| acToggleButton | 切换按钮 | 
ControlType 属性不仅可以用于在代码中检查特定控件的类型,也可以对控件类型进行更改。例如,可以在窗体“设计”视图中将文本框的 ControlType 属性更改为 acComboBox,使文本框变为组合框。
利用 ControlType 属性还可以根据特定的条件,改变窗体上相似控件的特征。例如,当不想让用户编辑文本框中已有的数据时,可以将所有文本框的 SpecialEffect 属性设置为“平面”,并将窗体的 AllowEdits 属性设置为“否”。(SpecialEffect 属性不影响是否可以编辑数据,它只提供一个控件行为已经更改的视觉提示。)
ControlType 还用于在使用 CreateControl 方法创建控件时指定控件类型。
以下示例检查窗体上所有控件的 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