5第3题:程序运行时在文本框中输入一段英文文字,单击命令按钮后将它们保存至文件E:\yw.txt文件中,单击窗体后,显示输入对话框,在该对话框中输入任意一个字符,并在E:\yw.txt中查找,如果找到a,则提示a是文件中第几个字符,否则提示a在文件中不存在。参考答案:
Private Sub Form_Click() Dim a As String, b As String, c As String Dim n As Integer a = InputBox("请输入一个字符") Open "e:\ yw.txt" For Input As #1 Do While Not EOF(1) Line Input #1, b c = c + b Loop n = InStr(c, a) If n <> 0 Then MsgBox a & "是文件中第" & n & "个字符" Else MsgBox a & "在文件中不存在" End If Close #1 End Sub Private Sub Command1_Click() Open "e:\ yw.txt" For Output As #1 Print #1, Text1.Text Close #1 End Sub
6.第1题:编程,将一个三位整数倒序输出.
Private Sub Command1_Click()
Dim x as integer, bw as integer, sw as integer, gw as integer
x = Text1.Text
bw = x \ 100
sw = x \ 10 Mod 10
gw = x Mod 10
x = gw * 100 + sw * 10 + bw
Print x
End Sub
第2题:编程,当程序启动后用绿色文字显示"绿灯"2秒钟,接着黄色文字显示"黄灯"3秒钟,然后用红色文字显示"红灯"2秒钟。然后,再重新开始下一轮"绿灯"、"黄灯"、"红灯"
的显示。直到双击窗体停止变化。
参考答案:
Dim k As Integer
Private Sub Form_Load()
Label1.Caption = "绿灯"
End Sub
Private Sub Timer1_Timer()
k = k + 1
Select Case k
Case 1 To 2
Label1.Caption = "绿灯"
Label1.ForeColor = vbGreen
Case 3 To 5
Label1.Caption = "黄灯"
Label1.ForeColor = vbYellow
Case 6 To 7
Label1.Caption = "红灯"
Label1.ForeColor = vbRed
If k = 7 Then k = 0
End Select
第3题:编程实现两个文本文件的合并,要求用驱动器、目录、文件列表框选择文件1,单击按钮1后,调用通用对话框选择文件2;单击按钮2后,比较文件1和文件2的内容是否相同,如果不相同则用消息框提示“两个文件不一样”,相同则提示“两个文件一样”。参考答案:
Dim fn As String
Private Sub Command1_Click()
CommonDialog1.Filter = "(*.txt)|*.txt"
CommonDialog1.ShowOpen
End Sub
Private Sub Command2_Click()
Dim s1 As String,s2 as string,flag as Boolean
Open fn For input As #1
Open CommonDialog1.FileName For Input As #2
Flag=true
Do While Not EOF(1)
Line Input #1, s1
If not eof(2) then line input #2,s2 else flag=false:exit do
If s1<>s2 then flag=false:exit do
Loop
If not eof(2) or flag=false then
Msgbox “两个文件不一样”
Else
Msgbox “两个文件一样”
End if
Close
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
If Right(File1.Path, 1) = "\" Then
fn = File1.Path + File1.FileName
Else
fn = File1.Path + "\" + File1.FileName
End If
End Sub
Private Sub Form_Load()
File1.Pattern = "*.txt"
End Sub
13.1、编写一个函数,其功能是求Σn。
参考答案:
Private Function sum(n As Integer) As Single
Dim I as integer
sum = 0
For i = 1 To n
sum = sum + i
Next i
End Function
2、编制Form_Click事件过程,单击窗体后可以将坐标原点设置到窗体的中心位置(保持单位长度不变),并且以中心点为圆心,画一个当前窗体所能容纳的最大的红、黄、蓝、绿各占1/4的圆饼图,其中红、黄、蓝、绿分别为四部分扇形的填充色。
参考答案:
Private Sub Form_Click()
Dim x As Single, y As Single, r As Single
Const pi = 3.1415926
Form1.Cls
x = Form1.ScaleWidth / 2
y = Form1.ScaleHeight / 2
Form1.Scale (-x, -y)-(x, y)
r = Form1.ScaleHeight
If Form1.ScaleHeight > Form1.ScaleWidth Then r = Form1.ScaleWidth
r = r / 2
Form1.FillStyle = 0
Form1.FillColor = vbRed
Form1.Circle (0, 0), r, , -2 * pi, -2 * pi / 4
Form1.FillColor = vbYellow
Form1.Circle (0, 0), r, , -2 * pi / 4, -pi
Form1.FillColor = vbBlue
Form1.Circle (0, 0), r, , -pi, -6 * pi / 4
Form1.FillColor = vbGreen
Form1.Circle (0, 0), r, , -6 * pi / 4, -2 * pi
End Sub
第3题:设计一个界面如下图所示的点餐程序,运行时,用户选择了食物和份数以后,单击“结帐”按钮,将会在标签中显示所需的总金额。
参考答案:
Text1(Index).Enabled = False
End If
End Sub
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then '文本框只接受数字键
KeyAscii = 0
End If
End Sub
Private Sub Command1_Click() '结帐
Dim m As Integer, i As Integer
Dim dj(0 To 4) As Integer
dj(0) = 10: dj(1) = 5: dj(2) = 8: dj(3) = 3
For i = 0 To 3
If Check1(i).Value = 1 And Val(Text1(i).Text) <> 0 Then
m = m + dj(i) * Val(Text1(i).Text)
End If
Next i
If m <> 0 Then Label1.Caption = "您共需付款:" & m & "元。谢谢!" End Sub
Private Sub Command2_Click()
'下一位
Dim i As Integer
For i = 0 To 3
Text1(i).Text = ""
Text1(i).Enabled = False
Check1(i).Value = 0
Next
Label1.Caption = ""
End Sub