收藏 分销(赏)

VB上机练习题(二).doc

上传人:快乐****生活 文档编号:10820751 上传时间:2025-06-18 格式:DOC 页数:21 大小:226.51KB 下载积分:10 金币
下载 相关 举报
VB上机练习题(二).doc_第1页
第1页 / 共21页
VB上机练习题(二).doc_第2页
第2页 / 共21页


点击查看更多>>
资源描述
VB上机练习题(二) 18、新建一个工程,完成应用程序的设计,具体要求如下: 1. 按照图11所示在窗体上放置按钮和控件; 2. 在两个列表框控件的list中输入一些内容; 3. 单击“<”按钮时,把list2中选中的一项放到list1中,并且在list2中删除该项; 4. 单击“<<”按钮时,把list2中所有的项放到list1中,并且清空list2; 5. 单击“>”按钮时,把list1中选中的一项放到list2中,并且在list1中删除该项; 6. 单击“>>”按钮时,把list1中所有的项放到list2中,并且清空list1; 7. 单击“结束”按钮时,退出应用程序。 图15 Private Sub Command1_Click() If List1.ListIndex = -1 Then MsgBox "请选择一项再按键!" Else List2.AddItem List1.Text List1.RemoveItem List1.ListIndex End If End Sub Private Sub Command2_Click() Do Until List1.ListCount = 0 List2.AddItem List1.List(0) List1.RemoveItem 0 Loop End Sub Private Sub Command3_Click() Do Until List2.ListCount = 0 List1.AddItem List2.List(0) List2.RemoveItem 0 Loop End Sub Private Sub Command4_Click() If List2.ListIndex = -1 Then MsgBox "请选择一项再按键!" Else List1.AddItem List2.Text List2.RemoveItem List2.ListIndex End If End Sub Private Sub Command5_Click() End End Sub 19、新建一个工程,完成“收款计算”程序的设计,具体要求如下: 1. 按照图16所示在窗体上放置控件; 2. 窗体上有三个文本框,上面两个分别用于输入商品单价和商品数量,单击“计算”,将应付款显示在最下面一个文本框中; 3. 最下面一个文本框(应付款)不能直接输入; 4. 单击“清除”按钮,三个文本框内容被清空,同时第一个文本框获得焦点。 图16 Private Sub Command1_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" Text1.SetFocus End Sub Private Sub Command2_Click() Dim x, y, sum! x = Val(Text1.Text) y = Val(Text2.Text) sum = x * y Text3.Text = CStr(sum) End Sub Private Sub Form_Load() Text3.Enabled = False End Sub 20、新建一个工程,完成应用程序的设计,具体要求如下: 1. 如图17所示,在窗体上放置一个水平滚动条、一个标签框和一个命令按钮; 2. 滚动条状态发生改变时,标签上的文字可以左右移动,文字移动范围等于滚动条的范围。 图17 Private Sub Command1_Click() End End Sub Private Sub Form_Load() HScroll1.Max = 6000 HScroll1.Min = 0 HScroll1.LargeChange = 40 HScroll1.SmallChange = 10 End Sub Private Sub HScroll1_Change() Label1.Left = HScroll1.Value End Sub 21、新建一个工程,完成应用程序的设计,具体要求如下: 1. 按照图18所示在窗体上放置控件; 2. 4个单选按钮分别用于显示星期、年份、月份和日期; 3. 选中某个单选按钮时,在文本框中显示当天对应的日期信息; 4. 单击“结束”命令该按钮时,退出应用程序。 图18 Private Sub Option1_Click() If Option1.Value = True Then Text1.Text = WeekdayName(Weekday(Now)) End If End Sub Private Sub Option2_Click() If Option2.Value = True Then Text1.Text = Year(Now) End If End Sub Private Sub Option3_Click() If Option3.Value = True Then Text1.Text = Month(Now) End If End Sub Private Sub Option4_Click() If Option4.Value = True Then Text1.Text = Day(Now) End If End Sub 22、新建一个工程,完成“计算平均成绩”应用程序的设计,具体要求如下: 1. 按照图19所示在窗体上放置控件; 2. 在输入或修改单科成绩的同时计算平均分,即在"高数"、"英语"和"计算机"文本框内容发生改变时,就要立即重新计算平均分,并将计算结果在“平均成绩”文本框中显示; 3. “平均成绩”文本框不允许编辑,即不能手工修改; 4. 各单科成绩文本框中只能输入数字,不能输入字母或汉字,否则给出错误提示。 图19 Dim a, b, c, sum! Private Sub Form_Load() Text4.Enabled = False End Sub Private Sub Text1_Change() a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) sum = a + b + c Text4.Text = CStr(sum) End Sub Private Sub Text2_Change() a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) sum = a + b + c Text4.Text = CStr(sum) End Sub Private Sub Text3_Change() a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) sum = a + b + c Text4.Text = CStr(sum) End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then MsgBox "请输入数字!" End Sub Private Sub Text2_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then MsgBox "请输入数字!" End Sub Private Sub Text3_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then MsgBox "请输入数字!" End Sub 23、设计一个秒表模拟程序,按下“开始”按钮,屏幕显示当前时间;按下“结束”按钮,屏幕显示结束时间和持续时间;按下“退出”按钮,结束程序。 图20 Dim a As Date, b As Date Private Sub Command1_Click() a = Now Text1.Text = Format(Now, "HH:MM:SS") End Sub Private Sub Command2_Click() b = Now Text2.Text = Format(Now, "HH:MM:SS") Text3.Text = Format(a - b, "HH:MM:SS") End Sub Private Sub Command3_Click() End End Sub 24、设计一个用来控制文本框中信息格式的程序,界面如图21所示: 图21 Private Sub Check1_Click(Index As Integer) If Check1(0).Value = 1 Then Text1.FontBold = True Else Text1.FontBold = False End If If Check1(1).Value = 1 Then Text1.FontItalic = True Else Text1.FontItalic = False End If If Check1(2).Value = 1 Then Text1.FontUnderline = True Else Text1.FontUnderline = False End If End Sub Private Sub Option1_Click(Index As Integer) Select Case Index Case 0 Text1.FontSize = 14 Case 1 Text1.FontSize = 12 Case Else Text1.FontSize = 8 End Select End Sub Private Sub Option2_Click(Index As Integer) Select Case Index Case 0 Text1.ForeColor = vbRed Case 1 Text1.ForeColor = vbGreen Case Else Text1.ForeColor = vbBlue End Select End Sub 25、新建一个工程,完成“字幕闪烁”程序的设计,程序界面如图22所示,具体要求如下: 1. 窗体的标题为“字幕闪烁”,固定边框; 2. 在属性窗口中将标签(Label1)的标题设为“祝您考试成功”,字体设置为“宋体”、“粗体”、“二号”,文字颜色为“红色”,格式设置为水平居中对齐; 3. 单击“开始”按钮(Command1),标签文字在定时器控制下自动交替以红蓝两种颜色显示,同时“开始”按钮变为“停止”按钮; 4. 单击“停止”按钮,标签文字停止闪烁,同时“停止”按钮变为“开始”按钮; 5. 定时器(Timer1)的时间间隔为0.3秒。 图22 Private Sub Command1_Click() If Command1.Caption = "开始" Then Command1.Caption = "停止" Else Command1.Caption = "开始" End If If Command1.Caption = "开始" Then Timer1.Enabled = False If Command1.Caption = "停止" Then Timer1.Enabled = True End Sub Private Sub Form_Load() Label1.ForeColor = vbRed Label1.Left = (Form1.Width - Label1.Width) / 2 Command1.Caption = "开始" Timer1.Enabled = False Timer1.Interval = 300 End Sub Private Sub Timer1_Timer() If Label1.ForeColor = vbRed Then Label1.ForeColor = vbBlue Else: Label1.ForeColor = vbRed End If End Sub 26、新建一个工程,完成“字幕滚动”程序的设计,程序界面如图23所示,具体要求如下: 1. 窗体的标题为“字幕滚动”,固定边框; 2. 在属性窗口中将标签(Label1)的标题设为“祝您考试成功”,字体设置为“宋体”、“粗体”、“二号”,文字颜色为“红色”; 3. 单击“开始”按钮,标签文字在定时器控制下自动地从左向右移动,移动速度为每个时间间隔右移100缇。当标签移动到窗体外时,再从窗体的左边进入,同时“开始”按钮变为“停止”按钮; 4. 单击“停止”按钮,标签“祝您考试成功”文字停止滚动,同时“停止”按钮变为“开始”按钮; 5. 定时器(Timer1)的时间间隔为0.1秒。 图23 Private Sub Command1_Click() If Command1.Caption = "开始" Then Command1.Caption = "停止" Else: Command1.Caption = "开始" End If If Command1.Caption = "开始" Then Timer1.Enabled = False If Command1.Caption = "停止" Then Timer1.Enabled = True End Sub Private Sub Form_Load() Label1.ForeColor = vbRed Command1.Caption = "开始" Label1.Left = (Form1.Width - Label1.Width) / 2 Timer1.Enabled = False Timer1.Interval = 100 End Sub Private Sub Timer1_Timer() If Label1.Left <= Form1.Width Then Label1.Left = Label1.Left + 100 Else: Label1.Left = -Label1.Width End If End Sub 27、新建一个工程,完成“字幕放大”程序的设计,程序界面如图24所示,具体要求如下: 1. 窗体的标题为“字幕放大”,固定边框; 2. 单击“开始”按钮(Command1),标签“欢迎光临”(Label1)文字在定时器控制下字号自动增加2,同时“开始”按钮变为“停止”按钮; 3. 单击“停止”按钮,标签“欢迎光临”文字停止放大,同时“停止”按钮变为“开始”按钮; 4. 要求标签文字在放大时保持水平居中; 5. 定时器(Timer1)的时间间隔为0.2秒。 图24 Private Sub Command1_Click() If Command1.Caption = "开始" Then Command1.Caption = "停止" Else Command1.Caption = "开始" End If If Command1.Caption = "开始" Then Timer1.Enabled = False If Command1.Caption = "停止" Then Timer1.Enabled = True End Sub Private Sub Form_Load() Label1.Left = (Form1.Width - Label1.Width) / 2 Command1.Caption = "开始" Timer1.Enabled = False Timer1.Interval = 200 End Sub Private Sub Timer1_Timer() Label1.FontSize = Label1.FontSize + 2 Label1.Left = (Form1.Width - Label1.Width) / 2 End Sub 28、新建一个工程,完成“改变大小”程序的设计,程序界面如图25所示,具体要求如下: 1. 窗体的标题为“改变大小”,固定边框; 2. 窗体上引入一个红色的圆形形状控件(Shape1),圆的直径为3400Twips; 3. 窗体的下面有一个水平滚动条(Hscroll1),将它的最大值设置为与圆的直径相同,初始值为最大值,SmallChange和LargeChange均为100Twips; 4. 水平滚动条的下面有两个标签控件,左边标签(Label1)的标题为“圆的直径为:”,右边标签(Label2)的标题为“3400”,两个标签均为固定边框; 5. 改变滚动条的值可以控制圆形直径,同时在标签Label2中动态显示圆的直径; 6. 要求圆形直径在变化时要保持圆心位置不变。 图25 Private Sub HScroll1_Change() Shape1.Width = HScroll1.Value Shape1.Height = HScroll1.Value Shape1.Left = (Form1.Width - Shape1.Width) / 2 Shape1.Top = (Form1.Height - Shape1.Height) / 2 Label2.Caption = Str(HScroll1.Value) End Sub 29、新建一个工程,完成“作图”程序的设计,程序界面如图26所示,具体要求如下: 1. 窗体的标题为“作图”,固定边框; 2. 窗体的右边是一个图片框(Picture1),用于显示图形; 3. 单击“坐标系”按钮(Command1),将图片框的坐标系统设置为原点在中央,x轴[-10,10],y轴[-10,10],并在图片框中画出该坐标系统示意图; 4. 单击“扇形”按钮(Command2),在图片框中画一个圆心在原点,半径为5,圆周为红色,线宽为2,内部为绿色,起始角为π/6,终止角为5π/6的扇形; 5. 单击“结束”按钮(Command3),程序结束运行。 图26 Private Sub Command1_Click() Picture1.Scale (-10, -10)-(10, 10) Picture1.Line (-10, 0)-(10, 0) Picture1.Line (0, -10)-(0, 10) Picture1.CurrentX = 0 Picture1.CurrentY = 0 Picture1.Print "(0,0)" End Sub Private Sub Command2_Click() Const pi = 3.1415926535 Picture1.DrawWidth = 2 Picture1.FillStyle = 0 Picture1.FillColor = RGB(0, 255, 0) Picture1.Circle (0, 0), 5, RGB(255, 0, 0), -pi / 6, -pi * 5 / 6 End Sub Private Sub Command3_Click() End End Sub 30、新建一个工程,完成“绘制图形”程序的设计,具体要求如下: 1. 窗体的标题为“绘制图形”,固定边框; 2. 按照图27所示在窗体上放置控件; 3. 窗体左侧为图片框(Picture1),用于绘制指定图形; 4. 首先在右侧框架(Frame1)中选择绘图颜色,接着在下方框架(Frame2)中选择图形种类,最后在图片框中绘制相应图形; 5. 用输入对话框输入所需参数,如绘制圆时,输入圆心坐标与半径; 6. 单击“结束”按钮(Command1),程序结束运行。 图27 Dim c As String Private Sub Option1_Click(Index As Integer) Select Case Index Case 0 c = vbRed Case 1 c = vbBlue Case Else c = vbBlack End Select End Sub Private Sub Option2_Click(Index As Integer) Select Case Index Case 0 Picture1.Cls Picture1.Line (0, 500)-(1000, 500), c Case 1 Picture1.Cls Picture1.Line (200, 200)-(500, 500), c, B Case Else Picture1.Cls X = InputBox("请输入x的值") Y = InputBox("请输入y的值") r = InputBox("请输入r的值") Picture1.Circle (X, Y), r, c End Select End Sub 31、新建一个工程,完成“倒计时”程序的设计,具体要求如下: 1. 参照图28完成界面设计。 2. 窗体的左边有一个框架Frame1,标题为“选择时间”;框架内有一组单选按钮控件数组,从上到下为Option1(0)、Option1(1)、Option1(2),标题分别为“1分钟”、“5分钟” 、“10分钟”。默认选择为1分钟。 3. 标签Label1,用于显示倒计时的剩余的时间。 4. 单击“开始计时”按钮(Command1)后,程序根据选择的时间开始倒计时,同时命令按钮变为不可使用,框架也不可使用。 5. 当剩余时间到0分0秒时,改为显示“时间到!”。 6. 定时器Timer1的时间间隔为1秒。 图28 Dim x As Integer Private Sub Command1_Click() Command1.Enabled = False Frame1.Enabled = False Timer1.Enabled = True End Sub Private Sub Form_Load() Timer1.Enabled = False Timer1.Interval = 1000 End Sub Private Sub Option1_Click(Index As Integer) Select Case Index Case 0 x = 1 * 60 Case 1 x = 5 * 60 Case 2 x = 10 * 60 End Select End Sub Private Sub Timer1_Timer() If x = 0 Then Label1.Caption = "时间到!" Timer1.Enabled = False Command1.Enabled = True Frame1.Enabled = True Else x = x - 1 Label1.Caption = Str(x \ 60) & "分" & Str(x Mod 60) & "秒" Frame1.Enabled = False Command1.Enabled = False End If End Sub 32、某公司对员工的工资进行调整:若原有工资大于等于1000元,增加工资35%;若小于1000元大于等于800元,则增加工资25%;若小于800元,则增加工资15%。请根据用户输入的原有工资,计算出增加后的工资。 图29 Private Sub Command1_Click() Text1.Text = "" End Sub Private Sub Command2_Click() End End Sub Private Sub Text1_Change() x = Val(Text1.Text) Select Case x Case Is > 1000 x = x * 1.35 Option1(0).Value = True Case 800 To 1000 x = x * 1.25 Option1(1).Value = True Case Else x = x * 1.15 Option1(2).Value = True End Select Text2.Text = CStr(x) End Sub 33、设计一个“家电提货单”管理程序,程序运行界面如下图所示。具体要求如下: 1. 单击“确定”后,根据选择的内容将清单及总价在列表框中列出。 2. “清除”按钮用于清空列表框中的项目。 3. 所有文本框只接受数字。 图30 Private Sub Command1_Click() Dim a1, a2, a3, a4, a5, a, t1, t2, t3, t4, t5, ts% t1 = Val(Text1.Text) t2 = Val(Text2.Text) t3 = Val(Text3.Text) t4 = Val(Text4.Text) t5 = Val(Text5.Text) If Check1.Value = 1 Then a1 = 3580 * t1 msg1 = "彩电" & CStr(t1) & "台" & vbCrLf End If If Check2.Value = 1 Then a2 = 660 * t2 msg2 = "微波炉" & CStr(t2) & "台" & vbCrLf End If If Check3.Value = 1 Then a3 = 1850 * t3 msg3 = "电冰箱" & CStr(t3) & "台" & vbCrLf End If If Check4.Value = 1 Then a4 = 2880 * t4 msg4 = "DVD" & CStr(t4) & "台" & vbCrLf End If If Check5.Value = 1 Then a5 = 5500 * t5 msg5 = "空调" & CStr(t5) & "台" & vbCrLf End If a = a1 + a2 + a3 + a4 + a5 ts = t1 + t2 + t3 + t4 + t5 msg6 = "共:" & CStr(ts) & "台 " & "合计金额:" & CStr(a) & "元" msg = msg1 + msg2 + msg3 + msg4 + msg5 + msg6 Text6.Text = msg End Sub Private Sub Command2_Click() Check1.Value = 0 Check2.Value = 0 Check3.Value = 0 Check4.Value = 0 Check5.Value = 0 Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text5.Text = "" Text6.Text = "" End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 End If End Sub Private Sub Text2_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 End If End Sub Private Sub Text3_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 End If End Sub Private Sub Text4_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 End If End Sub Private Sub Text5_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 End If End Sub 21
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 考试专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服