善用預設屬性

很多物件都具有預設屬性。使用預設屬性能夠簡化程式碼,因為設定屬性值時,不必特別指定該屬性名稱;但是使用預設屬性,也會降低程式碼的可讀性。例如以下就是利用預設屬性來簡化程式碼的例子:

不使用預設屬性時:

Private Sub Command1_Click()
     Dim ctl As Control
     For Each ctl In Controls
          If TypeOf ctl Is Label Then
               Print ctl.Caption
          ElseIf TypeOf ctl Is TextBox Then
               Print ctl.Text
          End If
     Next
End Sub

使用預設屬性時:

Private Sub Command2_Click()
     Dim ctl As Control
     For Each ctl In Controls
          If TypeOf ctl Is Label Or TypeOf ctl Is TextBox Then
               Print ctl
          End If
     Next
End Sub

以下是一些控制項的預設屬性:

控制項 預設屬性
Check box Value
Combo box Text
Command button Value
Common dialog Action
Data Caption
Data-bound combo box Text
Data-bound grid Text
Data-bound list box Text
Directory list box Path
Drive list box Drive
File list box FileName
Frame Caption
Horizontal scroll bar Value
Image Picture
Label Caption
Line Visible
List box Text
Option button Value
Picture box Picture
Shape Shape
Text box Text
Timer Enabled
Vertical scroll bar Value

 


[ 上一個 | 首頁 | 重要觀念 | 下一個 ]

This page was written by Jaric on Feb. 14 , 1998. All rights reserved.

Total pageview since 4/6/1999.