📄 frmmenu.vb
字号:
Me.btnExt.Name = "btnExt"
Me.btnExt.Size = New System.Drawing.Size(88, 40)
Me.btnExt.TabIndex = 4
Me.btnExt.Text = "退 出"
'
'frmMenu
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.CancelButton = Me.btnExt
Me.ClientSize = New System.Drawing.Size(680, 496)
Me.Controls.Add(Me.btnExt)
Me.Controls.Add(Me.btnSav)
Me.Controls.Add(Me.btnEdt)
Me.Controls.Add(Me.btnDel)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.grbMenu)
Me.Controls.Add(Me.dgdMenu)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "frmMenu"
Me.Text = "菜谱管理"
Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
CType(Me.dgdMenu, System.ComponentModel.ISupportInitialize).EndInit()
Me.grbMenu.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
'用于区分是对增加进行保存还是对编辑进行保存
Dim SavAsAdd As Boolean
'创建DB类的对象
Dim odb As New DB.DB
'定义字符串变量,用于保存数据表操作的SQL语句
Dim sqlSel As String
Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
'定义变量用于保存窗体的最大高度和宽度
Dim m, n As Integer
sqlSel = "select id as 编号,name as 菜名,status as 是否可用,price " & _
"as 价格,type as 类型,memo as 说明 from menu"
Me.WindowState = FormWindowState.Maximized
'记录窗体的最大高度和宽度
m = Me.Height
n = Me.Width
Me.WindowState = FormWindowState.Normal
'设置窗体的大小和位置
Me.Width = n
Me.Height = m
Me.Top = 0
Me.Left = 0
'设置窗体中各个控件的位置和大小
dgdMenu.Left = 0
dgdMenu.Top = 0
dgdMenu.Width = Me.Width
dgdMenu.Height = 2 * Me.Height / 5
dgdMenu.RowHeaderWidth = 5
dgdMenu.PreferredColumnWidth = (Me.Width - 35) / 6
grbMenu.Left = 20
grbMenu.Top = dgdMenu.Height + 20
grbMenu.Width = Me.Width - 40
grbMenu.Height = 2 * Me.Height / 5
btnAdd.Left = 250
btnAdd.Top = dgdMenu.Height + grbMenu.Height + 50
btnAdd.Height = 40
btnAdd.Width = 80
btnDel.Left = 350
btnDel.Top = dgdMenu.Height + grbMenu.Height + 50
btnDel.Height = 40
btnDel.Width = 80
btnEdt.Left = 450
btnEdt.Top = dgdMenu.Height + grbMenu.Height + 50
btnEdt.Height = 40
btnEdt.Width = 80
btnSav.Left = 550
btnSav.Top = dgdMenu.Height + grbMenu.Height + 50
btnSav.Height = 40
btnSav.Width = 80
btnExt.Left = 650
btnExt.Top = dgdMenu.Height + grbMenu.Height + 50
btnExt.Height = 40
btnExt.Width = 80
'设置表格的数据源
dgdMenu.DataSource = odb.CreateDataTable(sqlSel)
'设置各个控件的状态
FreshFrm(0)
End Sub
Private Sub FreshFrm(ByVal n As Integer)
'设置增加、删除和编辑操作可用
btnAdd.Enabled = True
btnDel.Enabled = True
btnEdt.Enabled = True
'设置保存功能不可用
btnSav.Enabled = False
'设置记录的详细信息不可编辑
txtNo.ReadOnly = True
txtName.ReadOnly = True
txtPrice.ReadOnly = True
txtMemo.ReadOnly = True
cmbStatus.Enabled = False
cmbType.Enabled = False
'设置各个控件中的值
txtNo.Text = dgdMenu.Item(n, 0)
txtName.Text = dgdMenu.Item(n, 1)
cmbStatus.Text = IIf(dgdMenu.Item(n, 2) = "Y", "可用", "不可")
txtPrice.Text = dgdMenu.Item(n, 3)
cmbType.Text = dgdMenu.Item(n, 4)
txtMemo.Text = dgdMenu.Item(n, 5)
End Sub
Private Sub dgdMenu_CurrentCellChanged(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles dgdMenu.CurrentCellChanged
'当当前单元格变化时重新刷新窗体中控件值
FreshFrm(dgdMenu.CurrentCell.RowNumber)
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnAdd.Click
'设置文本框和组合框可用并赋初值
txtName.ReadOnly = False
txtName.Text = ""
txtNo.ReadOnly = False
txtNo.Text = ""
txtPrice.ReadOnly = False
txtPrice.Text = ""
txtMemo.ReadOnly = False
txtMemo.Text = ""
cmbStatus.Enabled = True
cmbType.Enabled = True
cmbStatus.Text = "可用"
cmbType.Text = "热菜"
'增加操作时增加、删除和修改操作都不可用
'增加操作时保存操作可用
btnAdd.Enabled = False
btnDel.Enabled = False
btnEdt.Enabled = False
btnSav.Enabled = True
'设置增加操作标志
SavAsAdd = True
End Sub
Private Sub btnEdt_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnEdt.Click
'设置文本框和组合框可用
txtName.ReadOnly = False
txtPrice.ReadOnly = False
txtMemo.ReadOnly = False
cmbStatus.Enabled = True
cmbType.Enabled = True
'编辑操作时增加、删除和修改操作都不可用
'编辑操作时保存操作可用
btnAdd.Enabled = False
btnDel.Enabled = False
btnEdt.Enabled = False
btnSav.Enabled = True
SavAsAdd = False
End Sub
Private Sub btnSav_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnSav.Click
'对增加操作进行保存
If SavAsAdd = True Then
If txtName.Text = "" Then
'新增加的记录必须有菜肴名称
MsgBox("菜肴的名称是需要要输入的!", MsgBoxStyle.Exclamation)
txtName.Focus()
Exit Sub
Else
If txtNo.Text = "" Then
'新增加的记录必须有菜肴编号
MsgBox("菜肴的编号是需要要输入的!", MsgBoxStyle.Exclamation)
txtNo.Focus()
Exit Sub
Else
If txtPrice.Text = "" Then
'新增加的记录必须有菜肴价格
MsgBox("价格字段是需要要输入的!", MsgBoxStyle.Exclamation)
txtPrice.Focus()
Exit Sub
Else
'输入检测无误后可以提交数据
Try
Dim strSql = "insert into Menu " & "values ('" & _
Trim(txtNo.Text) & "','" & Trim(txtName.Text) & "','" & _
IIf(cmbStatus.Text = "可用", "Y", "N") & _
"',convert(smallmoney," & "'" & Trim(txtPrice.Text) & _
"'),'" & Trim(cmbType.Text) & "','" & Trim(txtMemo.Text) & "')"
'按照顺序将用户输入的数据插入到相应的字段中
'调用DB类中方法进行数据表更新
odb.UpdateDataBase(strsql)
'DataGrid重新检索更新后的数据
dgdMenu.DataSource = odb.CreateDataTable(sqlSel)
MsgBox("增加操作成功!", MsgBoxStyle.Information)
Catch mm As Exception
'捕捉异常
MsgBox(mm.Message)
Exit Sub
End Try
End If
End If
End If
Else
'对编辑操作进行保存
If txtName.Text = "" Then
'编辑的记录必须有菜肴名称
MsgBox("菜肴的名称是需要要输入的!", MsgBoxStyle.Exclamation)
txtName.Focus()
Exit Sub
Else
If txtPrice.Text = "" Then
'编辑的记录必须有菜肴价格
MsgBox("价格字段是需要要输入的!", MsgBoxStyle.Exclamation)
txtPrice.Focus()
Exit Sub
Else
'输入检测无误后可以提交数据
Try
Dim strSql As String = "update menu set Name='" & Trim(txtName.Text) _
& "',Status='" & IIf(cmbStatus.Text = "可用", "Y", "N") & _
"',Price =convert(smallmoney,'" & Trim(txtPrice.Text) & _
"'),Type='" & cmbType.Text & "',Memo='" & Trim(txtMemo.Text) _
& "' where ID='" & Trim(txtNo.Text) & "'"
'按照顺序将用户输入的数据插入到相应的字段中
'调用DB类中方法进行数据表更新
odb.UpdateDataBase(strsql)
'DataGrid重新检索更新后的数据
dgdMenu.DataSource = odb.CreateDataTable(sqlSel)
MsgBox("编辑操作成功!", MsgBoxStyle.Information)
Catch mm As Exception
' 捕捉异常
MsgBox(mm.Message)
Exit Sub
End Try
End If
End If
End If
'刷新详细信息
FreshFrm(dgdMenu.CurrentRowIndex)
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnDel.Click
'确认是否删除,如果用户选择Yes,则进行删除处理;否则,不进行任何操作
If MsgBox("删除的记录不能恢复,确定要删除么?", MsgBoxStyle.Question _
+ MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Try
'以编号为关键字删除Menu数据表中记录
Dim strDel As String = "delete from menu where ID = '" & _
Trim(txtNo.Text) & "'"
'调用DB类中UpdateDateBase方法执行指定的操作
odb.UpdateDataBase(strDel)
'DataGrid表格重新检索数据
dgdMenu.DataSource = odb.CreateDataTable(sqlSel)
MsgBox("删除成功!", MsgBoxStyle.Information)
Catch mm As Exception
'异常处理
MsgBox(mm.Message)
Exit Sub
End Try
End If
'刷新详细信息
FreshFrm(0)
End Sub
Private Sub btnExt_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnExt.Click
Me.Close()
End Sub
Private Sub txtPrice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPrice.TextChanged
If Not (Val(Trim(txtPrice.Text) > 0)) Then
MsgBox("价格必须是正数!", MsgBoxStyle.Exclamation)
txtPrice.Focus()
'选中优惠金额文本框中数字部分
txtPrice.Select(0, Len(txtPrice.Text))
Exit Sub
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -