菜鸟捉虫(11)
本题中,作者对Load事件理解错误。Load事件是当窗体装入到内存时,就自动触发的事件,一般将控件的默认值和窗体级代码放到Load事件。把判断语句放入到Load事件中,程序执行时就判断Label2.Width的值是否大于2500并且小于2550。因为Label2.Width的值定义为0,系统认为不满足条件,所以无法切换图片或关闭。应把判断语句放在定时器事件中。正确的程序段如下:
Private Sub Form_Load()
Timer1.Enabled=True
Label2.Width=0
Label1.Caption=“秀美山川”
Picture1.Picture=LoadPicture(“e:/1.jpg”)
End Sub
Private Sub Timer1_Timer()
Label2.Width=Label2.Width+30
If Label2.Width>2500 And Label2.Width<2550 Then
Label1.Caption=“潺潺流水”
Picture1.Picture=LoadPicture(“e:/2.jpg”)
ElseIf Label2.Width=5000 Then
End
Else
End If
End Sub
由于本栏目推出后,受到了广大“菜鸟级”编程爱好者的喜爱,收到大量反馈信息。经研究约定,获奖读者名额增加至5名;自本期起,所给题目将不说明错误个数,并逐渐加大难度。以下是上期获奖者名单(奖品为《电脑报配套光盘2001年第2期》):
王翔 江苏常州 李华夏 湖南怀化
李俊 广东东莞 刘军 四川德阳
刘建群 陕西汉中
本期题目:
创建一个类名为“CEmployee”的类模块,在类中有以下的定义:
Option Explicit
Private m_lEmployeeNumber As Long
Private m_Family() As String
Public Property Get EmployeeNumber() As Long
EmployeeNumber=m_lEmployeeNumber
End Property
Public Property Let EmployeeNumber(lNumber As Long)
If lNumber<1000 Then
Err.Raise 10,“Cemployee:EmployeeNumber”,“Invalid EmployeeNumber”
Else
m_lEmployeeNumber=lNumber
End If
End Property
Public Property Get Family() As String()
Family=m_Family
End Property
Public Property Let Family(arr As String)
m_Family=arr
End Property
Private Function DisplayFamily() As String
Dim X
Dim sTemp As String
sTemp=“雇员号:”&CStr(m_lEmployeeNumber)&vbCrLf
For Each X In m_Family
sTemp=sTemp&X&vbCrLf
Next
DisplayFamily=sTemp
End Function
在主窗体中,添加以下代码:
Option Explicit
Private Lee As Cemployee
Private Sub Form_Click()
Dim Lee As New CEmployee
Lee.EmployeeNumber=100
ReDim arr(0 To 2) As String
arr(0)=“李明”
arr(1)=“王丽丽”
arr(2)=“李小明”
Lee.Family=arr
Print Lee.DisplayFamily
End Sub