第一篇:Vb课程设计-职工信息系统.
职工信息系统设计报告
一、设计思路 1.要达到的目的
培养学生综合利用VB语言进行程序设计的能力,主要是培养学生综合使用基本控件的、文件读写、数组操作和菜单设计等能力。
2.关键问题的解决 ①设置属性
设计时要注意一些功能需要在其它功能之后才能使用,如“平均工资”、“高于平均”等按钮需要在读入数据后才能使用,设计界面时需要将相应的按钮的Enable属性为false,在读入数据后才设置Enable属性为True。
②编写代码
需要对每一个窗体编写事件处理过程。最主要的工作包含在主窗体中,代码包括事件处理过程和必要的声明。变量声明部分声明本程序所需要的变量和数组,由于单击按钮时,都要对姓名、职称、工资进行操作,因此相应的数组和变量应定义为模块级的。在编写事件处理过程代码时注意以下几点:(1定义动态数组分别用于存放姓名、职称、工资数据。
(2单击“读取数据”按钮,利用通用对话框所提供的打开标准对话框来选择要打开的数据文件,并利
用文件的基本操作将该文件中的的数据依次读入到相应数组中。注意为保留动态数组中存放的内容,在使用ReDim语句中要加上Preserve关键字。
(3计算职工的平均工资,并存放在平均工资变量,此变量应定位模块级的。
(4利用InputBox 函数来输入要追加的学生的信息。(5利用InputBox 函数来输入要删除的职工的姓名。
利用MsgBox 函数或另一个窗体来显示高于及等于(或低于平均工资和分类汇总的结果。。
二、部分程序关键源代码及注释 Dim i As Integer, aver As Single Dim a(As String, b(As String, c(As String Private Sub Command1_Click(Dim str As String CommonDialog1.Filter = “txt(*.txt|*.txt|all file(*.*|*.*” CommonDialog1.FilterIndex = 1 On Error GoTo usercancle CommonDialog1.ShowOpen usercancle: Text1 = “姓名职称工资” fileno = FreeFile Open CommonDialog1.FileName For Input As #fileno i = 1 Do While Not EOF(fileno
ReDim Preserve a(i, b(i, c(i Input #fileno, a(i, b(i, c(i str = a(i & “ ” & b(i & “ ” & c(i Text1 = Text1 + vbNewLine + str i = i + 1 Loop Close #fileno i = i1 Print i aver = sum / i s1 = “职工的平均工资为:” & aver s2 = “职工总人数:” & i MsgBox s1 & vbCrLf & s2, , “zh1” Command3.Enabled = True Command4.Enabled = True End Sub Private Sub Command3_Click(Dim x As Integer, l1 As String, m1 As String Form2.Text1 = “姓名职称工资” Form2.Text1 = Form2.Text1 & vbNewLine & “--------------------------” x = 0 Print i For i = 1 To i If c(i >= aver Then x = x + 1
l1 = a(i & “ ” & b(i & “ ” & c(i Form2.Text1 = Form2.Text1 & vbNewLine & l1 End If Next i i = i1 Print i Form2.Text1 = Form2.Text1 & vbNewLine & “--------------------------” m2 = “工资小于平均工资的人数” & y Form2.Text1 = Form2.Text1 & vbNewLine & m2 Form2.Caption = “工资小于平均工资的人数” Load Form2 Form2.Show End Sub Private Sub Command5_Click(Dim n1 As Integer, n2 As Integer, n3 As Integer, n4 As Integer Dim str1 As String, str2 As String, str3 As String, str4 As String For i = 1 To i
If a(i = “教授” Then n1 = n1 + 1 ElseIf b(i = “副教授” Then n2 = n2 + 1 ElseIf c(i = “讲师” Then n3 = n3 + 1 Else n4 = n4 + 1 End If Next i i = i1 a(i = a(i + 1 b(i = b(i + 1 c(i = c(i + 1 Next i i = i1 Print i End Sub Private Sub manu1_Click(Dim m1 As Integer, m2 As Integer m1 = InputBox(“请输入工资下限” m2 = InputBox(“请输入工资上限” Form2.Text1 = “姓名职称工资” r = 1 For i = 1 To i If c(i >= m1 And c(i <= m2 Then r = r + 1 str5 = a(i & “ ” & b(i & “ ” & c(i Form2.Text1 = Form2.Text1 & vbCrLf & str5 End If Next i i = i1 For j = i + 1 To g If Val(c(i > Val(c(j Then t = c(i c(i = c(j c(j = t t = a(i a(i = a(j a(j = t t = b(i b(i = b(j
b(j = t End If Next j Next i i = i1 For j = i + 1 To g If Val(c(i < Val(c(j Then
t = c(i c(i = c(j c(j = t t = a(i a(i = a(j a(j = t t = b(i b(i = b(j b(j = t End If Next j Next i i = i-1 Form2.Text1 = “姓名职称工资” For i = 1 To i str7 = a(i & “ ” & b(i & “ ” & c(i Form2.Text1 = Form2.Text1 & vbNewLine & str7 Next i Form2.Caption = “降序排序” Load Form2
Form2.Show End Sub Private Sub manu5_Click(End End Sub
三、设计方案的完善及目前存在的问题 1.设计方案要完善的地方 ①对编程语言的进一步完善 ②对程序的调用 ③程序不够人性化 2.目前存在的问题 ①编程语言不够流畅。
四、本次设计的收获及心得体会
对VB理解不深刻,知识匮乏,但这是颇有实际用处的,认真学习vb,不断认识自己的不足,并不断改进。认真学习科学知识,在学习中充分实践。把运用与学习结合在一起。
五、对该题目和VB设计的意见和建议 1.对该题目的意见和建议 该程序应该充分与现实结合。
让程序更容易让顾客进行操作。更加人性化。
2.对本次设计的意见和建议
设计课程比较实用,但颇有难度,希望以后能给我们较多时间进行设计,并请老师多帮助解决疑难问题,谢谢。
第二篇:VB 课程设计
学生成绩管理系统一、功能描述
首先简单地阐述本人所要实现系统的目的,然后应较详细的叙述本系统所要实现的功能。
************ *******
二、概要设计
1、系统功能模块框图设计
根据功能描述,建立系统的体系结构,即将整个系统分解成若干子功能模块,并用框图表示各功能模块之间的衔接关系,并简要说明各模块的功能。
程序流程框图如图所示:(仅供参考)
图1 程序流程图
2、数据库设计
包括数据库的组成说明,数据库中各表的结构说明(包括字段名、字段类型等),可以包含图片例如下表:
表1 学生信息表
三、详细设计
详细说明主要窗体的实现过程,包括窗体的设计图片及其上所包含的控件名和属性设置;
详细说明主要操作的代码实现过程,包括过程间的调用关系(注:在此给出不完全代码)。如下所示:
1、账号登录
帐号登录模块中登录与注册两按钮,单击注册会进入注册模块中,单击登录会进入系统进行下一步的操作,帐号文本框与数据库相连接,当在帐号文本框输入帐号后点击登录按钮后,会在数据库中进行查询看数据库中是否会有该帐号,且该帐号的密码是否与密码框中输入的相同,如果相同会进入系统,如果不存在该账号或密码与帐号不相匹配,则会提示“帐号或密码错误”,帐号文本框会得到焦点,让重新输入。
(1)窗体截图(如图2)
图2 注册窗体
(2)功能实现
2、学生信息查询窗体
图3 查询窗体
四、调试中存在的问题及解决方法
包括调试过程中遇到的主要问题及采取的解决方法。例如:
1、注册账号出现的错误
数据库中帐号被设为主键,当注册时的帐号与数局库中的帐号有相同时就会出错。
2、解决方法:在写入数据库前先进行查询数据库
五、心得体会
本人在本次实习中取得的收获、遇到的困难、如何面对和克服各种困难、本系统的不足之处,将要改进的思想等。
六、参考文献
[1] 罗朝盛.Visual Basic 6.0程序设计教程(第3版).人民邮电出版社,2009年2月
[2] [3]
七、附录
包括全部源程序清单(程序中应有足够的注释)。
第三篇:VB课程设计
课程设计
学 院 班 级
指导老师
姓 名 学 号 成 绩
2013年 月 10
日
一.课程设计题目
学生管理系统
二.功能描述
按设定的用户名密码进行登录后,可按需求进行对数据库中已有的学生信息及成绩进行查询,添加,删除,修改等操作。
三.概要设计
登录界面功能:让用户进行登录。主界面功能:让用户选择进行哪项操作。
查询学生信息菜单功能:输入学生学号,对该学生的个人信息以
及成绩进行查询,若直接点击查询按钮,则显示全部学生个人信息及成绩。
编辑学生信息菜单功能:可对数据库中已有的学生信息进行查
询,添加,删除,修改等操作。
学生信息维护菜单功能:可对数据库中已有的学生成绩进行查
询,添加,删除,修改等操作。
四.详细设计
五.效果以及存在问题
说明系统的运行效果(附上运行界面图片)、存在哪些不足以及预期的解决办法。
登录界面
登录学生管理系统
按查询条件查询
打开修改学生信息子窗口
学生成绩维护子窗口
垂直窗口排列
关于窗口
不足:1.没有实现用户登录界面中的注册功能
2.没有实现主界面中文件下的新建和保存功能。
六.心得体会
经过几天的苦战,终于把课程设计粗略的做完了。刚开始做的时候,毫无头绪,一片茫然。好在书本上有类似的实验,可以供参考。尽管如此,真正做起项目来,还是有着这样那样的困难,有时一个空格少打都会出错,导致我检查了半天才发现问题所在。由于VB语法还运用不熟练,很多代码都是模仿书本上的敲的,许多代码往往能编译通过,但很多都会抛出各种异常,让人头疼。有时系统就给出个有个参数没有被指定值,就可以让人搞的焦头烂额。因此,写程序真的也很需要强大的耐心,只要熬下去了,当你做出程序时,也会倍有成就感。目前,程序是基本写完了,平常学的理论都得到了运用,感觉自己的能力也增强了。一句话,实践才是硬道理。
七.附录代码
(附上各模块代码)
1.登录界面
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Static n As Integer
If txtName.Text = “abc” And txtPassWord.Text = “123” Then
Dim form2 As New Form2()
form2.Show()
Me.Close()
Else
n = n + 1
If n < 5 Then
MsgBox(“请输入正确的用户名和密码!”, MsgBoxStyle.Critical)
Else
MsgBox(“请输入正确的用户名和密码!”, MsgBoxStyle.Critical)
Button1.Enabled = False
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button2.Click
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button3.Click
Dim form3 As New Form3()
form3.Show()
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load
End Sub End Class
2.查询学生菜单代码
Public Class Form6
Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load
Dim objConn As New OleDb.OleDbConnection
Dim objDaXS_KC As New OleDb.OleDbDataAdapter
Dim objDaXS As New OleDb.OleDbDataAdapter
Dim objCommXS_KC As New OleDb.OleDbCommand
Dim objCommXS As New OleDb.OleDbCommand
Dim objDsXS_KC As New DataSet
Dim objDsXS As New DataSet
objConn.ConnectionString = “Provider = Microsoft.jet.OLEDB.4.0;data source = 'D:XSCJ.mdb'”
objCommXS.CommandText = “ Select * From XS ”
objCommXS_KC.CommandText = “ Select * From XS_KC ”
objCommXS_KC.Connection = objConn
objDaXS_KC.SelectCommand = objCommXS_KC
objCommXS.Connection = objConn
objDaXS.SelectCommand = objCommXS
objConn.Open()
objDaXS_KC.Fill(objDsXS_KC, “XS_KC”)
objDaXS.Fill(objDsXS, “XS”)
objConn.Close()
DataGridView1.DataSource = objDsXS_KC.Tables(“XS_KC”)
DataGridView2.DataSource = objDsXS.Tables(“XS”)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Dim objConn As New OleDb.OleDbConnection
Dim objDaXS_KC As New OleDb.OleDbDataAdapter
Dim objDaXS As New OleDb.OleDbDataAdapter
Dim objCommXS_KC As New OleDb.OleDbCommand
Dim objCommXS As New OleDb.OleDbCommand
Dim objDsXS_KC As New DataSet
Dim objDsXS As New DataSet
Dim WhereStr As String
WhereStr = “"
If Trim(TxtXH.Text)<> ”“ Then
WhereStr = ” 学号like “ + Trim(TxtXH.Text)
End If
objConn.ConnectionString = ”Provider = Microsoft.jet.OLEDB.4.0;data source = 'D:XSCJ.mdb'“
objCommXS.CommandText = ” Select * From XS “
objCommXS_KC.CommandText = ” Select * From XS_KC “
If WhereStr <> ”“ Then
objCommXS.CommandText = objCommXS.CommandText & ”where“ & WhereStr
objCommXS_KC.CommandText = objCommXS_KC.CommandText & ”where“ & WhereStr
End If
objCommXS_KC.Connection = objConn
objDaXS_KC.SelectCommand = objCommXS_KC
objCommXS.Connection = objConn
objDaXS.SelectCommand = objCommXS
objConn.Open()
objDaXS_KC.Fill(objDsXS_KC, ”XS_KC“)
objDaXS.Fill(objDsXS, ”XS“)
objConn.Close()
DataGridView1.DataSource = objDsXS_KC.Tables(”XS_KC“)
DataGridView2.DataSource = objDsXS.Tables(”XS“)
End Sub End Class
3编辑学生信息菜单
Public Class Form7
Dim objDs As New DataSet
Dim objxstable As DataTable
Dim objda As New OleDb.OleDbDataAdapter
Dim wherestr As String
Public Sub refreshdata()
objda.Update(objDs, ”XS“)
objxstable.Clear()
bindgridview(wherestr)
End Sub
Public Sub bindgridview(ByVal strXH As String)
Dim objConn As New OleDb.OleDbConnection
Dim objComm As New OleDb.OleDbCommand
WhereStr = ”“
If Trim(TxtXH.Text)<> ”“ Then
wherestr = ” 学号like“ + Trim(TxtXH.Text)
End If
objConn.ConnectionString = ”Provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:XSCJ.mdb'“
objComm.CommandText = ” Select * From XS “
If WhereStr <> ”“ Then
objComm.CommandText = objComm.CommandText & ”where“ & WhereStr
End If
'把objConn设置为objConn的数据连接
objComm.Connection = objConn
objda.SelectCommand = objComm
'创建INSERT Command
Dim InsCommand As New OleDb.OleDbCommand(”INSERT INTO XS(学号,姓名,专业名,性别,出生日期,总学分,备注)VALUES(?,?,?,?,?,?,?)“, objConn)
InsCommand.Parameters.Add(”学号“, OleDb.OleDbType.VarChar, 6, ”学号“)
InsCommand.Parameters.Add(”姓名“, OleDb.OleDbType.VarChar, 8, ”姓名“)
InsCommand.Parameters.Add(”专业名“, OleDb.OleDbType.VarChar, 10, ”专业名“)
InsCommand.Parameters.Add(”性别“, OleDb.OleDbType.VarChar, 2, ”性别“)
InsCommand.Parameters.Add(”出生日期“, OleDb.OleDbType.Date, Nothing, ”出生日期“)
InsCommand.Parameters.Add(”总学分“, OleDb.OleDbType.Integer, Nothing, ”总学分“)
InsCommand.Parameters.Add(”备注“, OleDb.OleDbType.VarChar, Nothing, ”备注“)
objda.InsertCommand = InsCommand
'创建Delte Command
Dim delcommand As New OleDb.OleDbCommand(”DELETE * from XS where 学号=?“, objConn)
delcommand.Parameters.Add(”学号“, OleDb.OleDbType.Char, 6, ”学号“)
objda.DeleteCommand = delcommand
'创建Update Command
Dim updCommand As New OleDb.OleDbCommand(”UPDATE XS SET 学号=?,姓名=?,专业名=?,性别=?,出生日期=?,总学分=?,备注=? WHERE 学号=?“, objConn)
updCommand.Parameters.Add(”学号“, OleDb.OleDbType.VarChar, 6, ”学号“)
updCommand.Parameters.Add(”姓名“, OleDb.OleDbType.VarChar, 8, ”姓名“)
updCommand.Parameters.Add(”专业名“, OleDb.OleDbType.VarChar, 10, ”专业名“)
updCommand.Parameters.Add(”性别“, OleDb.OleDbType.VarChar, 2, ”性别“)
updCommand.Parameters.Add(”出生日期“, OleDb.OleDbType.Date, Nothing, ”出生日期“)
updCommand.Parameters.Add(”总学分“, OleDb.OleDbType.Integer, Nothing, ”总学分“)
updCommand.Parameters.Add(”备注“, OleDb.OleDbType.VarChar, Nothing, ”备注“)
updCommand.Parameters.Add(”学号“, OleDb.OleDbType.VarChar, 6, ”学号“)
objda.UpdateCommand = updCommand
objConn.Open()
'打开数据库连接
objda.Fill(objDs, ”XS“)'填充数据
objxstable = objDs.Tables(”XS“)
objConn.Close()
'关闭数据集
'把DataGrid1的DataSource属性设置为刚刚取到的数据表,这样就可以显示数据了
DataGridView1.DataSource = objDs.Tables(”XS“)
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)Handles DataGridView1.CellContentClick
TxtStuXH.Text = DataGridView1.CurrentRow.Cells.Item(0).Value.ToString '显示学号
TxtStuXM.Text = DataGridView1.CurrentRow.Cells.Item(1).Value.ToString '显示姓名
TxtZY.Text = DataGridView1.CurrentRow.Cells.Item(2).Value.ToString '显示专业
TxtXB.Text = DataGridView1.CurrentRow.Cells.Item(3).Value.ToString '显示性别
TxtCSRQ.Text = DataGridView1.CurrentRow.Cells.Item(4).Value.ToString '显示出生日期
TxtStuZXF.Text = DataGridView1.CurrentRow.Cells.Item(5).Value.ToString '显示总学分
TxtBZ.Text = DataGridView1.CurrentRow.Cells.Item(6).Value.ToString '显示备注
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Dim objConn As New OleDb.OleDbConnection
Dim objDaXS As New OleDb.OleDbDataAdapter
Dim objCommXS As New OleDb.OleDbCommand
Dim objDsXS As New DataSet
Dim WhereStr As String
WhereStr = ”“
If Trim(TxtXH.Text)<> ”“ Then
WhereStr = ” 学号like “ + Trim(TxtXH.Text)
End If
objConn.ConnectionString = ”Provider = Microsoft.jet.OLEDB.4.0;data source = 'D:XSCJ.mdb'“
objCommXS.CommandText = ” Select * From XS “
If WhereStr <> ”“ Then
objCommXS.CommandText = objCommXS.CommandText & ”where“ & WhereStr
End If
objCommXS.Connection = objConn
objDaXS.SelectCommand = objCommXS
objConn.Open()
objDaXS.Fill(objDsXS, ”XS“)
objConn.Close()
DataGridView1.DataSource = objDsXS.Tables(”XS“)
End Sub
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load
wherestr = ”“
bindgridview(wherestr)
End Sub
Private Sub butAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles butAdd.Click
Dim response As MsgBoxResult
response = MsgBox(”确定要添加记录吗?“, vbOKCancel + vbQuestion, ”系统提示“)
If response = MsgBoxResult.Ok Then
Dim myRow As DataRow = objxstable.NewRow()
myRow(”学号“)= TxtStuXH.Text
myRow(”姓名“)= TxtStuXM.Text
myRow(”专业名“)= TxtZY.Text
myRow(”性别“)= TxtXB.Text
myRow(”出生日期“)= TxtCSRQ.Text
myRow(”总学分“)= TxtStuZXF.Text
myRow(”备注“)= TxtBZ.Text
objxstable.Rows.Add(myRow)
refreshdata()
End If
End Sub
Private Sub butEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles butEdit.Click
Dim response As MsgBoxResult
response = MsgBox(”确定要修改记录吗?“, vbOKCancel + vbQuestion, ”系统提示“)
If response = MsgBoxResult.Ok Then '用户选择“确定”
'修改学号
objxstable.Rows.Item(DataGridView1.CurrentRow.Index).Item(0)= TxtStuXH.Text
'修改姓名
objxstable.Rows.Item(DataGridView1.CurrentRow.Index).Item(1)= TxtStuXM.Text
'修改专业
objxstable.Rows.Item(DataGridView1.CurrentRow.Index).Item(2)= TxtZY.Text
'修改性别
objxstable.Rows.Item(DataGridView1.CurrentRow.Index).Item(3)= TxtXB.Text
'修改出生日期
objxstable.Rows.Item(DataGridView1.CurrentRow.Index).Item(4)= TxtCSRQ.Text
'修改总学分
objxstable.Rows.Item(DataGridView1.CurrentRow.Index).Item(5)= TxtStuZXF.Text
'修改备注
objxstable.Rows.Item(DataGridView1.CurrentRow.Index).Item(6)= TxtBZ.Text
refreshdata()'更新并刷新显示
End If
End Sub
Private Sub butDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles butDelete.Click
Dim response As MsgBoxResult
response = MsgBox(”确定要删除记录吗?“, vbOKCancel + vbQuestion, ”系统提示“)
If response = MsgBoxResult.Ok Then '用户选择“确定”
'除学生成绩表当前记录
objxstable.Rows.Item(DataGridView1.CurrentRow.Index).Delete()
refreshdata()'更新并刷新显示
End If
End Sub End Class
4.学生成绩维护菜单
Public Class Form4
Dim objDs As New DataSet
Dim objXSTable As DataTable
Dim objDa As New OleDb.OleDbDataAdapter
Dim WhereStr As String
Public Sub refreshdata()
objDa.Update(objDs, ”XS_KC“)
objXSTable.Clear()
bindgridview(WhereStr)
End Sub
Public Sub bindgridview(ByVal strXH As String)
Dim objConn As New OleDb.OleDbConnection
Dim objComm As New OleDb.OleDbCommand
WhereStr = ”“
If Trim(TxtXH.Text)<> ”“ Then
WhereStr = ” 学号like“ + Trim(TxtXH.Text)
End If
objConn.ConnectionString = ”Provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:XSCJ.mdb'“
objComm.CommandText = ” Select * From XS_KC “
If WhereStr <> ”“ Then
objComm.CommandText = objComm.CommandText & ”where“ & WhereStr
End If
'把objConn设置为objConn的数据连接
objComm.Connection = objConn
objDa.SelectCommand = objComm
'创建INSERT Command
Dim InsCommand As New OleDb.OleDbCommand(”INSERT INTO XS_KC(学号,课程号,成绩,学分)VALUES(?,?,?,?)“, objConn)
InsCommand.Parameters.Add(”学号“, OleDb.OleDbType.VarChar, 6, ”学号“)
InsCommand.Parameters.Add(”课程号“, OleDb.OleDbType.VarChar, 4, ”课程号“)
InsCommand.Parameters.Add(”成绩“, OleDb.OleDbType.Integer, Nothing, ”成绩“)
InsCommand.Parameters.Add(”学分“, OleDb.OleDbType.Integer, Nothing, ”学分“)
objDa.InsertCommand = InsCommand
'创建Delte Command
Dim delcommand As New OleDb.OleDbCommand(”DELETE * from XS_KC where 序号=?“, objConn)
delcommand.Parameters.Add(”序号“, OleDb.OleDbType.Char, 6, ”序号“)
objDa.DeleteCommand = delcommand
'创建Update Command
Dim updCommand As New OleDb.OleDbCommand(”UPDATE XS_KC set 学号=?,课程号=?,成绩=?,学分=? where 序号=?“, objConn)
updCommand.Parameters.Add(”学号“, OleDb.OleDbType.VarChar, 6, ”学号“)
updCommand.Parameters.Add(”课程号“, OleDb.OleDbType.VarChar, 4, ”课程号“)
updCommand.Parameters.Add(”成绩“, OleDb.OleDbType.Integer, Nothing, ”成绩“)
updCommand.Parameters.Add(”学分“, OleDb.OleDbType.Integer, Nothing, ”学分“)
updCommand.Parameters.Add(”序号“, OleDb.OleDbType.Integer, Nothing, ”序号“)
objDa.UpdateCommand = updCommand
objConn.Open()
'打开数据库连接
objDa.Fill(objDs, ”XS_KC“)'填充数据
objXSTable = objDs.Tables(”XS_KC“)
objConn.Close()
'关闭数据集
'把DataGrid1的DataSource属性设置为刚刚取到的数据表,这样就可以显示数据了
DataGridView1.DataSource = objDs.Tables(”XS_KC“)
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)Handles DataGridView1.CellContentClick
txtStuXKID.Text = DataGridView1.CurrentRow.Cells.Item(0).Value.ToString '显示ID
txtStuXH.Text = DataGridView1.CurrentRow.Cells.Item(1).Value.ToString '显示学号
txtStuKCH.Text = DataGridView1.CurrentRow.Cells.Item(2).Value.ToString '显示课程号
txtStuCJ.Text = DataGridView1.CurrentRow.Cells.Item(3).Value.ToString '显示成绩
txtStuXF.Text = DataGridView1.CurrentRow.Cells.Item(4).Value.ToString '显示学分
End Sub
Private Sub btnDdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles butAdd.Click
Dim response As MsgBoxResult
response = MsgBox(”确定要添加记录吗?“, vbOKCancel + vbQuestion, ”系统提示“)
If response = MsgBoxResult.Ok Then
Dim myRow As DataRow = objXSTable.NewRow()
myRow(”学号“)= txtStuXH.Text
myRow(”课程号“)= txtStuKCH.Text
myRow(”成绩“)= txtStuCJ.Text
myRow(”学分“)= txtStuXF.Text
objXSTable.Rows.Add(myRow)
refreshdata()
End If
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles butEdit.Click
Dim response As MsgBoxResult
response = MsgBox(”确定要修改记录吗?“, vbOKCancel + vbQuestion, ”系统提示“)
If response = MsgBoxResult.Ok Then '用户选择“确定”
'修改学号
objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(1)= txtStuXH.Text
'修改课程号
objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(2)= txtStuKCH.Text
' 修改成绩
objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(3)= txtStuCJ.Text
'修改学分
objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(4)= txtStuXF.Text
refreshdata()'更新并刷新显示
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles butDelete.Click
Dim response As MsgBoxResult
response = MsgBox(”确定要删除记录吗?“, vbOKCancel + vbQuestion, ”系统提示“)
If response = MsgBoxResult.Ok Then '用户选择“确定”
'除学生成绩表当前记录
objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Delete()
refreshdata()'更新并刷新显示
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Dim objConn As New OleDb.OleDbConnection
Dim objDaXS As New OleDb.OleDbDataAdapter
Dim objCommXS As New OleDb.OleDbCommand
Dim objDsXS As New DataSet
Dim WhereStr As String
WhereStr = ”“
If Trim(TxtXH.Text)<> ”“ Then
WhereStr = ” 学号like “ + Trim(TxtXH.Text)
End If
objConn.ConnectionString = ”Provider = Microsoft.jet.OLEDB.4.0;data source = 'D:XSCJ.mdb'“
objCommXS.CommandText = ” Select * From XS_KC “
If WhereStr <> ”“ Then
objCommXS.CommandText = objCommXS.CommandText & ”where“ & WhereStr
End If
objCommXS.Connection = objConn
objDaXS.SelectCommand = objCommXS
objConn.Open()
objDaXS.Fill(objDsXS, ”XS_KC“)
objConn.Close()
DataGridView1.DataSource = objDsXS.Tables(”XS_KC“)
End Sub
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load
WhereStr = ”"
bindgridview(WhereStr)
End Sub End Class
5.主菜单界面代码
Public Class Form2
Private Sub quitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles quitMenuItem.Click
Me.Close()
End Sub
Private Sub importscoreMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles importscoreMenuItem.Click
Dim form4 As New Form4
form4.MdiParent = Me
form4.WindowState = FormWindowState.Normal
form4.Show()
End Sub
Private Sub updateinfoMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles updateinfoMenuItem.Click
Dim form7 As New Form7
form7.MdiParent = Me
form7.WindowState = FormWindowState.Normal
form7.Show()
End Sub
Private Sub selectinfoMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles selectinfoMenuItem.Click
Dim form6 As New Form6
Form6.MdiParent = Me
form6.WindowState = FormWindowState.Normal
form6.Show()
End Sub
Private Sub cascadeMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles cascadeMenuItem.Click
Me.LayoutMdi(MdiLayout.Cascade)
End Sub
Private Sub horizonMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles horizonMenuItem.Click
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub
Private Sub verticalMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles verticalMenuItem.Click
Me.LayoutMdi(MdiLayout.TileVertical)
End Sub
Private Sub aboutMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles aboutMenuItem.Click
Dim form5 As New Form5
form5.MdiParent = Me
form5.WindowState = FormWindowState.Normal
form5.Show()
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles ToolStripButton1.Click
selectinfoMenuItem.PerformClick()
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles ToolStripButton2.Click
updateinfoMenuItem.PerformClick()
End Sub
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles ToolStripButton3.Click
importscoreMenuItem.PerformClick()
End Sub
Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles ToolStripButton4.Click
cascadeMenuItem.PerformClick()
End Sub
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles ToolStripButton5.Click
aboutMenuItem.PerformClick()
End Sub
Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles ToolStripButton6.Click
quitMenuItem.PerformClick()
End Sub
Private Sub ToolStripStatusLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles ToolStripStatusLabel1.Click
aboutMenuItem.PerformClick()
End Sub End Class
八.参考文库
百度文库,百度知道
第四篇:vb课程设计
Visual Basic课程设计报告
题目1: 贪吃蛇 难度:**** 题目2: 难度: 指导教师:
姓 名: 学 号: 专 业: 班 级: 学 院:
日 期:
目录
1、题目简介...................................................................................................错误!未定义书签。
2、设计思路与总体方案.................................................................................................................3
2.1 窗体Form1.vb【设计】...................................................................................................3 2.2窗体Form2.vb【设计】..................................................................................................11 2.3窗体Form3.vb【设计】..................................................................................................14 2.4 窗体Form4.vb【设计】.................................................................................................14 2.5窗体Form5.vb【设计】....................................................................................................2.6窗体Form6.vb【设计】..................................................................错误!未定义书签。2.6 标准模块..........................................................................................................................14
3、设计过程遇到的主要问题及解决方法...................................................................................16
4、设计中尚存的不足分析...........................................................................................................16
5、心得体会...................................................................................................................................17
6、参考文献...................................................................................................................................17
1.题目简介
贪吃蛇是一种很流行的电子游戏。如图1所示,一条蛇在棋盘状的格子平面上爬行,由键盘控制其转弯;格子上随机出现五个数字(1~5),当蛇的头部碰到数字时会将数字“吃掉”,蛇的身体会变长。当蛇的头撞到障碍物时或碰到自己身体时,游戏结束。游戏结束时,蛇身体越长的分越高。
图1 程序启动时界面如图1所示,游戏平面有20*20个方格构成。使用回车键可以暂
停(如图2所示),蛇爬到边界时,如果未遇到障碍物,会从一侧爬出再从另一侧爬入。如果运动过程中,其头部碰到障碍物或自己,则被撞死,游戏结束。
图2 如果该用户的成绩超过记录成绩,则会显示图3所示的对话框提示输入用户名,以供记录成绩,排行榜界面如图4所示。
图3
图4
选择“设置”菜单命令,打开图5所示的“设置”对话框。在该对话框中可以选
择不同的障碍物形状,也可指定背景和障碍物颜色。
图5 为了使效果更好,我加了背景音乐和速度控制。
2、设计思路与总体方案
(说明用到哪些控件并进行难点分析,说明所使用到的模块、过程、变量、数组、自定义数据类型及其相互关系,必要时给出系统结构图等图表。
对如下事项逐步进行必要的分析、说明:
程序设计的主要思想,程序功能是如何实现的?
设计了几个程序模块(有几个窗体模块、几个标准模块),各个功能模块的作者及想到之间的关系(模块间是如何传递信息和数据的,采用什么方式,为什么?)?
程序中定义了哪些主要的变量(分别定义了哪些过程级变量?模块级变量和全局变量),这些变量起什么作用?是否全局变量? 这里不需要给出大段的源程序,只需要给出关键的实现代码即可。)2.1 窗体Form1.vb【设计】
这是主界面,但也是最难的环节,之后要用到的控件与显示都要在其基础上发展。首先我们要将背景网格和蛇身画出来,由于受到了动态添加控件的启发,我用了powerpacks中的ovalshape来构造蛇身,lineshape来构造线,为了满足蛇的身体长长,用到了数组。由于代码之间有许多调配,在此我所举的代码皆主要起说明作用,实际情况可看源代码。代码如下:
Public num As Integer = 4 ‘记录蛇的长度,即ovalshape的个数
Public j As String‘记录Inputstring的字符串
Public p As Integer = 1‘记录MsgBox所产生的值
Dim w As Integer
Dim d As Integer ‘为键盘方向确定一固定值
Dim recordname As String‘记录玩家名字
Dim bnum As Integer = 1‘记录障碍物的个数
Public recordlist(7)As String ‘记录成绩
Public recordscore(7)As Integer
Public gametime As Integer ‘记录游戏进行所用时间
Public gameinterval As Integer = 200 ‘记录游戏Timer控件interval值
Public score As Integer ‘记录得分
Public direction As Integer
Public line(41)As LineShape
Public food(4)As RectangleShape
Public eye(2)As OvalShape '蛇的眼睛
Public a(num)As OvalShape '蛇的身体
Public word(4)As Label
Imports Microsoft.VisualBasic.PowerPacks Public a(num)As OvalShape ‘a()为定义蛇身的数组,变量num用来记录蛇身个数
Public line(41)As LineShape Dim canvas = New Microsoft.VisualBasic.PowerPacks.ShapeContainer
For i = 4 To 0 Step-1 a(i)= New OvalShape
a(i).Size = New Size(20, 20)a(i).Top = 360 a(i).Left = 120 + 20 * i a(i).FillStyle = FillStyle.Solid a(i).FillColor = Color.Yellow a(i).Parent = canvas Next
a(0).FillColor = Color.Red ‘給蛇上色 a(0).BackColor = Color.Red Dim i As Integer
canvas.parent = Me.Panel1 For i = 0 To 20 '形成网格,以两点坐标来确定线的位置
line(i)= New LineShape line(i).X1 = 0 line(i).X2 = 400 line(i).Y1 = 20 * i
line(i).Y2 = 20 * i line(i).Parent = canvas Next
For i = 21 To 41 line(i)= New LineShape line(i).X1 = 20 *(i21)line(i).Y1 = 0 line(i).Y2 = 400 line(i).Parent = canvas 蛇已经做好,那么我们可以进行下一步:让蛇随键盘的上下左右运动起来。这一步曾经困扰我很久,但通过上网我发现每一个键盘都对应着一个代码,如:左37,上38,右39,下40。首先得让控件响应键盘事件,其次让蛇动,而蛇的运动可看作蛇头移动,蛇身跟随前一个身体。由此可以用以下代码解决:
Private Sub form_keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)Handles Me.KeyDown Select Case e.KeyCode Case 37 If d <> 2 Then d = 0 ‘这样能让蛇不往回走
Case 38 If d <> 1 Then d = 3 Case 39 If d <> 0 Then d = 2 Case 40 If d <> 3 Then d = 1 End Select
End Sub Select Case d Case 2 For i = num To 1 Step-1 a(i).Left = a(i1).Top Next
a(0).Left += 20 Case 1 For i = num To 1 Step-1 a(i).Left = a(i1).Top Next
a(0).Top += 20 Case 0 For i = num To 1 Step-1
a(i).Left = a(i1).Top Next
a(0).Left-= 20 Case 3 For i = num To 1 Step-1 a(i).Left = a(i1).Top Next
a(0).Top-= 20 End Select 然后我们可以来设置食物来让蛇吃,我用rectangleshape控件来制造食物,就像和画出蛇身一样写,但是每当被吃后它有出现,而且不能出现在蛇身上。首先,它是随机出现的,我们可用随机函数,然后可通过判断让它不出现在蛇身上。对于数字1,2,3,4,5;我用lebal控件,然后让其和食物坐标同步,这样看起来就像数字在食物上一样。代码如下:
For i = 0 To 4 If a(0).Top = food(i).Top And a(0).Left = food(i).Left Then ‘判断是否吃到
score += i + 1 ‘分数增加 num += i + 1 ‘蛇身的个数增加 food(i).Visible = False word(i).Visible = False
ReDim Preserve food(4)ReDim Preserve word(4)food(i)= New RectangleShape food(i).Size = New Size(20, 20)Randomize()food(i).Left = Int(Rnd()* 20)* 20 ‘随机确定新坐标
food(i).Top = Int(Rnd()* 20)* 20 L2: For g = 0 To num1 If food(i).Left = a(g).Left And food(i).Top = a(g).Top Then
Call setloction(i)GoTo L2 End If
Next
food(i).FillStyle = FillStyle.Solid food(i).Parent = canvas word(i)= New Label word(i).Text = i + 1 word(i).Size = New Size(10, 10)
word(i).Top = food(i).Top + 5 word(i).Left = food(i).Left + 5 word(i).Parent = canvas ReDim Preserve a(num)‘蛇长长,重新定义动态数组
For k = numii1 If a(0).Top = barrier(k).Top And a(0).Left = barrier(k).Left Then
Timer1.Enabled = False a(0).FillColor = Color.Red lifetime-= 1 If score < 3 Then
If lifetime < 1 Then
MsgBox(“GAME OVER!”, 64, “贪吃蛇”)recordname = InputBox(“GAME OVER!” & Chr(13)& “你的成绩为” & Int(score)& “分用时” & _ Int(gameinterval * gametime / 1000)& ”秒。“ & Chr(13)& ”请留下大名“, ”贪吃蛇“, ”无名氏“)Else
p = MsgBox(”Do you want to continue?“, vbYesNo, ”生命值“ & lifetime)If p = 6 Then
Exit For
End If
End If
Else
If lifetime < 1 Then
recordname = InputBox(”GAME OVER!“ & Chr(13)& ”你的成绩为“ & Int(score)& ”分用时” & _ Int(gameinterval * gametime / 1000)& “秒。” & Chr(13)& “请留下大名?”, “贪吃蛇”, “无名氏”)Else
p = MsgBox(“Do you want to continue?”, vbYesNo, “生命值” & lifetime)If p = 6 Then
Exit For
End If
End If
End If
Call record()Call initialize()Exit For
End If
Next 想要形成障碍物,在此我们需要用到文件的读操作。主要代码如下:
FileOpen(1, filelacation, OpenMode.Input)' 读取墙壁,形成迷宫 bnum = 1 Dim L As Integer
Dim T As Integer
For i = 1 To 419 j = InputString(1, 2)If Val(j)= 1 Then
barrier(bnum)= New RectangleShape barrier(bnum).Left = 20 * L barrier(bnum).Top = 20 * T barrier(bnum).Size = New Size(20, 20)barrier(bnum).FillColor = barriercolor barrier(bnum).FillStyle = FillStyle.Solid barrier(bnum).Parent = canvas bnum += 1 End If
If Val(j)= 0 Then ‘用此方法使读出的内容与坐标对应
End If
If j = vbCrLf Then L-= 1 End If L += 1
If L = 20 Then L = 0 : T += 1 End If
Next
FileClose(1)在此基础上便可调用其它的过程,比较简单。代码如下:
Private Sub 开始ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles 开始ToolStripMenuItem.Click If Timer1.Enabled = False Then Timer1.Enabled = True
Exit Sub
End If
If Timer1.Enabled = True Then Timer1.Enabled = False
End If
End Sub
Private Sub 新开局ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles 新开局ToolStripMenuItem.Click Call initialize()End Sub
Private Sub 关于ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles 关于ToolStripMenuItem.Click MsgBox(“贪吃蛇vb2010.2016.3.20”, 64, “版本说明”)End Sub
Private Sub 帮助ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles 帮助ToolStripMenuItem1.Click Form3.Show()End Sub
Private Sub 设置ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles 设置ToolStripMenuItem.Click Form2.Show()End Sub
Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles 退出ToolStripMenuItem.Click
End
End Sub
Private Sub 排行榜ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles 排行榜ToolStripMenuItem.Click Form6.Show()End Sub
2.2窗体Form2.vb【设计】
这个界面是第二重要的,当然它的设计也较麻烦。设计的障碍物的类型用到了listbox控件,也用到了菜单栏与pictureBox控件。而调用的过程shapeview所用到的内容与Form1.vb中的类似,因此方便好多。
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load ListBox1.Items.Add(“方盒形”)ListBox1.Items.Add(“隧道形”)ListBox1.Items.Add(“螺旋形”)ListBox1.Items.Add(“路障形”)ListBox1.Items.Add(“弯曲形”)ListBox1.Items.Add(“怪异形”)ListBox1.Items.Add(“无障碍形”)PictureBox2.BackColor = Color.DarkGray PictureBox1.BackColor = Color.Aqua TextBox1.Text = lifetime Call shapeview()End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex = 0 Then filelacation = “maze1方盒形.txt” filename = “方盒形” mazenum = 1 End If
If ListBox1.SelectedIndex = 1 Then filelacation = “maze1隧道形.txt” filename = “隧道形” mazenum = 2 End If
If ListBox1.SelectedIndex = 2 Then filelacation = “maze1螺旋形.txt” filename = “螺旋形”
mazenum = 3 End If
If ListBox1.SelectedIndex = 3 Then filelacation = “maze1路障形.txt” filename = “路障形” mazenum = 4 End If
If ListBox1.SelectedIndex = 4 Then filelacation = “maze1弯曲形.txt” filename = “弯曲形” mazenum = 5 End If
If ListBox1.SelectedIndex = 5 Then filelacation = “maze1怪异形.txt” filename = “怪异形” mazenum = 6 End If
If ListBox1.SelectedIndex = 6 Then
filelacation = “maze1无障碍形.txt” filename = “无障碍形” mazenum = 7 End If
For k = 1 To oldnum121)line1(i).X2 = 10 *(i单车.wav“
'musicname = ”MUSIC陈奕迅-单车.wav“
My.Computer.Audio.Play(My.Application.Info.DirectoryPath & ”MUSIC陈奕迅-单车.wav“)End If
End Sub
Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles RadioButton6.CheckedChanged If RadioButton6.Checked = True Then
'Form1.AxWindowsMediaPlayer1.Ctlcontrols.stop()
My.Computer.Audio.Stop()End If
End Sub End Class
2.6窗体Form5.vb[设计】
此窗体用来调节蛇运动的快慢程度。
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles TrackBar1.Scroll
Dim v As Integer v = TrackBar1.Value timeinterval = 500 /(2 ^ v)Label3.Text = TrackBar1.Value End Sub
2.7窗体Form6.vb[设计】
此窗体用来显示玩家的成绩,用到了listBox控件来显示成绩,点击清除所有记录可还原,recordlist()用来记录文件中的每一行。Public Class Form6
Public Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load For i = 1 To 7 ListBox1.Items.Add(Form1.recordlist(i))Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click ListBox1.Items.Clear()FileOpen(1, My.Application.Info.DirectoryPath & ”clear.txt“, OpenMode.Input)For i = 1 To 7 Form1.recordlist(i)= LineInput(1)ListBox1.Items.Add(Form1.recordlist(i))Next
FileClose(1)End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button2.Click Me.Close()End Sub End Class
2.8标准模块
定义各种变量
Imports Microsoft.VisualBasic.PowerPacks Module Module1
Public barriercolor As System.Drawing.Color = Color.DarkGray
‘记录障碍物的颜色
Public panalcolor As System.Drawing.Color = Color.Aqua ‘记录面板的颜色
Public oldnum As Integer
Public oldnum1 As Integer
Public bnum1 As Integer
Public filelacation As String = ”maze1螺旋形.txt“
Public filename As String = ”螺旋形"
Public timeinterval As Integer = 1000 Public mazenum As Integer = 2 ‘为每一个障碍物类型做标记
Public lifetime As Integer = 1 ‘生命次数
Public barrier(400)As RectangleShape End Module
3、设计过程遇到的主要问题及解决方法
3.1刚开始时对于画出后面的线条时我是添加了41个Lineshape控件,一个个确定等距的坐标将其画下,废了我好大的气力,但后来受到了动态添加控件的触动用数组将其解决。
3.2如何引发键盘事件曾一度困扰我,了解了键盘下所代表的代码意义,之前一直使用的是select case keycode代码,怎么也无法使其响应。后来看看课本上关于键盘的代码,随便使用了select case e.keycode代码,令我欣慰的是竟然成功了。
3.3下一个是如何在界面上显示出障碍物。根据所提供的文件夹,了解起大概用法,为了让我编程更容易,我对其文件做了一定的修改。
3.4最难的是过程之间的调用与联系,解决的办法就是不断的进行调试,修改。比如在我编写重新开局的菜单代码时总是会出现两只蛇;又如在编写排行榜时便不是很方便的能使用listbox.Items.remove这行代码,因此我得另辟蹊径。
4、设计中尚存的不足分析
4.1由于我采用的编写方法是将完成这个贪吃蛇游戏所需满足的要求分开,一个一个的进行攻破,我会发现越往后编写越要考虑到之前的内容,在解决程序之间的关系会越麻烦,调试的次数更多。而且编完后纵观全局会发现代码的结构较混乱,重复性高,他人难以看的明白。
4.2 在定义变量时我尽量定义其为全局变量和模块级变量。部分变量为了简单或难以翻译为英文的仅仅使用了字母代替,对读代码有一定困难。
4.3 我所犯下的最大错误就是使用控件来显示蛇,因为在运行程序时蛇身会随着长度的增加闪烁的愈发厉害。可能是控件太多,程序运行不流畅,效率不高。
4.4 我想在游戏中添加一些音乐,并且用到了循环播放的代码,但是至今我还是没有想到如何能将播放中的音乐暂停。还有的就是怎样才能同时播放两种声音。
4.5 对于排行榜的编写存在一些问题,比如:怎样才能使显示的行间距更开;如何才能使所显示的各项指标在其对应的列下,在这一点上有很大的缺陷。可能程序要用到dictionary方法。
4.6 当我运行程序时,如果我点了界面上的某条线的话,蛇就不再响应键盘事件了,必须使用ctrl+方向键才能使其响应键盘事件。应该是我点击时失去了焦点导致无法正常运行。
5、心得体会
这个程序从难度上讲足足有四颗星,因此我想说我确实摸索了很久,从界面的制作排版到代码的编写。由于计算机编程知识当初只学了些皮毛,很多次我在编程时不断调试却依然出现这样那样的毛病,可能是无意中拼写错误,但有些确实是因为不了解vb的某些较为冷僻的用法所致。这段时间,常会向同学请教,也自觉地去借了些书看看,基本上完成了要求,但是功能、界面都还不是很完善,我会借助以后的时间继续去加工的,我需要的是学以致用。
我体会到了编写程序是个有条理的逻辑过程,首先必须有个大致的思路,整体的规划出来。然后再是分步地去具体完成,尤为重要的是需要投入相当一部分精力去精益求精,这样你的成果才不会是毛糙的。思维调动是件另人愉快的事情,而我们应该去学会享受脑力劳动的过程,哪怕是一次次地失利。这就是2个月的课程设计所教会我的。
6、参考文献
[2].王栋,visual basic程序设计实用教程,北京,清华大学出版社
第五篇:vb课程设计 职工工资管理系统代码
Form1: Dim errorCount As Integer Private Sub Command1_Click()If Text1.Text = “system” And Text2.Text = “1234” Then
Load Form2
Form2.Show
Me.Hide
MsgBox “登陆成功,欢迎您进入职工工资管理系统”, vbInformation, “登陆成功”
Else
errorCount = errorCount + 1
If errorCount < 3 Then
MsgBox “用户名或密码错误(第” & errorCount & “次)!”, vbOKOnly + vbExclamation, “登陆失败”
Text1.SetFocus
End If
If errorCount >= 3 Then
Text1.Enabled = False
Text2.Enabled = False
Command1.Enabled = False
MsgBox “三次输入用户名或密码错误,程序将自动退出!”, vbOKOnly + vbExclamation, “登陆失败”
End
End If End If
End Sub
Private Sub Command2_Click()Unload Me End Sub
Private Sub Form_Load()errorCount = 0 End Sub
Private Sub Form_Unload(Cancel As Integer)If MsgBox(“是否要退出程序?”, vbYesNo, “退出提示”)= vbNo Then
Cancel = 1 End If End Sub
For i = 1 To jlNum
ygList(i).shifagongzi =(CSng(ygList(i).jibengz)+ ygList(i).gerensui
CSng(ygList(i).jingtie)
ygList(y).shifagongzi = ygList(zuishao).shifagongzi ygList(zuishao).shifagongzi = linShi.shifagongzi End If Next loadData End Sub
Private Sub Command5_Click()Dim linShi As YuanGong
linShi.ygnamg = InputBox(“请输入要添加的姓名”, “添加员工-姓名录入”)If Len(Trim(linShi.ygnamg))> 0 Then
linShi.jibengz = InputBox(“请输入添加员工的基本工资”, “添加员工-基本工资录入”)
If Len(Trim(linShi.jibengz))> 0 Then
linShi.jingtie = InputBox(“请输入添加员工的津贴”, “添加员工-津贴录入”)
If Len(Trim(linShi.jingtie))> 0 Then
linShi.gerensui = InputBox(“请输入添加员工的个人所得税”, “添加员工-个税录入”)
If Len(Trim(linShi.gerensui))> 0 Then
linShi.suidianfei = InputBox(“请输入添加员工的水电费”, “添加员工-水电费录入”)
If Len(Trim(linShi.suidianfei))> 0 Then
Open filedir For Append As #1
Write #1, linShi.ygnamg;linShi.jibengz;linShi.jingtie;linShi.gerensui;linShi.suidianfei
Close #1
MsgBox “添加员工成功!”, vbOKOnly + vbInformation, “成功”
jlNum = 0 If Len(Trim(filedir))> 0 Then
Open filedir For Input As #1
Do While Not EOF(1)
Line Input #1, a
If Len(Trim(a))> 0 Then
jlNum = jlNum + 1
End If
Loop
Close #1
ReDim Preserve ygList(jlNum)As YuanGong
jlNum = 0
Open filedir For Input As #1
Do While Not EOF(1)
jlNum = jlNum + 1
Input #1, ygList(jlNum).ygnamg
Input #1, ygList(jlNum).jibengz
Input #1, ygList(jlNum).jingtie
Next
Close #1
MsgBox “删除员工成功!”, vbOKOnly + vbInformation, “成功”
jlNum = 0 If Len(Trim(filedir))> 0 Then
Open filedir For Input As #1
Do While Not EOF(1)
Line Input #1, a
If Len(Trim(a))> 0 Then
jlNum = jlNum + 1
End If
Loop
Close #1
ReDim ygList(jlNum)As YuanGong
jlNum = 0
Open filedir For Input As #1
Do While Not EOF(1)
jlNum = jlNum + 1
Input #1, ygList(jlNum).ygnamg
Input #1, ygList(jlNum).jibengz
Input #1, ygList(jlNum).jingtie
Input #1, ygList(jlNum).gerensui
Input #1, ygList(jlNum).suidianfei
Loop
Close #1
loadData
Command2.Enabled = True
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = True
Command6.Enabled = True End If
Text1.Text = “姓名” & vbTab & “工资” & vbTab & “津贴” & vbTab & “所得税” & vbTab & “水电费” & vbCrLf
For i = 1 To jlNum
Text1.Text = Text1.Text & ygList(i).ygnamg & vbTab & ygList(i).jibengz & vbTab & ygList(i).jingtie & vbTab & ygList(i).gerensui & vbTab & ygList(i).suidianfei & vbCrLf
Next Else
MsgBox “删除员工失败!”, vbOKOnly + vbExclamation, “失败” End If End Sub
Private Sub Command7_Click()