问答题

注意:下面出现的“”均为c:\wexam\25160001。
请根据以下各小题的要求设计Visual Basic应用程序(包括界面和代码)。
(1) 在名称为Form1的窗体上放置两个列表框,名称分别为List1和List2。在List1中添加“第一题”、“第二题”……“第八题”,并设置MultiSelect属性为2(要求在控件属性中设置实现)。再放置一个名称为Cmd1,标题为“复制”的命令按钮。程序运行后,如果单击“复制”按钮,将List1中选中的内容(至少两项)复制到List2中。如果选择的项数少于2项,用消息框提示“请选择至少两项”。如图17-1所示。
注意:保存时必须存放在考生文件夹下,窗体文件名为sjt1.frm,工程文件名为sjt1.vbp。
(2) 在名称为Form1的窗体上放置一个名称为Text1的文本框。程序运行后,用户在文本框中输入的英文字母一律用大写显示(要求焦点在最右端)。如图17-2所示。
注意:保存时必须存放在考生文件夹下,窗体文件名为sjt2.frm,工程文件名为sjt2.vbp。

【参考答案】

[解析] 在窗体上建立好控件后,先设置控件属性,再编写事件过程。
自列表框中添加项目可以用事件AddItem事......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
[解析] 在窗体上建立好控件后,先设置控件属性,再编写事件过程。 控件数组是由一组相同类型的控件组成,建立控件数组时系统给每个元素赋一个惟一的索引号(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 第三步:调试并运行程序。 第四步:按题目要求存盘。
问答题
[解析] 控件数组是由一组相同类型的控件组成,建立控件数组时系统给每个元素赋一个惟一的索引号(Index),例如,控件数组cmdName(C)表示控件数组名为cmdName的第D个元素;鼠标的形状由属性MousePointer来设置,该属性值为AA时对应程序运行时的鼠标形状,该属性值为C时对应文本编辑时的鼠标形状,该属性值为G时对应双向箭头的鼠标形状,该属性值为E时对应移动窗体时的鼠标形状。解题步骤: 第一步:建立界面并设置控件属性。程序中用到的控件及其属性见表AG-C。 表 AG-C 对 象 下 标 属 性 设 置 值 Label 数组 Label(0) Name Caption LabelA 程序运行 Label(A) Name Caption LabelA 文本编辑 Label(B) Name Caption LabelA 双向箭头 Label(C) Name Caption LabelA 移动窗体 第二步:编写程序代码。 程序提供的代码: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) ’FormA.=0 End Sub Private Sub LabelA_MouseMove(Index As Integer, Button As Integer, Shift _ As Integer, X As Single, Y As Single) ’Select Case Case 0 FormA.MousePointer=AA Case A FormA.MousePointer=C Case B FormA.MousePointer=G Case C FormA.MousePointer=E End Select End Sub 参考代码: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) FormA.MousePointer=0 End Sub Private Sub LabelA_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single) Select Case Index Case 0 FormA.MousePointer=AA Case A FormA.MousePointer=C Case B FormA.MousePointer=G Case C FormA.MousePointer=E End Select End Sub 第三步:调试并运行程序。 第四步:按题目要求存盘。 (B) [解析] 打开文件的命令是Open,其常用形式为:Open“文件名”For模式As[#]文件号[Len=记录长度],对文件的写操作所用的命令是Write#或Print#命令,其形式分别为:Print#文件号,[输出列表];Write#文件号,[输出列表],二者区别是Write#是以紧凑格式存放,即在数据项之间插入逗号,并给字符串加上双引号。解题步骤: 第一步:建立界面并设置控件属性。程序中用到的控件及其属性见表AG-D。 表 AG-D 对 象 属 性 设 置 值 Text Name text TextA 空 Commandbutton Name Caption CA 读入 Commandbutton Name Caption CB 计算并保存 第二步:编写程序代码。 程序提供的代码: Dim arr(A To I) As Integer Private Sub CA_Click() Dim i As Integer ’ inF.txt For Input As A For i=A To I Input #A, arr(i) TextA.Text=TextA.Text+Str(arr(i) ) Next Close #A CA.Enabled=False End Sub Private Sub CB_Click() Dim i As Integer TextA.Text= Open outF.txt For Output As A For i = A To I arr(i)=arr(i) * arr(i) ’Print #A, , TextA.Text=TextA.Text+Str(arr(i) ) Next Close #A CB.Enabled=False End Sub 参考代码: Dim arr(A To I) As Integer Private Sub CA_Click() Dim i As Integer Open inF.txt For Input As A For i=A To I Input #A, arr(i) TextA.Text=TextA.Text+str(arr(i) ) Next Close #A CA.Enabled=False End Sub Private Sub CB_Click() Dim i As Integer TextA.Text= Open outF.txt For Output As A For i=A To I arr(i)=arr(i) * arr(i) Print #A, arr(i), TextA.Text=TextA.Text+Str(arr(i) ) Next Close #A CB.Enabled=False End Sub 第三步:调试并运行程序。 第四步:按题目要求存盘。