VB中的属性过程详解

Eddy 发布于2009-12-13 20:32:30 分类: 程序设计 已浏览loading 网友评论0条 我要评论

VB6中属性过程通过Property Get、Property Let及Property Set来定义。其中用Property Get读属性,Property Let写(改变)属性,Property Set来给一个对象设置引用。

属性过程主要在类模块中使用,当把属性过程用于预定义对象是,主要用来给窗体增加属性。

示例代码(给窗体增加一个“窗体居中”的属性):

Option Explicit
Private IsCenter As Boolean
Public Property Let center(x As Boolean)
    IsCenter = x
    If IsCenter Then
        Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
    End If
End Property
Public Property Get center() As Boolean
    center = IsCenter
End Property

Private Sub Command1_Click()
    If center = True Then
        Print "窗体位于屏幕中央 center="; center
    ElseIf center = False Then
        Print "窗体不在屏幕中央 center="; center
    End If
End Sub

Private Sub Command2_Click()
    Me.center = True
End Sub

Private Sub Command3_Click()
    Me.Left = Rnd * 4000
    Me.Top = Rnd * 2000
    center = False
End Sub

已经有(0)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/program/234.html     欢迎订阅Eddy Blog

记住我的信息,下次不用再输入 欢迎给Eddy Blog留言