《VB期末考试复习.doc》由会员分享,可在线阅读,更多相关《VB期末考试复习.doc(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、VB期末考试复习1、判断分数等级(if.end if 和 select case)Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim score As Integer 定义一个变量,保存分数的 score = Integer.Parse(Me.TextBox1.Text) 下面开始判断分数等级 If score 100 Or score = 90 Then MessageBox.Show
2、(优秀!) ElseIf score = 80 Then MessageBox.Show(良好!) ElseIf score = 60 Then MessageBox.Show(及格!) Else MessageBox.Show(不及格!) End If End SubEnd Class例:Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim score As Single Try 把可能
3、出错的代码写在try的下面 score = Single.Parse(Me.TextBox1.Text) Select Case score Case 100 MessageBox.Show(满分) Case 90 To 99 MessageBox.Show(优秀) Case 80 To 89 MessageBox.Show(良好) Case 60 To 79 MessageBox.Show(及格) Case 0 To 59 MessageBox.Show(不及格) Case Else MessageBox.Show(分数无效) End Select Catch ex As Exception
4、 只要有错,就把错误信息显示出来 MessageBox.Show(ex.Message) End Try End SubEnd Class2、for next 循环结构代码:Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i, s As Integer For i = 1 To 99 Step 2 s = s + i Next MsgBox(1+3+.+99= & s) End Su
5、b/求斐波那契级数 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim a, b, i As Long Dim t As Long Dim s As String a = 1 b = 1 s = a & , & b & , For i = 3 To 90 t = a + b s = s & t & , If i Mod 6 = 0 Then s = s & Chr(10) End If a = b b = t Next Ms
6、gBox(s)End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click用数组来保存斐波那契级数 Dim f(20) As Long Dim i As Integer 定义字符串变量s,初始值为“空”;作用:把各个元素连接成一个字符串 Dim s As String = 定义字符串s,作用:把各个元素连接一个字符串 f(1) = 1 f(2) = 1 计算斐波那契级数,从第项开始算 For i = 3 To 20 f(i) =
7、 f(i - 1) + f(i - 2) Next For i = 1 To 20 s = s & f(i) & , 把各元素连接起来 下面控制一行输出多少个元素 If i Mod 5 = 0 Then s = s & Chr(10) End If Next 显示结果 MsgBox(s) End Sub/九九乘法表 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim i As Integer, j As Integer Di
8、m s As String = For i = 1 To 9 For j = 1 To i s &= i & * & j & = & i * j & , Next s &= vbCrLf Next MsgBox(s) End SubEnd Class代码:Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 第一种定义数组的方法 指明了数组下标的上限 Dim a(5) As Integer 定义一
9、个数组a,最大下标为,表示可以存个元素 a(0) = 10 a(1) = 20 a(2) = 30 a(3) = 40 a(4) = 50 a(5) = 60 Dim i As Integer For i = 0 To 5 MsgBox(第 & i + 1 & 个元素的值为: & a(i) Next End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 第二种定义数组的方法 不指明数组下标的下限 Dim a() As
10、String = as, sf, fsg, gge MsgBox(元素的长度即元素的个数: & a.Length) Dim i As Integer For i = 0 To a.Length - 1 MsgBox(第 & i + 1 & 个元素的值为: & a(i) Next End SubEnd ClassPublic Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 首先判断账号是否录入 If Me.
11、TextBox1.Text = Then MessageBox.Show(请输入账号) Me.TextBox1.Focus() Exit Sub End If 接下来判断密码是否录入 If Me.TextBox2.Text.Length = 0 Then MessageBox.Show(密码不能为空,请录入!) Me.TextBox2.Focus() Exit Sub End If 最后判断帐号和密码是否合法 If Me.TextBox1.Text = And Me.TextBox2.Text = 123 Then MessageBox.Show(合法用户) Else 条件不成立,执行Else
12、下面的代码 MessageBox.Show(账号或密码有错,请联系管理员!) End If End SubEnd Class3、MDI窗体的应用(把设置父窗体的IsMdiContainer属性为true)菜单栏Text:用来指示菜单项的标题.还可以通过在要用作访问键的字符前放置一个”&”来指定访问键.或在其中输入”-”来创建菜单项为分割线Checked:设置菜单项是否被选中,True为选中,False为未选中Name:设置菜单项的名字ShortcutKeys:指定可由菜单项的快捷键Enabled:指定菜单项是否可用Visible:指定菜单项是否可见Image:为菜单项添加图标点击“查询人员信息
13、”,可以链接到查询人员信息窗体.代码:Public Class Form1 Private Sub 查看机构信息ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查看机构信息ToolStripMenuItem.Click 单击本菜单项时显示“机构信息窗体” frmDepInfo.MdiParent = Me 指定父窗体Me,它就变成子窗体了 frmDepInfo.Show() frmDepInfo.Focus() End Sub Private Sub 编辑机构
14、信息ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 编辑机构信息ToolStripMenuItem.Click frmEditDepInfo.MdiParent = Me frmEditDepInfo.Show() frmEditDepInfo.Focus() End Sub Private Sub 查看人员信息ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.Ev
15、entArgs) Handles 查看人员信息ToolStripMenuItem.Click frmCKperInfo.MdiParent = Me frmCKperInfo.Show() frmCKperInfo.Focus() End Sub Private Sub 编辑人员信息ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 编辑人员信息ToolStripMenuItem.Click frmBJperInfo.MdiParent = Me frmBJper
16、Info.Show() frmBJperInfo.Focus() End Sub Private Sub 查看帐号ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查看帐号ToolStripMenuItem.Click frmCKnum.MdiParent = Me frmCKnum.Show() frmCKnum.Focus() End Sub Private Sub 修改密码ToolStripMenuItem_Click(ByVal sender As Sy
17、stem.Object, ByVal e As System.EventArgs) Handles 修改密码ToolStripMenuItem.Click frmXGpassport.MdiParent = Me frmXGpassport.Show() frmXGpassport.Focus() End Sub Private Sub 添加帐号ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 添加帐号ToolStripMenuItem.Click frmAdd
18、Num.MdiParent = Me frmAddNum.Show() frmAddNum.Focus() End Sub Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem.Click Application.Exit() End SubEnd Class4、面向对象的类的使用使用类代码:Public Class Form1 Private Sub Button1_Click(ByVal sen
19、der As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 使用类来定义变量(这个变量一般叫对象) 定义的时候用New关键字 Dim myCat As New Cat MsgBox(这只猫还没有名字,因为还没有给它取名 & myCat.catsName) 下面给它取名字 其实就是访问属性“CatName”,用“对象名.属性名”访问 myCat.catsName = 小花 myCat.catsAge = 2 myCat.catsColur = 黑色 MsgBox(这只猫叫: & myCat.catsName &
20、 ,养了 & myCat.catsAge & 年了 & ,是 & myCat.catsColur) End SubEnd Class定义类代码:Public Class Cat 定义一个字段,private访问级别 Private CatName As String Private CatAge As Integer Private CatColur As String 定义一个属性,属性一定和某个字段相对应的 其作用:取得某字段的值或给字段赋值 Public Property catsName() As String Get Return CatName End Get Set(ByVal
21、value As String) CatName = value End Set End Property Public Property catsAge() As Integer Get Return CatAge End Get Set(ByVal value As Integer) CatAge = value End Set End Property Public Property catsColur() As String Get Return CatColur End Get Set(ByVal value As String) CatColur = value End Set E
22、nd PropertyEnd Class计算矩形的表面积和体积的类的使用使用类的过程代码:Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim c As New MyCube c.Length = Double.Parse(Me.TextBox1.Text) c.Width = Double.Parse(Me.TextBox2.Text) c.Height = Double.Parse(M
23、e.TextBox3.Text) Me.TextBox4.Text = c.tiji() Me.textbox5.text = c.Bmj() End Sub End Class定义类的过程代码:Public Class MyCube Private L As Double Private W As Double Private H As Double Public Property Length() As Double Get Return L End Get Set(ByVal value As Double) L = value End Set End Property Public P
24、roperty Width() As Double Get Return W End Get Set(ByVal value As Double) W = value End Set End Property Public Property Height() As Double Get Return W End Get Set(ByVal value As Double) H = value End Set End Property 定义计算矩形的体积的类 Public Function tiji() As Double Dim v As Double v = L * W * H Return
25、 v End Function 定义计算矩形表面积的类 Public Function Bmj() As Double Dim s As Double s = L * W * 2 + L * H * 2 + W * H * 2 Return s End FunctionEnd Class通过使用类来获取个人信息1、定义类personPublic Class person 定义私有字段 Private name As String Private age As Integer 构造无参数的函数 Public Sub New() name = 小林 age = 21 End Sub 构造两个有参数
26、的函数 Public Sub New(ByVal x As String, ByVal y As Integer) name = x age = y End Sub 通过定义function来获取信息 Public Function Getname() As String Return name End Function Public Function Getage() As Integer Return age End FunctionEnd Class2、使用类的过程代码:Public Class Form1 Private Sub Button1_Click(ByVal sender A
27、s System.Object, ByVal e As System.EventArgs) Handles Button1.Click 使用有参数的类 Dim p As New person Me.TextBox1.Text = p.Getname Me.TextBox2.Text = p.Getage End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 使用无参数的类 Dim p As New person(大民,
28、 45) Me.TextBox1.Text = p.Getname Me.TextBox2.Text = p.Getage End SubEnd Class5、如何编写精确查找和模糊查找Public Class Form1 Private Sub RecordsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Validate() Me.RecordsBindingSource.EndEdit() Me.RecordsTableAdapter.Update(
29、Me.StudentDataSet.Records) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TODO: 这行代码将数据加载到表“StudentDataSet.Records”中。您可以根据需要移动或移除它。 Me.RecordsTableAdapter.Fill(Me.StudentDataSet.Records) End Sub Private Sub Button1_Click(ByVal sender As
30、 System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Me.TextBox1.Text.Length = 0 Then MsgBox(请输入姓名!) End If Me.RecordsTableAdapter.FillByName(Me.StudentDataSet.Records, Me.TextBox1.Text) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Ha
31、ndles Button3.Click 按姓名模糊查找 Me.RecordsTableAdapter.FillBylikeName(Me.StudentDataSet.Records, % + Me.TextBox1.Text + %) End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click 按家庭住址查找 Me.RecordsTableAdapter.FillByADDr(Me.StudentDataSet.Recor
32、ds, % + Me.TextBox3.Text + %) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.RecordsTableAdapter.FillByNum(Me.StudentDataSet.Records, Me.TextBox2.Text) End SubEnd Class6、如何用vb 编写查找人员的基本信息Public Class Form1 Private Sub RecordsBin
33、dingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Validate() Me.RecordsBindingSource.EndEdit() Me.RecordsTableAdapter.Update(Me.StudentDataSet.Records) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.L
34、oad TODO: 这行代码将数据加载到表“StudentDataSet.Records”中。您可以根据需要移动或移除它。 Me.RecordsTableAdapter.Fill(Me.StudentDataSet.Records) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim str As String = Nothing str =编号like %1% 如果录了编号信息,表示只有编号作为条件 If
35、Me.TextBox1.Text.Length 0 Then str = 编号like % & Me.TextBox1.Text & % End If If Me.TextBox2.Text.Length 0 Then str = 姓名like % & Me.TextBox2.Text & % End If 如果录入了编号和姓名信息,表示这两个字段作为条件 If str Nothing And Me.TextBox2.Text.Length 0 Then 如果str=nothing,并且姓名“不为空”,那么仅把姓名作为条件 str = str & and 姓名like % & Me.TextB
36、ox2.Text & % 表示只录入姓名,但不录入编号 ElseIf Me.TextBox2.Text.Length 0 Then str = 姓名like % & Me.TextBox2.Text & % End If If str Nothing And Me.ComboBox1.Text.Length 0 Then str = str & and 性别like % & Me.ComboBox1.Text & % ElseIf Me.ComboBox1.Text.Length 0 Then str = 性别like % & Me.ComboBox1.Text & % End If filt
37、er属性为过滤条件,根据条件筛选记录 Me.RecordsBindingSource.Filter = str End SubEnd Class7、8、按手机号码查询Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TODO: 这行代码将数据加载到表“StudentDataSet.Records”中。您可以根据需要移动或移除它。 Me.RecordsTableAdapter.Fill(Me.StudentDataSet.Reco