Visual Basic
---课程设计与心得
班级:计科107
姓名:张麟树
学号:1004681324
一、实验课题:设计一个能计算2阶、3阶、4阶行列式的小软件,运行时先由用户输入行列式的阶数,然后软件自动生成行列式的输入模板,如用户输入的阶数是3,则产生9个输入框并排成3行3列且用两条竖线界定。这里需用控件的动太加载技术。
二、实验代码:
Dim N As Integer
Public Function choose(ByVal X As Integer)
Do
If X = 2 Then
With Form1
.Line1.Visible = True
.Line2.Visible = True
.Line3.Visible = False
.Line4.Visible = False
.Line5.Visible = False
.Line6.Visible = False
End With
For I = 0 To 3
Form1.Text1(I).Visible = True
Next I
Exit Do
ElseIf X = 3 Then
With Form1
.Line1.Visible = False
.Line2.Visible = False
.Line5.Visible = False
.Line6.Visible = False
.Line3.Visible = True
.Line4.Visible = True
End With
For I = 0 To 8
Form1.Text1(I).Visible = True
Next I
Exit Do
ElseIf X = 4 Then
With Form1
.Line3.Visible = False
.Line4.Visible = False
.Line1.Visible = False
.Line2.Visible = False
.Line5.Visible = True
.Line6.Visible = True
End With
For I = 0 To 15
Form1.Text1(I).Visible = True
Next I
Exit Do
Else
Call Command1_Click
End If
Loop
End Function
Public Function Huanyuan()
Dim I As Integer
For I = 0 To 15
Form1.Text1(I).Visible = False
Next I
With Form1
.Line1.Visible = False
.Line2.Visible = False
.Line3.Visible = False
.Line4.Visible = False
.Line5.Visible = False
.Line6.Visible = False
End With
End Function
Private Sub Command1_Click()
Dim I As Integer
Call Huanyuan
N = InputBox("请输入阶数(2、3或4):")
For I = 0 To 15
Form1.Text1(I).Text = ""
Next I
Call choose(N)
End Sub
Private Sub Command2_Click()
Dim val As Integer
If N = 2 Then
val = Form1.Text1(0) * Form1.Text1(3) - Form1.Text1(2) * Form1.Text1(1)
ElseIf N = 3 Then
val = Form1.Text1(0) * Form1.Text1(3) * Form1.Text1(8) + Form1.Text1(1) * Form1.Text1(5) * Form1.Text1(6) + Form1.Text1(4) * Form1.Text1(2) * Form1.Text1(7) - Form1.Text1(0) * Form1.Text1(5) * Form1.Text1(7) - Form1.Text1(1) * Form1.Text1(2) * Form1.Text1(8) - Form1.Text1(3) * Form1.Text1(4) * Form1.Text1(6)
Else
val = Form1.Text1(0) * Form1.Text1(3) * Form1.Text1(8) * Form1.Text1(15) + Form1.Text1(2) * Form1.Text1(7) * Form1.Text1(14) * Form1.Text1(9) + Form1.Text1(1) * Form1.Text1(5) * Form1.Text1(11) * Form1.Text1(12) + Form1.Text1(4) * Form1.Text1(10) * Form1.Text1(6) * Form1.Text1(13) - Form1.Text1(0) * Form1.Text1(10) * Form1.Text1(8) * Form1.Text1(13) + Form1.Text1(1) * Form1.Text1(2) * Form1.Text1(11) * Form1.Text1(14) + Form1.Text1(3) * Form1.Text1(4) * Form1.Text1(6) * Form1.Text1(15) + Form1.Text1(5) * Form1.Text1(7) * Form1.Text1(9) * Form1.Text1(12)
End If
MsgBox ("结果为: " & val)
End Sub
Private Sub Command3_Click()
End
End Sub
三、实验结果:
选择菜单:
二阶行列式:
三阶行列式:
四阶行列式:
四、心得与体会:
作为一名计算机专业的学生,在接触了C语言、数据结构与C++课程后,对计算机语言有了一定的兴趣,于是在这个学期我毫不犹豫的选择了VB课程(算法与程序设计)。
VB程序设计的入门学习并不难,但却是一个十分重要的过程,因为程序设计思想的形成,以及之前学习积累的经验,很快就对VB有了一定的了解。下面就是我这段时间学习的新的与体会。
我认为作为学习程序设计最重要的一点是要注重理解一些重要的概念.VB程序设计本身并不复杂,翻开一本程序设计学习的书籍,看到的无非就是变量,函数,条件语句,循环语句等概念.但要真正能进行程序设计,需要深入理解这些概念.因此,在 程序入门阶段还是应该重视概念的学习.我们学习VB并没有从正常的途径,而是通过老师的演示与讲解,了解并学习了这门课程,可以说VB语言相对来说比其他语言简单,从他的语法就可以看出
其次,自己动手编写程序。程序设计入门阶段要经常自己动手抄写或编写一些小程序,亲自动手进行程序设计是创造性思维应用的体现,是培养逻辑思维的好方法.因此一定要多动手编写程序,而且要从小程序开发开始,逐渐提高写程序的能力.只有不断的从自己的程序中积累经验,从中进步,才能写出好的程序。程序设计课是高强度的脑力劳动,不是听会的,也不是看会的,而是练会的.只有自己动手,编写一些程序,才会有成就感,进而对课程产生兴趣,学起来才比较从容.动手能力的培养是这门课和以往课程最大的不同之处.重点放在思路,算法,编程构思和程序实现上.语句只是表达工具,要求堂上积极思考,尽量当堂学懂,并做到灵活应用. 编程序不像做其它的事情,写程序之前一定要计划周详,把所有的可能性都想到,并想好相应的解决方法,不然思想会很混乱,很容易出现错误。还有就是编程序最重要的其实是思想,思想确定之后用什么语言或什么方法就简单了,接下来就是设计并写成代码的事情了
好的程序员就是这样一步一步炼成的。
在者,阅读,借签别人设计的好程序。多看别人设计好的程序代码,包括教材上的例题程序.在读懂别人程序后,要想他为什么这么设计能不能将程序修改完成更多的功能 则可以学到别人优秀的东西,帮助自己 提高自身水平.
通过这次学习,我越来越喜欢上计算机语言,感谢方老师,我相信在计算机语言上回走的更远。
第二篇:Vb课程设计代码
login
Imports System.Data.oledb
///// Public Class login
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Dim DR As OleDbDataReader
Dim count As Integer = 0
//// Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.MDB" con.Open()
cmd.Connection = con
End Sub
//// Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
cmd.CommandText = "Select * From test.User where 账号='" & TextBox1.Text & "'" & _ " and 密码='" & TextBox2.Text & "'"
DR = cmd.ExecuteReader
If DR.Read Then
Me.Hide()
MDIform1.Show()
Else
DR.Close()
cmd.CommandText = "Select * From test.User where 账号='" & TextBox1.Text & "'" DR = cmd.ExecuteReader
If DR.Read = False Then
MsgBox("登录信息错误,请检查后重新输入!", MsgBoxStyle.Exclamation, "提示")
TextBox1.Focus()
TextBox1.SelectAll()
Else
MsgBox("登录信息错误,请检查后重新输入!", MsgBoxStyle.Exclamation, "提示") TextBox2.Focus()
TextBox2.SelectAll()
End If
DR.Close()
End If
count += 1
If count = 3 Then
Application.Exit()
End If
End Sub
//// Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar < "A" Or e.KeyChar > "Z" Then
e.Handled = True
If e.KeyChar = Chr(8) Then
e.Handled = False
End If
If e.KeyChar = Chr(13) Then
TextBox2.Focus()
End If
End If
End Sub
////Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Application.Exit()
End Sub
End Class
MDI FORM
Imports System.Windows.Forms
/////Public Class MDIform1
Dim CurrTime As Date
//// Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim form1 As New Form1
Form1.MdiParent = Me
Form1.Show()
End Sub
//// Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim form2 As New Form2
Form2.MdiParent = Me
Form2.Show()
End Sub
///// Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim form3 As New Form3
Form3.MdiParent = Me
Form3.Show()
End Sub
///// Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
If MsgBox("您确定系统吗?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "退出系统") = MsgBoxResult.Yes Then
Application.Exit() '退出系统
Else
Exit Sub
End If
End Sub
///// Private Sub MDIform1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ToolStripStatusLabel3.Text = Now.Date
CurrTime = CDate(Today.Date & " 17:00:00")
CurrTime = CDate(CurrTime.Subtract(Now).ToString)
'CurrTime = CDate("2:00:00")
End Sub
///// Private Sub NewToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripButton.Click
Dim form1 As New Form1
Form1.MdiParent = Me
Form1.Show()
End Sub
///// Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripButton.Click
Dim form2 As New Form2
Form2.MdiParent = Me
Form2.Show()
End Sub
///// Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim form3 As New Form3
Form3.MdiParent = Me
Form3.Show()
End Sub
///// Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
If MsgBox("您确定系统吗?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "退出系统") = MsgBoxResult.Yes Then
Application.Exit() '退出系统
Else
Exit Sub
End If
End Sub
///// Sub DisplayTime()
If CurrTime <> "00:00:00" Then
CurrTime = CurrTime.AddSeconds(-1)
ToolStripStatusLabel1.Text = "距离考试结束还有:" &
Microsoft.VisualBasic.Format(CurrTime, "HH:mm:ss")
Else
MsgBox("考试时间到,请交卷!")
End If
End Sub
///// Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' ToolStripStatusLabel1.Text = "考试结束还有:" & (16 - Now.Hour) & "小时" & 60 - Now.Minute & "分" & 60 - Now.Second & "秒"
If My.Computer.Keyboard.NumLock = True Then
ToolStripStatusLabel2.Text = "num :" & "on"
Else
ToolStripStatusLabel2.Text = "num :" & "off"
End If
End Sub
///// Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Call DisplayTime()
End Sub
End Class
Public Class Form2
Dim WithEvents objs As New salarycard
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
objs.accounts = "000001"
objs.passwords = "123456"
objs.balances = "865.30"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
objs.add(Val(TextBox1.Text))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
objs.Draw(Val(TextBox2.Text))
End Sub
Private Sub obj_NumberBelowPoint() Handles objs.NumberBelowPoint
MsgBox("您的卡内余额已少于元!")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
objs.ShowMe()
End Sub
End Class
Salarycard (存钱 取钱 ) Public Class salarycard
Private account As String
Private password As String
Private balance As Single
Public Event NumberBelowPoint()
Public Property accounts() As String Get
Return account
End Get
Set(ByVal value As String)
account = value
End Set
End Property
Public Property passwords() As String Get
Return password
End Get
Set(ByVal value As String)
password = value
End Set
End Property
Public Property balances() As Single Get
Return balance
End Get
Set(ByVal value As Single)
balance = value
End Set
End Property
Public Sub Draw(ByVal money As Single) Dim no As String
Dim pass As String
no = InputBox("请输入账号:") pass = InputBox("请输入密码:")
If no = account And pass = password Then
If money > balance Then
MsgBox("余额不足,请查询后再试!") Else
balance = balance - money
MsgBox("钱已取出!")
If balance < 10 Then
RaiseEvent NumberBelowPoint()
End If
End If
Else
Err.Number = vbObjectError + 512 + 1
Err.Description = "无效的密码的错误"
MessageBox.Show(" 错误号为 : " & Err.Number, Err.Description) End If
End Sub
Public Sub add(ByVal money As Single)
balance += money
MsgBox("钱已存入!")
End Sub
Public Sub ShowMe()
MessageBox.Show("账号:" & account & " 余额:" & balance) End Sub
End Class
Form 3
Imports System.Data.OleDb
Public Class Form3
Dim con As Integer
Dim Navigator As BindingManagerBase
Dim MyCon As OleDbConnection
Dim MyCom As OleDbCommand
Dim MyDataAdapter As OleDbDataAdapter
Dim MyDs As DataSet
Dim CurRowNo As Integer
Dim AppendRow As Boolean
///// Private Sub DispValue()
TextBox1.Text = MyDs.Tables("Publisher").Rows(Navigator.Position)(0).ToString TextBox2.Text = MyDs.Tables("Publisher").Rows(Navigator.Position)(1).ToString TextBox3.Text = MyDs.Tables("Publisher").Rows(Navigator.Position)(2).ToString End Sub
///// Private Sub ClearText()
TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = ""
End Sub
///// Private Sub TextReadOnly()
TextBox1.ReadOnly = True : TextBox2.ReadOnly = True : TextBox3.ReadOnly = True End Sub
///// Private Sub TextReadWrite()
TextBox1.ReadOnly = False : TextBox2.ReadOnly = False : TextBox3.ReadOnly = False End Sub
///// Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim CString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Henry Book.MDB" Dim SQLStr As String = "Select * From Publisher"
MyCon = New OleDbConnection(CString)
MyCon.Open()
MyCom = New OleDbCommand()
MyCom.Connection = MyCon
MyCom.CommandType = CommandType.Text
MyCom.CommandText = SQLStr
MyDataAdapter = New OleDbDataAdapter()
MyDataAdapter.SelectCommand = MyCom
MyDs = New DataSet()
MyDataAdapter.Fill(MyDs, "Publisher")
Navigator = Me.BindingContext(MyDs, "Publisher")
TextReadOnly()
DispValue()
End Sub
///// Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Navigator.Position = 0
DispValue()
End Sub
///// Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Navigator.Position = 0 Then
Navigator.Position = Navigator.Count - 1
Else
Navigator.Position -= 1
End If
DispValue()
End Sub
///// Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Navigator.Position = Navigator.Count - 1 Then
Navigator.Position = 0
Else
Navigator.Position += 1
End If
DispValue()
End Sub
///// Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Navigator.Position = Navigator.Count - 1
DispValue()
End Sub
///// Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
CurRowNo = Navigator.Position
TextReadWrite()
ClearText()
AppendRow = True
End Sub
///// Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim AppString As String
Dim MyComB As New OleDbCommand()
MyComB.Connection = MyCon
MyComB.CommandType = CommandType.Text
If (AppendRow = True) Then
AppString = "Insert into Publisher(PUBLISHER_CODE,PUBLISHER_NAME,CITY) values('" +_ TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')"
MyComB.CommandText = AppString
MyComB.ExecuteNonQuery()
End If
MyDataAdapter.Update(MyDs, "Publisher")
MyDs.Clear()
MyDataAdapter.Fill(MyDs, "Publisher")
Navigator.Position = Navigator.Count - 1
DispValue()
MsgBox("保存成功!")
End Sub
///// Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim MyComC As New OleDbCommand()
MyComC.Connection = MyCon
MyComC.CommandType = CommandType.Text
If Navigator.Count > 0 Then
MyComC.CommandText = "Delete From publisher where PUBLISHER_CODE= '" + _
MyDs.Tables("Publisher").Rows(Navigator.Position)("PUBLISHER_CODE").ToString() + "'"
MyComC.ExecuteNonQuery()
MsgBox("删除成功")
Else
MsgBox("已无记录可删除")
Exit Sub
End If
MyDataAdapter.Update(MyDs, "Publisher")
MyDs.Clear()
MyDataAdapter.Fill(MyDs, "Publisher")
If Navigator.Count > 0 Then
DispValue()
Else
ClearText()
End If
End Sub
///// Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Form4.Show()
End Sub
End Class
////////////////////////////////////////////////////////////////////////////////////////////
*******************************************
Public Class Form1
Private Function jiecheng(ByVal n As Integer) As Single
Dim s As Single
Dim i As Integer
s = 1
For i = 1 To n
s = s * i
Next
Return s
End Function
Private Function s(ByVal n As Integer, ByVal x As Single) As Single
Dim i As Integer
Dim result As Single = 0
For i = 1 To n
result += Math.Pow(x, 2 * i - 1) / jiecheng(2 * i)
Next
Return result
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer
Dim n As Integer
n = InputBox(" 请输入 N 的值:")
x = InputBox(" 请输入 X的值:")
TextBox1.Text = s(n, x)
End Sub
End Class
************************************
Public Class Form2
Dim WithEvents objs As New salarycard
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
objs.receive(Val(TextBox1.Text))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
objs.pay (Val(TextBox2.Text))
End Sub
Private Sub obj_NumberBelowPoint() Handles objs.overlimit
MsgBox("您的应付款小于限额!!!")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
objs.ShowMe()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
****Salarycard 2
Public Class salarycard
Private commmany_name As String = "联想" Private telephone As String = "123456" Private person As String = "smith" Private money As Double = "12500.23" Private limit As Double = "20000.00" Public Event overlimit()
Public Property commmany_names() As String Get
Return commmany_name
End Get
Set(ByVal value As String)
commmany_name = value
End Set
End Property
Public Property telephones() As String Get
Return telephone
End Get
Set(ByVal value As String)
telephone = value
End Set
End Property
Public Property persons() As String Get
Return person
End Get
Set(ByVal value As String)
person = value
End Set
End Property
Public Property moneys() As Double Get
Return money
End Get
Set(ByVal value As Double)
value = money
End Set
End Property
Public Property limits() As Double
Get
Return limit
End Get
Set(ByVal value As Double)
value = limit
End Set
End Property
Public Sub receive(ByVal mon As Double)
If mon < 0 Then
Err.Number = vbObjectError + 512 + 1
Err.Description = "无效的金额的错误"
MessageBox.Show(" 错误号为 : " & Err.Number, Err.Description)
Else
limit = limit + mon
MsgBox("收款已存入!")
End If
End Sub
Public Sub pay(ByVal mon As Double)
If money > limit Then
RaiseEvent overlimit()
Else
money = money + mon
MsgBox("应付款已增加 !")
End If
End Sub
Public Sub ShowMe()
MessageBox.Show("公司名称 :" & commmany_name & " 电话号码 :" & telephone & "联系人 :" & person & "应付款 :" & money & "限额" & limit)
End Sub
End Class