📄 form6.vb
字号:
ComboBox1.Enabled = False
End Sub
'日期控件选择日期的事件
Private Sub MonthCalendar1_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateSelected
'定义gdate是Date类型变量
Dim gdate As Date
'gdate是选择的日期
gdate = MonthCalendar1.SelectionRange.Start()
'设置日期控件不可见
MonthCalendar1.Visible = False
'更新Textbox2的内容
TextBox2.Text = CStr(gdate)
End Sub
'响应选择日期事件
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'日期控件可见
MonthCalendar1.Visible = True
End Sub
'响应关闭的事件
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Me.Close()
End Sub
'设置dobutton函数
'a,b,c,d,e分别代表了添加、删除、修改,取消和日期选择几个按键的Enable值
Private Function dobutton(ByVal a, ByVal b, ByVal c, ByVal d, ByVal e)
'首先将5个按钮均置为False
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
Button6.Enabled = False
'判断a,b,c,d,e的值,1则为按钮有效,0为按钮无效
If a = 1 Then Button1.Enabled = True
If b = 1 Then Button2.Enabled = True
If c = 1 Then Button3.Enabled = True
If d = 1 Then Button4.Enabled = True
If e = 1 Then Button6.Enabled = True
End Function
'响应添加的事件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'状态加1,如果n=1,则表示现在是输入状态
n = n + 1
If n = 1 Then
'打开Textbox和ComboBox接受用户数据
TextBox1.Enabled = True
TextBox3.Enabled = True
TextBox7.Enabled = True
ComboBox1.Enabled = True
'清空Textbox和ComboBox中间的内容
TextBox1.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox3.Text = ""
TextBox7.Text = ""
ComboBox1.Text = ""
'TextBox2的内容是当前的日期
TextBox2.Text = CStr(Today.Date.ToShortDateString)
'设置按键
dobutton(1, 0, 0, 1, 1)
End If
'如果n=2,则表示现在是更新数据库的操作
If n = 2 And TextBox1.Text <> "" And TextBox2.Text <> "" And ComboBox1.Text <> "" Then '对输入的数据进行判断和校验
'计算单价
If Val(TextBox3.Text) = 0 Then TextBox3.Text = 1
TextBox5.Text = Int(Val(TextBox1.Text) / Val(TextBox3.Text))
'生成SQL语句
nsql = "INSERT INTO outgoingtable VALUES(" + Chr(39) + CStr(TextBox2.Text) + Chr(39) + "," + Chr(39) + CStr(ComboBox1.Text) + Chr(39) + "," + Chr(39) + CStr(TextBox6.Text) + Chr(39) + "," + Chr(39) + CStr(TextBox3.Text) + Chr(39) + "," + Chr(39) + CStr(TextBox4.Text) + Chr(39) + "," + Chr(39) + CStr(TextBox1.Text) + Chr(39) + "," + Chr(39) + CStr(TextBox5.Text) + Chr(39) + "," + Chr(39) + CStr(TextBox7.Text) + Chr(39) + ")"
'打开数据库
If OleDbConnection1.State = ConnectionState.Closed Then OleDbConnection1.Open()
'指定SQL语句对应的连接,指定SQL语句
ncommand.Connection = OleDbConnection1
ncommand.CommandText() = nsql
'执行SQL语句,更新数据库
ncommand.ExecuteNonQuery()
'更新Outgoingtable1 DataSet数据集
Outgoingtable1.Clear()
outgoingtable.Fill(Outgoingtable1)
'关闭数据库连接
'设置Textbox,ComboBox和按钮
OleDbConnection1.Close()
TextBox1.Enabled = False
TextBox3.Enabled = False
TextBox7.Enabled = False
ComboBox1.Enabled = False
dobutton(1, 1, 1, 0, 0)
End If
'如果n=2表示状态标志结束,置0
If n = 2 Then n = 0
End Sub
'响应删除的事件
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'生成SQL语句
nsql = "DELETE FROM outgoingtable WHERE 日期=" + Chr(39) + CStr(TextBox2.Text) + Chr(39) + " and 项目名称=" + Chr(39) + CStr(ComboBox1.Text) + Chr(39) + " and 类型=" + Chr(39) + CStr(TextBox6.Text) + Chr(39) + " and 数量=" + Chr(39) + CStr(TextBox3.Text) + Chr(39) + " and 单位=" + Chr(39) + CStr(TextBox4.Text) + Chr(39) + " and 金额=" + Chr(39) + CStr(TextBox1.Text) + Chr(39) + " and 单价=" + Chr(39) + CStr(TextBox5.Text) + Chr(39) + " and 备注=" + Chr(39) + CStr(TextBox7.Text) + Chr(39)
'打开数据库
If OleDbConnection1.State = ConnectionState.Closed Then OleDbConnection1.Open()
'指定SQL语句对应的连接,指定SQL语句
ncommand.Connection = OleDbConnection1
ncommand.CommandText() = nsql
'执行SQL语句,更新数据库
ncommand.ExecuteNonQuery()
'更新Outgoingtable1 DataSet数据集
Outgoingtable1.Clear()
outgoingtable.Fill(Outgoingtable1)
'关闭数据库连接
OleDbConnection1.Close()
End Sub
'响应取消的事件
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'设置按钮,恢复原来设定
dobutton(1, 1, 1, 0, 0)
'置状态为0
n = 0
'设置Textbox,ComboBox
TextBox1.Enabled = False
TextBox3.Enabled = False
TextBox7.Enabled = False
ComboBox1.Enabled = False
End Sub
'响应更新的事件
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'状态加1,如果n=1,则表示现在是输入状态
n = n + 1
If n = 1 Then
'备份原来数据库中的数据
bak1 = TextBox1.Text
bak2 = TextBox2.Text
bak3 = TextBox3.Text
bak4 = TextBox4.Text
bak5 = TextBox5.Text
bak6 = TextBox6.Text
bak7 = TextBox7.Text
bak8 = ComboBox1.Text
'设置Textbox,ComboBox和按钮
TextBox1.Enabled = True
TextBox3.Enabled = True
TextBox7.Enabled = True
ComboBox1.Enabled = True
dobutton(0, 0, 1, 1, 1)
End If
'如果n=2,则表示现在是更新数据库的操作
If n = 2 And TextBox1.Text <> "" And TextBox2.Text <> "" And ComboBox1.Text <> "" Then
'计算单价
If Val(TextBox3.Text) = 0 Then TextBox3.Text = 1
TextBox5.Text = Val(TextBox1.Text) / Val(TextBox3.Text)
'生成SQL语句
nsql = "UPDATE outgoingtable SET 日期=" + Chr(39) + CStr(TextBox2.Text) + Chr(39) + ", 项目名称=" + Chr(39) + CStr(ComboBox1.Text) + Chr(39) + " ,类型=" + Chr(39) + CStr(TextBox6.Text) + Chr(39) + ",数量=" + Chr(39) + CStr(TextBox3.Text) + Chr(39) + " , 单位=" + Chr(39) + CStr(TextBox4.Text) + Chr(39) + " ,金额=" + Chr(39) + CStr(TextBox1.Text) + Chr(39) + " ,单价=" + Chr(39) + CStr(TextBox5.Text) + Chr(39) + ", 备注=" + Chr(39) + CStr(TextBox7.Text) + Chr(39) + " WHERE 日期=" + Chr(39) + bak2 + Chr(39) + " and 项目名称=" + Chr(39) + bak8 + Chr(39) + " and 类型=" + Chr(39) + bak6 + Chr(39) + " and 数量=" + Chr(39) + bak3 + Chr(39) + " and 单位=" + Chr(39) + bak4 + Chr(39) + " and 金额=" + Chr(39) + bak1 + Chr(39) + " and 单价=" + Chr(39) + bak5 + Chr(39) + " and 备注=" + Chr(39) + bak7 + Chr(39)
'打开数据库
If OleDbConnection1.State = ConnectionState.Closed Then OleDbConnection1.Open()
'指定SQL语句对应的连接,指定SQL语句
ncommand.Connection = OleDbConnection1
ncommand.CommandText() = nsql
'执行SQL语句,更新数据库
ncommand.ExecuteNonQuery()
'更新Outgoingtable1 DataSet数据集
Outgoingtable1.Clear()
outgoingtable.Fill(Outgoingtable1)
'关闭数据库连接
OleDbConnection1.Close()
'设置Textbox,ComboBox和按钮
TextBox1.Enabled = False
TextBox3.Enabled = False
TextBox7.Enabled = False
ComboBox1.Enabled = False
dobutton(1, 1, 1, 0, 0)
End If
'如果n=2表示状态标志结束,置0
If n = 2 Then n = 0
End Sub
'ComboBox更新事件,用来设置项目类型和单位
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'定义SQL语句
Dim mySelectQuery As String = "Select * from outgoing Where 项目名称=" + Chr(39) + ComboBox1.Text + Chr(39)
'定于SQL命令,所使用的数据连接是OleDbConnection1,SQL语句是mySelectQuery
Dim myCommand As New OleDbCommand(mySelectQuery, OleDbConnection1)
'打开数据库连接
If OleDbConnection1.State = ConnectionState.Closed Then OleDbConnection1.Open()
'定义myReader是数据的读入记录
Dim myReader As OleDbDataReader
'读入数据
myReader = myCommand.ExecuteReader()
'记录前进一位
myReader.Read()
'设置TextBox的内容
TextBox4.Text = myReader.GetString(3)
TextBox6.Text = myReader.GetString(2)
'关闭myReader
myReader.Close()
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim keycode As Integer
keycode = Asc(e.KeyChar)
If (keycode >= 48 And keycode <= 57) Or (keycode = 46) Then
Else
MessageBox.Show("请输入数字")
e.Handled = True
End If
End Sub
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
Dim keycode As Integer
keycode = Asc(e.KeyChar)
If (keycode >= 48 And keycode <= 57) Or (keycode = 46) Then
Else
MessageBox.Show("请输入数字")
e.Handled = True
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -