可以在 Visual Basic 中使用 ControlType 確定窗體或報(bào)表中的控件的類型。Byte 型,可讀/寫。
expression.ControlType
expression 必需。返回“應(yīng)用于”列表中的一個(gè)對(duì)象的表達(dá)式。
ControlType 屬性是用于指定控件類型的固有常量。
常量 |
控件 |
注釋 ControlType 屬性只能通過使用 Visual Basic 在窗體設(shè)計(jì)視圖或報(bào)表設(shè)計(jì)視圖中設(shè)置,但能在所有視圖中查看。
ControlType 屬性不僅可以用于在代碼中檢查特定控件的類型,也可以對(duì)控件類型進(jìn)行更改。例如,可以在窗體“設(shè)計(jì)”視圖中將文本框的 ControlType 屬性更改為 acComboBox,使文本框變?yōu)榻M合框。
利用 ControlType 屬性還可以根據(jù)特定的條件,改變窗體上相似控件的特征。例如,當(dāng)不想讓用戶編輯文本框中已有的數(shù)據(jù)時(shí),可以將所有文本框的 SpecialEffect 屬性設(shè)置為“平面”,并將窗體的 AllowEdits 屬性設(shè)置為“否”。(SpecialEffect 屬性不影響是否可以編輯數(shù)據(jù),它只提供一個(gè)控件行為已經(jīng)更改的視覺提示。)
ControlType 還用于在使用 CreateControl 方法創(chuàng)建控件時(shí)指定控件類型。
以下示例檢查窗體上所有控件的 ControlType 屬性。對(duì)所有標(biāo)簽和文本框控件,該過程切換其“SpecialEffect”屬性。當(dāng)標(biāo)簽的“特殊效果”屬性設(shè)置為“凹陷”,且文本框的“特殊效果”屬性設(shè)置為“普通”,AllowAdditions、AllowDeletions 和 AllowEdits 屬性均設(shè)置為 True,則 intCanEdit 變量將切換為允許編輯基礎(chǔ)數(shù)據(jù)。
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