多个窗体拥有类似的结构和代码 应该怎样精简
发现有好几个窗体的代码结构有点类似 虽然设计界面有点不同
都有一个 btnOk 和 btnCancel 按钮 这两个按钮的事件也比较类似 只是对象不同 写法上有点类似
都有一个Datagrid控件 都有closeing事件和Load事件 写法也比较类似
只是处理的对象有点不同
打个比方 我贴两个Form
Imports System.Data.OleDbPublic Class frmSetUser Private Sub frmSetUser_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing '检查有无更改 If adminTable.Table.GetChanges IsNot Nothing Then Select Case MessageBox.Show("您已对管理员数据进行了更改,在退出前是否保存相关的修改?", "是否保存", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) Case Windows.Forms.DialogResult.Yes adminTable.Save() Case Windows.Forms.DialogResult.No adminTable.Table.RejectChanges() '撤消变更 Case Windows.Forms.DialogResult.Cancel e.Cancel = True End Select End If config.saveWindow(Me) End Sub Private Sub frmSetUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load config.initWindow(Me) '绑定数据 dgvAdmins.DataSource = adminTable.Table dgvAdmins.Columns(0).ReadOnly = True End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click '保存并退出 If adminTable.Table.GetChanges IsNot Nothing Then adminTable.Save() Me.Close() End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click Me.Close() End SubEnd Class
Imports System.Data.OleDbPublic Class frmSetGoodsClass Private Sub frmSetGoodsClass_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing '检查有无更改 If category.Table.GetChanges IsNot Nothing Then Select Case MessageBox.Show("您已对商品大类进行了更改,在退出前是否保存相关的修改?", "是否保存", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) Case Windows.Forms.DialogResult.Yes category.Save() Case Windows.Forms.DialogResult.No category.Table.RejectChanges() '撤消变更 Case Windows.Forms.DialogResult.Cancel e.Cancel = True End Select End If '退出时保存窗体大小 config.saveWindow(Me) End Sub Private Sub frmSetGoodsClass_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '初始化窗体尺寸 config.initWindow(Me) '绑定数据 dgvClass.DataSource = category.Table dgvClass.Columns(0).ReadOnly = True End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click '保存并退出 If category.Table.GetChanges IsNot Nothing Then category.Save() Me.Close() End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click Me.Close() End SubEnd Class