收藏 分享(赏)

VB上机练习题.doc

上传人:精品资料 文档编号:10478326 上传时间:2019-11-21 格式:DOC 页数:22 大小:614.65KB
下载 相关 举报
VB上机练习题.doc_第1页
第1页 / 共22页
VB上机练习题.doc_第2页
第2页 / 共22页
VB上机练习题.doc_第3页
第3页 / 共22页
VB上机练习题.doc_第4页
第4页 / 共22页
VB上机练习题.doc_第5页
第5页 / 共22页
点击查看更多>>
资源描述

1、11.新建工程,界面如下图,将窗体名称改为 f1,标题属性为“我的第一个工程” , Text2 允许多行,三个命令按钮名称分别为 C1、C2 、C3,单击 C1 按钮将 Text1 的字体改为黑体,Text2 的字体改为隶书。 单击 C2 按钮将 Text1 的文字颜色改为红色,Text2 的背景颜色改为蓝色。单击 C3 按钮结束程序运行。Private Sub Command1_Click()Text1.Font = 黑体End SubPrivate Sub Command2_Click()Text1.ForeColor = vbRedText2.ForeColor = vbBlueEnd

2、SubPrivate Sub Command3_Click()EndEnd Sub2.新建工程,界面如上图,运行时,鼠标在“改变窗体颜色”按钮上按下时使窗体背景色变为红色,鼠标抬起时使窗体背景色变为绿色。鼠标在窗体空白地方按下时使 Text1 的内容为“在窗体上按下了鼠标” ,鼠标抬起时使 Text1 的内容为“在窗体上抬起了鼠标” 。Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)Form1.BackColor = vbRedEnd SubPrivate

3、Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)Form1.BackColor = vbGreenEnd SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)Text1.Text = “在窗体上按下了鼠标“End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X A

4、s Single, Y As Single)Text1.Text = “在窗体上抬起了鼠标“End Sub3.新建工程,界面如下图,运行时,在文本框 Text1 中输入内容的同时,在文本框 Text2、Text3 中显示相同的内容,但字体不同(字体自定) 。Private Sub Command1_Click() Text1.Text = “Text2Text12Text2.Text = “Text3.Text = “End SubPrivate Sub Command2_Click()EndEnd SubPrivate Sub Text1_Change()Text2.Text = Text1

5、.TextText3.Text = Text1.TextEnd Sub注:“字体不同”可以在属性那调“Font”4. 设计一个窗体,如上图所示。当单击某个命令按钮时,对文本框中的文字完成相应的设置。其中,每单击一次“增大”或“缩小”按钮将使文本框中的文字增大或缩小 5 磅, “粗体”按钮将使文本框中的文字加粗, “斜体”按钮将使文本框中的文字倾斜, “下划线”按钮将使文本框中的文字加下划线, “删除线”按钮将使文本框中的文字加删除线。Private Sub Command1_Click()Text1.FontSize = Text1.FontSize + 5End SubPrivate Sub

6、 Command2_Click()Text1.FontSize = Text1.FontSize - 5End SubPrivate Sub Command3_Click()Text1.FontBold = TrueEnd SubPrivate Sub Command4_Click()Text1.FontItalic = TrueEnd SubPrivate Sub Command5_Click()Text1.FontUnderline = TrueEnd SubPrivate Sub Command6_Click()Text1.FontStrikethru = True End Sub5.

7、设计一个窗体,如下图所示。程序中将 定义成符号常量(用 Const ),运行时,输入底面半径r 和圆柱高 h,然后单击“计算”命令按钮,在相应文本框中显示计算结果。运算结果为只读。Private Sub Command1_Click()Const pi = 3.14h = Val(Text1)r = Val(Text2)Text3.Text = pi * r 2Text4.Text = 2 * pi * r * hText5.Text = pi * r 2 * hEnd Sub36. 设计界面如上图所示。单击“出题”按钮,产生任意两个【1,100】之间的随机整数于 Text1、 Text2 中

8、,单击“计算”按钮,求两个数的和。运算结果为只读。Private Sub Command1_Click()Text1.Text = Int(100 * Rnd + 1)Text2.Text = Int(100 * Rnd + 1)End SubPrivate Sub Command2_Click()a = Val(Text1.Text)b = Val(Text1.Text)Text3.Text = a + bEnd Sub7设计一个数字钟表程序,界面如图所示。运行时单击窗体,显示当前的年月日星期及时间。Private Sub Form_Click()Label2.Caption = Year(

9、Now)Label4.Caption = Month(Now)Label6.Caption = Day(Now)Label9.Caption = Weekday(Now)Label11.Caption = TimeEnd Sub8设计一个数学函数程序,界面如图所示。程序运行时,在文本框中输入任意数值,单击相应按钮标签中显示结果。文本框始终处于选中状态。Private Sub Command1_Click()Label3.Caption = Abs(Val(Text1)Text1.SetFocusText1.SelStart = 0Text1.SelLength = Len(Text1.Text

10、)End SubPrivate Sub Command2_Click()Label3.Caption = Sqr(Val(Text1)Text1.SetFocusText1.SelStart = 0Text1.SelLength = Len(Text1.Text)End Sub4Private Sub Command3_Click()Label3.Caption = Fix(Val(Text1)Text1.SetFocusText1.SelStart = 0Text1.SelLength = Len(Text1.Text)End SubPrivate Sub Command4_Click()L

11、abel3.Caption = Int(Val(Text1)Text1.SetFocusText1.SelStart = 0Text1.SelLength = Len(Text1.Text)End SubPrivate Sub Command5_Click()Label3.Caption = Sgn(Val(Text1)Text1.SetFocusText1.SelStart = 0Text1.SelLength = Len(Text1.Text)End Sub9设计一工程,在文本框中输入一整数,单击“判断”按钮,判断能否同时被 3、5、7 整除,若能用 MsgBox 函数显示 “能同时被 3

12、、5、7 整除” ,否则显示“不能整除” 。Private Sub Command1_Click()a = Val(Text1)If a Mod 3 = 0 And a Mod 5 = 0 And a Mod 7 = 0 ThenMsgBox “能同时被 3、5、7 整除“ElseMsgBox “不能整除 “End IfEnd Sub10设计一个计算购书价钱的程序,界面如下图所示。为了提高运行速度,设置 Tab 键序,使得输入完单价后,焦点定位到输入数量文本框中。单击“计算总价”或用访问键,算出总价。总价文本框要求为只读属性。完成相应功能。Private Sub Command1_Click(

13、)a = Val(Text1)b = Val(Text2)Text3.Text = a * bEnd SubPrivate Sub Command2_Click()EndEnd Sub511设计一工程,界面如上图所示。程序运行,单击“计算” ,求出对应角度的三角函数的绝对值于文本框 Text2 中,同时要求选中文本框 Text1 内容,以便继续输入。Private Sub Command1_Click()a = Val(Text1)b = a * 3.14 / 180c = Abs(Sin(b)d = Abs(Cos(b)Text2.Text = Text2.Text String(a, “*

14、“)Next iEnd SubPrivate Sub Command2_Click()Clsa = Val(InputBox(“请输入行数 “)For i = 1 To aPrint Tab(a - i + 1); String(2 * i - 1, “*“)Next iEnd SubPrivate Sub Command3_Click()Clsa = Val(InputBox(“请输入行数 “)For i = a To 1 Step -1Print Tab(a - i + 1); String(2 * i - 1, “*“)Next iEnd SubPrivate Sub Command4_

15、Click()Clsa = Val(InputBox(“请输入行数 “)For i = a To 1 Step -1Print Tab(a - i + 1); String(2 * i - 1, “*“); _Space(3); String(2 * (a - i) + 1, “*“)Next iEnd Sub26设计如下图所示界面,单击时输出 N!和 1!+2!+N!结果,要求 N!用过程实现。Function h(n As Integer) As IntegerDim s As Integers = 1For i = 1 To ns = s * iNext ih = sEnd Functi

16、onPrivate Sub Command1_Click()Dim n As Integern = Val(Text1)Text2.Text = h(n)End SubPrivate Sub Command2_Click()Dim n As Integerm = Val(Text1)11s = 0For n = 1 To ms = s + h(n)Next nText3.Text = sEnd Sub27 编写一个程序,输出一个 5*5 的矩阵,该矩阵主、负对角线上元素均为 1,其余元素均为 2,执行结果如上图所示。Option Base 1Dim A(5, 5) As IntegerPriv

17、ate Sub Form_Click()For i = 1 To 5For j = 1 To 5A(i, j) = 2If i = j Then A(i, j) = 1If i + j = 6 Then A(i, j) = 1Next j, iFor i = 1 To 5CurrentY = i * 300For j = 1 To 5CurrentX = j * 300Print A(i, j);Next j, iEnd Sub28界面如下图所示。分别在 Textl 和 Text2 中输入两个自然数 x、y,用展转相除的方法求自然数x,y 的最大公约数和最小公倍数。在窗体上显示结果。Dim a

18、 As Integer, b As Integer, x As Integer, y As Integer,r as Integer Private Sub Command1_Click()x = Val(Text1)y = Val(Text2)a = x: b = yr = x Mod yDo While r = 0 ThenFor i = 0 To List1.ListCount 1End IfEnd SubPrivate Sub Command2_Click()For i = 0 To List1.ListCount - 1List1.Selected(0) = TrueList2.Ad

19、dItem List1.TextList1.RemoveItem 0Next iEnd SubPrivate Sub Command4_Click()For i = 0 To List2.ListCount - 1List2.Selected(0) = TrueList1.AddItem List2.TextList2.RemoveItem 0Next iEnd Sub32设计界面如下图所示,运行时,当移动滚动条时,在标签中能正确显示摄氏、华氏温度值(C=5(F-32 )/9) 14Private Sub Form_Load()VScroll1.Min = 100VScroll1.Max =

20、0End SubPrivate Sub VScroll1_Change()Label2.Caption = VScroll1.ValueLabel1.Caption = Format(VScroll1.Value * 9 / 5 + 32, “#.0“)End Sub33设计上面界面,按“开始”按钮使标签在窗体上从左向右移动,当遇到窗体的右侧后,改成从右向左移动,按“停止”按钮停止标签移动。Dim a As IntegerPrivate Sub Command1_Click()Timer1.Enabled = TrueEnd SubPrivate Sub Command2_Click()Tim

21、er1.Enabled = FalseEnd SubPrivate Sub Timer1_Timer() End Sub34编写一个程序,实现在标签中自动依次显示“祝你” , “考试” , “成功” 。Dim a As IntegerPrivate Sub Timer1_Timer()a = a + 1b = a Mod 3If b = 1 Then Label1.Caption = “祝你“If b = 2 Then Label1.Caption = “考试“If b = 0 Then Label1.Caption = “成功“End Sub35窗体上有一个名为 L1 的列表框,通过属性向列

22、表框添加 3 个项目,分别是“123、456、789” 。当启动后,每次双击列表框中任何一项,则总在最后添加一项,该项是所有项之和。界面如下图。Private Sub L1_DblClick()Dim b As Double, s As Doublea = Val(L1.ListCount)s = 0For i = 0 To a15b = Val(L1.List(i)s = s + bNext iL1.AddItem sEnd Sub36设计如上界面,编写适当的事件过程。 程序运行后,每过 1 秒钟文本框的背景色能够由红到绿,绿到蓝,再由蓝到红循环变化。Dim a As IntegerPriv

23、ate Sub Timer1_Timer()a = a + 1b = a Mod 3If b = 1 Then Text1.BackColor = vbRedIf b = 2 Then Text1.BackColor = vbGreenIf b = 0 Then Text1.BackColor = vbBlueEnd Sub37单击第一个按钮,生成 20 个0,100之间的随机整数,显示于文本框 1 中,单击第二个按钮,求这 20 个数中的最大数和最小数,显示于文本框 2 中。 Option Base 1Dim a(20) As IntegerPrivate Sub Command1_Clic

24、k()Text1.Text = “For i = 1 To 20a(i) = Int(99 * Rnd - 0)Text1.Text = Text1.Text 1For i = 2 To n - 1s = “For j = 1 To i - 1A(i, j) = A(i - 1, j) + A(i - 1, j - 1)s = s A(i, 0) & s & Space(5) & A(i, i)Next iEnd Sub44编写程序,实现矩阵转置,即将一个 nm 的矩阵(均由随机函数产生,并且是两位整数)的行和列互换。单击显示按钮将原矩阵放在图片框 1,单击转置按钮将转置后的矩阵输出在图片框

25、2。Option Base 1Dim a(4, 5) As Integer, b(5, 4) As IntegerPrivate Sub Command1_Click()For i = 1 To 4s = “For j = 1 To 5a(i, j) = Int(91 * Rnd + 10)s = s & Space(2) & a(i, j)Next jPicture1.Print sNext iEnd SubPrivate Sub Command2_Click()For i = 1 To 5s = “For j = 1 To 4b(i, j) = a(j, i)s = s & Space(2

26、) & b(i, j)Next jPicture2.Print sNext iEnd Sub45编写函数过程计算 1+2+3+。 。 。+n,运行时输入 m,n,p 的值,单击“Y=”输出结果(保留三位小数) 。 如上图所示。).321().().(myFunction A(h As Integer) As IntegerDim s As Integer, i As Integers = 0For i = 1 To hs = s + iNext iA = sEnd FunctionPrivate Sub Command1_Click()Dim m As Integer, n As Intege

27、r, p As Integer, k As Doublem = Val(Text1): n = Val(Text2): p = Val(Text3)x = A(m): y = A(n): z = A(p)19k = (x + y) / zText4.Text = kEnd Sub46利用控件数组 Text1、Text2 将 Text1 中随机产生的数,交换到 Text2 中,界面如下图所示。Private Sub Form_Load()For i = 0 To 5Text1(i).Text = Int(91 * Rnd + 10)Next iEnd SubPrivate Sub Command

28、1_Click()For k = 0 To 5Text2(k).Text = Text1(5 - k).TextNext kEnd Sub47在一工程中有两窗体,界面如图所示,试完成相应功能。这是标准模块的代码:Public w As Integer这是 Form1 的代码 :Private Sub Command1_Click()n = Val(Text1)Text2.Text = n 3End SubPrivate Sub Command2_Click()Form2.ShowUnload MeEnd Sub这是 Form2 的代码 :Private Sub Form_Load()Text1

29、.Text = nEnd SubPrivate Sub Command1_Click()s = 1For i = 1 To ns = s * iNext iText2.Text = sEnd SubPrivate Sub Command2_Click()Form1.ShowUnload MeEnd Sub2048在窗体放一个文本框和四个组合框,界面如下图所示,组合框 1 和组合框 2 的颜色包括:白、黑、红、绿、蓝、黄。组合框 3 的对齐方式有左、中、右。组合框 4 字体大小有10、12、14、18、20、22 磅,试完成相应功能。Private Sub Combo1_Click()Selec

30、t Case Combo1.ListIndexCase 0Text1.ForeColor = vbWhiteCase 1Text1.ForeColor = vbBlackCase 2Text1.ForeColor = vbRedCase 3Text1.ForeColor = vbGreenCase 4Text1.ForeColor = vbBlueCase 5Text1.ForeColor = vbYellowEnd SelectEnd SubPrivate Sub Combo2_Click()Select Case Combo1.ListIndexCase 0Text1.BackColor

31、= vbWhiteCase 1Text1.BackColor = vbBlackCase 2Text1.BackColor = vbRedCase 3Text1.BackColor = vbGreenCase 4Text1.BackColor = vbBlueCase 5Text1.BackColor = vbYellowEnd SelectEnd SubPrivate Sub Combo3_Click()Select Case Combo3.ListIndexCase 0Text1.Alignment = 0Case 1Text1.Alignment = 1Case 2Text1.Align

32、ment = 2End SelectEnd SubPrivate Sub Combo4_Click()Select Case Combo4.ListIndexCase 0Text1.FontSize = 10Case 1Text1.FontSize = 12Case 2Text1.FontSize = 14Case 3Text1.FontSize = 18Case 4Text1.FontSize = 20Case 521Text1.FontSize = 22End SelectEnd Sub49用 inputBox 函数输入 10 个数,分别求出奇数和偶数之和,用消息框(Msgbox 函数)输

33、出。Private Sub Command1_Click()s1 = 0: s2 = 0For i = 1 To 10a = Val(InputBox(“请输入第 “ & i & “个数“)b = a Mod 2If b 0 Thens1 = s1 + aElses2 = s2 + aEnd IfNext iMsgBox “奇数之和为 “ & s1 & vbCrLf & “偶数之和为“ & s2End Sub50用文本框 1 输入 10 个学生的某门课程的分数后,给出五级评分。评分标准如下:优 90成绩100 良 80成绩90中 70成绩80及格 60成绩70不及格 0成绩60评分结果显示在文

34、本框 2 中,如果分数不在0,100范围内,则用消息框(Msgbox 函数)给出错误提示,并将焦点定位在输入分数的文本框。Private Sub Form_Click()Static i As Integeri = i + 1a = Val(Text1)Select Case aCase Is 100MsgBox “输入错误,请重新输入“, , “错误“Text1.SetFocusCase Is = 60Text2.Text = Text2.Text & i & Space(2) & a & Space(2) & “不及格“ & vbCrLfLabel1.Caption = “输入第“ & i

35、 & “个学生的成绩“Case Is = 70Text2.Text = Text2.Text & i & Space(2) & a & Space(2) & “及格“ & vbCrLfLabel1.Caption = “输入第“ & i & “个学生的成绩“Case Is = 80Text2.Text = Text2.Text & i & Space(2) & a & Space(2) & “中“ & vbCrLfLabel1.Caption = “输入第“ & i & “个学生的成绩“Case Is = 9022Text2.Text = Text2.Text & i & Space(2) &

36、 a & Space(2) & “良“ & vbCrLfLabel1.Caption = “输入第“ & i & “个学生的成绩“Case Is = 100Text2.Text = Text2.Text & i & Space(2) & a & Space(2) & “优“ & vbCrLfLabel1.Caption = “输入第“ & i & “个学生的成绩“End SelectEnd SubPrivate Sub Form_Load()Label1.Caption = “输入第 1 个学生的成绩“End SubPrivate Sub Form_Load()Label1.Caption = “请输入第 1 个学生的成绩“End Sub

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报