【正确答案】[解析] 在窗体上建立好控件后,先设置控件属性,再编写事件过程。
控件数组是由一组相同类型的控件组成,建立控件数组时系统给每个元素赋一个惟一的索引号(Index),控件数组 cmdName(3)表示控件数组名为cmdName的第4个元素;利用其下标号可选择不同的控件数组元素,如:控件是否可用由其Enabled属性决定,当该属性值为True时可用,为False时不可用(灰色);控件是否可见由其Visible属性决定,该值为 True时,控件可见,为False时控件不可见。解题步骤:
第一步:建立界面并设置控件属性。程序中用到的控件及其属性见表17-5。
表 17-5
对 象 | 下 标 | 属 性 | 设 置 值 |
| Form1 | —— | Caption | 分苹果 |
| Fram1 | —— | Caption | 分苹果大赛 |
| Picture | PicSmile(0) | Name Picture Aotusize | PicSmile true |
| PicSmile(1) | Name Picture Aotusize | PicSmile True |
| Picture | PicCry(0) | Name Picture Aotusize | PicSmile True |
| PicCry(1) | Name Picture Aotusize | PicSmile true |
| Label | —— | Name Caption | Label Tom White |
| Label | —— | Name Caption | Label Marry Smith |
| Label | Label(0) | Name Caption | Label1 0 |
| Label(1) | Name Caption | Label1 0 |
Command 控件 | Command1(0) | Name Caption | Command1 减一个 |
| Command1(1) | Name Caption | Command1 减一个 |
Command 控件 | Command2(0) | Name Caption | Command2 加一个 |
| Command2(1) | Name Caption | Command2 加一个 |
第二步:编写程序代码。
程序提供的代码:
Option Explicit
Private Sub Command1_Click(Index As Integer)
If Index=0 Then
If Val(Label3(0) .Caption)>1 Then
Command1 (0) .Enabled=True
Label3(0) .Caption=Labe13(0) .Caption-1
Else
If Val(Label3(0) .Caption)=1 Then
Labe13(0) .Caption=Labe13(0) .Caption-1
End If
'Command1(0) .Enabled= ?
End If
Else
If Val(Label3(1) .Caption)>1 Then
Command1 (1) .Enabled=True
Label3(1) .Caption=Label3(1) .Caption-1
Else
If Val(Label3(1) .Caption)=1 Then
Label3(1) .Caption=Label3(1) .Caption-1
End If
Commandl(1) .Enabled=False
End If
End If
'If Val(Label3(0) .Caption) ? Val(Label3(1) .Caption) Then
picSmile(0) .Visible=True
picCry(0) .Visible=False
picCry(1) .Visible=True
picSmile(1) .Visible=False
Else
'If Val(Label3(0) .Caption) ? Val(Label3(1) .Caption) Then
picCry(0) .Visible=True
picSmile(0) .Visible=False
picSmile(1) .Visible=True
picCry(1) .Visible= False
Else
picSmile(0) .Visible=True
picCry(0) .Visible=False
picSmile(1) .Visible=True
picCry(1) .Visible=False
End If
End If
End Sub
Private Sub Command2_Click(Index As Integer)
If Index=0 Then
'Label3(0) .Caption=
Command1 (0) .Enabled=True
Else:
'Label3(1) .Caption=
Command1 (1) .Enabled=True
End If
'If Val(Label3(0) .Caption) ? Val(Label3(1) .Caption) Then
picSmile(0) .Visible=True
picCry(0) .Visible=False
picCry(1) .Visible=True
picSmile(1) .Visible=False
Else
'If Val(Label3(0) .Caption) ? Val(Label3(1) .Caption) Then
picCry(0) .Visible=True
picSmile(0) .Visible=False
picSmile(1) .Visible=True
picCry(1) .Visible=False
Else
picSmile(0) .Visible=True
picCry(0) .Visible=False
picSmile(1) .Visible=True
picCry(1) .Visible=False
End If
End If
End Sub
Private Sub Form_Load ()
PicSmile(0) .Visible=True
picSmile(1) .Visible=True
Command1(0) .Enabled=False
Command1(1) .Enabled=False
End Sub
参考代码:
Option Explicit
Private Sub Command1_Click(Index As Integer)
If Index=0 Then
If Val(Label3(0) .Caption)>1 Then
Command1 (0) .Enabled=True
Label3(0) .Caption=Label3(0) .Caption-1
Else
If Val(Label3(0) .Caption)=1 Then
Label3(0) .Caption=Label3(0) .Caption-1
End If
Command1(0) .Enabled=False
End If
Else
If Val(Label3(1) .Caption)>1 Then
Command1(1) .Enabled=True
Label3(1) .Caption=Label3(1) .Caption-1
Else
If Val(Label3(1) .Caption)=1 Then
Label3(1) .Caption=Label3(1) .Caption-1
End If
Command1(1) .Enabled=False
End If
End If
If Val(Label3(0) .Caption)>Val(Label3(1) .Caption) Then
picSmile(0) .Visible=True
picCry(0) .Visible=False
picCry(1) .Visible=True
picSmile(1) .Visible=False
Else
If Val(Label3(0) .Caption)<Val(Label3(1) .Caption) Then
picCry(0) .Visible=True
picSmile(0) .Visible=False
picSmile(1) .Visible=True
picCry(1) .Visible=False
Else
picSmile(0) .Visible=True
picCry(0) .Visible=False
picSmile(1) .Visible=True
picCry(1) .Visible=False
End If
End If
End Sub
Private Sub Command2_Click(Index As Integer)
If Index=0 Then
Label3(0) .Caption=Label3(0) .Caption+1
Command1 (0) .Enabled=True
Else:
Label3(1) .Caption=Label3(1) .Caption+1
Commandl (1) .Enabled=True
End If
If Val(Label3(0) .Caption) > Val(Label3(1) .Caption) Then
picSmile(0) .Visible = True
picCry(0) .Visible = False
picCry(1) .Visible = True
picSmile(1) .Visible = False
Else
If Val(Label3(0) .Caption)<Val(Label3(1).Caption) Then
picCry(0) .Visible=True
picSmile(0) .Visible=False
picSmiie(1) .Visible=True
picCry(1) .Visible=False
Else
picSmile(0) .Visible=True
picCry(0) .Visible=False
picSmile(1) .Visible=True
picCry(1) .Visible=False
End If
End If
End Sub
Private Sub Form_Load()
picSmile(0) .Visible=True
picSmile(1) .Visible=True
Command1(0) .Enabled=False
Command1(1) .Enabled=False
End Sub
第三步:调试并运行程序。
第四步:按题目要求存盘。