|
Object-Oriented Programming |
Top Previous Next |
|
VB.NET is a powerful OOP language, but Normal Accounting Software seldom employ OOP in their software. One key introduction of OOP in VB.NET is the visual inheritance, which greatly increase the software value in terms of manageability and extendability.
Figure 16
StdForm - The base form for the whole system StdDBForm - Inherited from StdForm, this is the base form for all Database related base form. StdDBTabForm - Inherited from StdDBForm, this is the base form for all Database related form with Tab. StdDBTranForm - Inherited from from StdDBTabForm, this is the base form for all the Master Settings and Transaction related forms.
Figure 17
Such as GL form, most of the data processing property are defined in the StdDBForm and consume at design time.
Example of processing code in StdDBForm - Public Class StdDBForm Public AllowAdd As Boolean Public AllowEdit As Boolean Public AllowDel As Boolean Private m_AllowPrint As Boolean Public Property AllowPrint() As Boolean Get Return m_AllowPrint End Get Set(ByVal value As Boolean) m_AllowPrint = value tsbtnPrint.Enabled = m_AllowPrint End Set End Property
Private m_PrintEngine As iCSReport Property PrintEngine() As iCSReport Get Return m_PrintEngine End Get Set(ByVal value As iCSReport) m_PrintEngine = value m_PrintEngine.ParentForm = MdiParent End Set End Property
Public DBStatus As FormDBStatus = FormDBStatus.FormBrowse
Protected m_KeyInfo As FormKeyInfo Public Overridable Property KeyInfo() As FormKeyInfo Get Return m_KeyInfo End Get Set(ByVal value As FormKeyInfo) m_KeyInfo = value End Set End Property
Protected ErrorControlList As ArrayList = New ArrayList Protected Sub ClearErrorControlList() For Each MyControl As Control In ErrorControlList ErrorProvider1.SetError(MyControl, "") Next End Sub
Public FormsSecurityInfo As ArrayList Public Event tsbtnSaveClick As EventHandler Public Event tsbtnCancelClick As EventHandler Public Event tsbtnEditClick As EventHandler Public Event tsbtnNewClick As EventHandler Public Event BeforeNewAndEdit As BeforeAfterNewAndEditEvent Public Event AfterNewAndEdit As BeforeAfterNewAndEditEvent Public Event BeforeDelete As CancelEventHandler Public Event AfterDelete As EventHandler Public Event BeforesbtnSaveclick As CancelEventHandler Public Event BeforesbtnCancelClick As CancelEventHandler Public Event AftertsbtnSaveclick As EventHandler Public Event AftertsbtnCancelClick As EventHandler Public Event BeforeValidateSave As EventHandler Public Event AfterValidateSave As EventHandler Public Event BeforeCtrlEnableSave As EventHandler Public Event AfterCtrlEnableSave As EventHandler Public Event tsbtnPrintClick As EventHandler
Public Function ValidateTextBlank(ByVal InControl As Control, ByVal ErrorMsg As String) As Boolean If BindingNavigator1.BindingSource.Count > 0 Then If InControl.Text.Length = 0 Then SetFormError(InControl, ErrorMsg) Return False Else SetFormError(InControl, "") Return True End If End If End Function
Public Overridable ReadOnly Property FirstControl() As Control Get Return Nothing End Get End Property
Private m_Dataset As DataSet Public Overridable Property Dataset() As DataSet Get Return m_Dataset End Get Set(ByVal value As DataSet) m_Dataset = value End Set End Property
Protected NeedSave As Boolean = False
Public Overridable Sub DBControlsEnabled(ByVal InValue As Boolean)
End Sub
Protected Overridable Sub CheckNvgButtons() If NeedSave = True Then BindingNavigatorMoveFirstItem.Enabled = False BindingNavigatorMoveNextItem.Enabled = False BindingNavigatorMovePreviousItem.Enabled = False BindingNavigatorMoveLastItem.Enabled = False BindingNavigatorAddNewItem.Enabled = False Else BindingNavigator1.MoveFirstItem = Nothing BindingNavigator1.MoveFirstItem = BindingNavigatorMoveFirstItem 'Reset one of the button to cause refresh on navigator buttons BindingNavigatorAddNewItem.Enabled = BindingNavigator1.BindingSource IsNot Nothing End If End Sub
Protected Sub SaveCancelEnabled(ByVal InValue As Boolean) NeedSave = InValue tsbtnSave.Enabled = InValue tsbtnCancel.Enabled = InValue tsbtnEdit.Enabled = Not InValue CheckNvgButtons() End Sub
Private m_Source As BindingSource
Protected Sub BindDataSource(ByVal InSource As BindingSource) ErrorProvider1.DataSource = InSource BindingNavigator1.BindingSource = InSource m_Source = InSource AddHandler BindingNavigator1.BindingSource.PositionChanged, AddressOf BindingSource_PositionChanged End Sub
Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click If AllowDel Then DBStatus = FormDBStatus.FormDel Dim MyCancel As CancelEventArgs = New CancelEventArgs MyCancel.Cancel = False RaiseEvent BeforeDelete(sender, MyCancel) If MyCancel.Cancel = False Then If BindingNavigator1.BindingSource IsNot Nothing Then If MessageBox.Show("Confirm Delete ?", "Delete Alert", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.OK Then Dim MyRow As DataRowView = CType(BindingNavigator1.BindingSource.Current, DataRowView) If MyRow IsNot Nothing Then MyRow.Delete() NeedSave = True DBControlsEnabled(False) SaveCancelEnabled(True) CheckNvgButtons() RaiseEvent AfterDelete(sender, e) End If End If End If End If End If End Sub
Protected Sub tsbtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnSave.Click Dim ValidResult As Boolean = True
If BindingNavigator1.BindingSource IsNot Nothing Then If BindingNavigator1.BindingSource.Count > 0 Then RaiseEvent BeforeValidateSave(sender, e) ValidResult = Validate() If ValidResult Then ValidResult = ValidateChildren() End If RaiseEvent AfterValidateSave(sender, e) End If End If
If ValidResult Then Dim MyCancelArgs As CancelEventArgs = New CancelEventArgs
MyCancelArgs.Cancel = False
If DBStatus = FormDBStatus.FormAdd Then Dim MySource As BindingSource = BindingNavigator1.BindingSource Dim MyTable As DataTable = Dataset.Tables(MySource.DataMember) If MyTable.Columns(0).Unique Then If MyTable.Columns(0).DataType Is System.Type.GetType("System.String") Then Dim MyRow() As DataRow = MyTable.Select(MyTable.Columns(0).ColumnName + _ "='" + CStr(CType(MySource.Current, DataRowView)(MyTable.Columns(0).ColumnName)) + "'") If MyRow.Length > 0 Then MsgBox("Code / No already exists !") If FirstControl IsNot Nothing Then FirstControl.Select() End If MyCancelArgs.Cancel = True End If End If End If End If
If MyCancelArgs.Cancel = False Then RaiseEvent BeforesbtnSaveclick(sender, MyCancelArgs) End If
If MyCancelArgs.Cancel = False Then If ErrorControlList.Count > 0 Then MsgBox("Please clear all error before save !") Exit Sub End If
Try NeedSave = False RaiseEvent tsbtnSaveClick(sender, e) RaiseEvent BeforeCtrlEnableSave(sender, e) DBControlsEnabled(NeedSave) RaiseEvent AfterCtrlEnableSave(sender, e) SaveCancelEnabled(NeedSave) RaiseEvent AftertsbtnSaveclick(sender, e) DBStatus = FormDBStatus.FormBrowse Catch ex As Exception If TypeOf ex Is System.Data.ConstraintException Then MsgBox("Code already exists !") Else MsgBox(ex.Message) End If NeedSave = True RaiseEvent BeforeCtrlEnableSave(sender, e) DBControlsEnabled(NeedSave) RaiseEvent AfterCtrlEnableSave(sender, e) SaveCancelEnabled(NeedSave) End Try End If End If End Sub
Private Sub tsbtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnCancel.Click Dim MyCancelArgs As CancelEventArgs = New CancelEventArgs
MyCancelArgs.Cancel = False
RaiseEvent BeforesbtnCancelClick(sender, MyCancelArgs)
If MyCancelArgs.Cancel = False Then Try NeedSave = False RaiseEvent tsbtnCancelClick(sender, e) DBControlsEnabled(NeedSave) SaveCancelEnabled(NeedSave) RaiseEvent AftertsbtnCancelClick(sender, e) ClearErrorControlList() DBStatus = FormDBStatus.FormBrowse Catch ex As Exception MsgBox(ex.Message) NeedSave = True DBControlsEnabled(NeedSave) SaveCancelEnabled(NeedSave) End Try End If End Sub
Private Sub BindingNavigatorAddNewItem_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.EnabledChanged BindingNavigatorDeleteItem.Enabled = BindingNavigatorAddNewItem.Enabled AddToolStripMenuItem.Enabled = BindingNavigatorAddNewItem.Enabled End Sub
Private Sub StdDBForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing If NeedSave Then If MessageBox.Show("Data not save, continue to close form ?", "Data not save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then e.Cancel = True Else e.Cancel = False End If End If End Sub
Private Sub tsbtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnEdit.Click If AllowEdit Then DBStatus = FormDBStatus.FormEdit Dim MyEvent As BeforeAfterEditNewEventAgrs = New BeforeAfterEditNewEventAgrs MyEvent.IsNew = False MyEvent.Cancel = False RaiseEvent BeforeNewAndEdit(sender, MyEvent) If MyEvent.Cancel = False Then NeedSave = True DBControlsEnabled(True) SaveCancelEnabled(NeedSave) RaiseEvent tsbtnEditClick(sender, e) RaiseEvent AfterNewAndEdit(sender, MyEvent) If FirstControl IsNot Nothing Then FirstControl.Select() End If End If End If End Sub
Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click If BindingNavigator1.BindingSource IsNot Nothing Then If AllowAdd Then DBStatus = FormDBStatus.FormAdd Dim MyEvent As BeforeAfterEditNewEventAgrs = New BeforeAfterEditNewEventAgrs MyEvent.IsNew = True MyEvent.Cancel = False RaiseEvent BeforeNewAndEdit(sender, MyEvent) If MyEvent.Cancel = False Then BindingNavigator1.BindingSource.AddNew() NeedSave = True DBControlsEnabled(True) SaveCancelEnabled(True) CheckNvgButtons() RaiseEvent AfterNewAndEdit(sender, MyEvent) If FirstControl IsNot Nothing Then FirstControl.Select() End If End If End If End If
If AllowAdd Then RaiseEvent tsbtnNewClick(sender, e) End If End Sub
Private Sub StdDBForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DBControlsEnabled(False) End Sub
Private Sub AddToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem.Click BindingNavigatorAddNewItem_Click(sender, e) End Sub
Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click BindingNavigatorDeleteItem_Click(sender, e) End Sub
Private Sub EditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.Click tsbtnEdit_Click(sender, e) End Sub
Private Sub BindingNavigatorMoveFirstItem_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveFirstItem.EnabledChanged FirstToolStripMenuItem.Enabled = BindingNavigatorMoveFirstItem.Enabled End Sub
Private Sub BindingNavigatorMovePreviousItem_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMovePreviousItem.EnabledChanged PreviousToolStripMenuItem.Enabled = BindingNavigatorMovePreviousItem.Enabled End Sub
Private Sub BindingNavigatorMoveNextItem_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveNextItem.EnabledChanged NextToolStripMenuItem.Enabled = BindingNavigatorMoveNextItem.Enabled End Sub
Private Sub BindingNavigatorMoveLastItem_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveLastItem.EnabledChanged LastToolStripMenuItem.Enabled = BindingNavigatorMoveLastItem.Enabled End Sub
Private Sub BindingNavigatorDeleteItem_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.EnabledChanged DeleteToolStripMenuItem.Enabled = BindingNavigatorDeleteItem.Enabled End Sub
Private Sub tsbtnEdit_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnEdit.EnabledChanged EditToolStripMenuItem.Enabled = tsbtnEdit.Enabled End Sub
Private Sub tsbtnSave_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnSave.EnabledChanged SaveToolStripMenuItem.Enabled = tsbtnSave.Enabled End Sub
Private Sub tsbtnCancel_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnCancel.EnabledChanged CancelToolStripMenuItem.Enabled = tsbtnCancel.Enabled End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click tsbtnSave_Click(sender, e) End Sub
Private Sub CancelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelToolStripMenuItem.Click tsbtnCancel_Click(sender, e) End Sub
Protected Sub BindingSource_PositionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) CheckNvgButtons() End Sub
Private Sub BindingNavigator1_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigator1.EnabledChanged DataNavigatorToolStripMenuItem.Enabled = BindingNavigator1.Enabled End Sub
Public Overrides Sub CallAfterFormInit() MyBase.CallAfterFormInit() BindingNavigatorAddNewItem.Enabled = AllowAdd tsbtnEdit.Enabled = AllowEdit BindingNavigatorDeleteItem.Enabled = AllowDel End Sub
Public Sub SetFormError(ByVal InControl As Control, ByVal InErrMsg As String) ErrorProvider1.SetError(InControl, InErrMsg) If InErrMsg <> "" Then If ErrorControlList.IndexOf(InControl) = -1 Then ErrorControlList.Add(InControl) End If Else ErrorControlList.Remove(InControl) End If End Sub
Public Sub ForceUpperGridColumnTextbox(ByVal InDataGridView As DataGridView, ByVal e As DataGridViewEditingControlShowingEventArgs, ByVal ColumnControlName As String) 'Force grid view control for acc# to be uppercase all the time If TypeOf e.Control Is TextBox Then If InDataGridView.Columns(InDataGridView.CurrentCell.ColumnIndex).Name = ColumnControlName Then CType(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper Else CType(e.Control, TextBox).CharacterCasing = CharacterCasing.Normal End If End If End Sub
Public Function CheckUnique(ByVal InTable As DataTable, ByVal InBinding As BindingSource, _ ByVal InControl As Control, ByVal KeyNo As String, ByVal KeyCode As String, ByVal ErrMsg As String) As Boolean Dim MyRow() As DataRow = InTable.Select(KeyCode + "='" + InControl.Text + "'") If MyRow.Length > 0 Then If CInt(MyRow(0)(KeyNo)) <> CInt(CType(InBinding.Current, DataRowView)(KeyNo)) Then SetFormError(InControl, ErrMsg) Return False Else SetFormError(InControl, "") Return True End If Else Return True End If End Function
Private Sub tsbtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnPrint.Click If BindingNavigator1.BindingSource IsNot Nothing Then Dim MySource As BindingSource = BindingNavigator1.BindingSource If MySource.Count > 0 Then RaiseEvent tsbtnPrintClick(sender, e) End If End If End Sub
Public Sub PrintReport(ByVal InReportName As String, Optional ByVal InDataset As DataSet = Nothing, Optional ByVal FilterKey As String = "") PrintEngine.ReportName = InReportName PrintEngine.SystemManager = SystemManager PrintEngine.SystemInfo = SystemInfo PrintEngine.Preview(InDataset, FilterKey) End Sub
Public Function GetFormSecurityInfo(ByVal InClassName As String) As FormCreateInfoDef Dim ReturnValue As FormCreateInfoDef = Nothing
If FormsSecurityInfo IsNot Nothing Then For Each MyInfo As FormCreateInfoDef In FormsSecurityInfo If MyInfo.ClassName = InClassName Then ReturnValue = MyInfo End If Next End If
Return ReturnValue End Function
Public Function CallForm(ByVal InFormName As String, ByVal TableName As String) As FormKeyInfo Dim MyInfo As FormCreateInfoDef = GetFormSecurityInfo(InFormName)
Dim MyFormsInfo As FormsInfoDef = _ CType(Activator.CreateInstance(MyInfo.AssemblyName, MyInfo.AssemblyName + ".FormsInfo").Unwrap, FormsInfoDef)
Dim CurrentForm As StdDBForm
CurrentForm = CType(MyFormsInfo.CreateForm(MyInfo.ClassName), StdDBForm)
CurrentForm.SystemInfo = SystemInfo CurrentForm.AllowAdd = MyInfo.AllowAdd CurrentForm.AllowEdit = MyInfo.AllowEdit CurrentForm.AllowDel = MyInfo.AllowDel CurrentForm.AllowPrint = MyInfo.AllowPrint CurrentForm.Text = MyInfo.Description CurrentForm.SystemManager = SystemManager CurrentForm.FormsSecurityInfo = FormsSecurityInfo CurrentForm.CallAfterFormInit() CurrentForm.ShowDialog()
Dim MyKeyInfo As FormKeyInfo
MyKeyInfo.KeyNo = CurrentForm.KeyInfo.KeyNo MyKeyInfo.KeyCode = CurrentForm.KeyInfo.KeyCode
Dataset.Tables(TableName).Clear() Dataset.Tables(TableName).Merge(CurrentForm.Dataset.Tables(TableName)) Dataset.Tables(TableName).AcceptChanges()
CurrentForm = Nothing
Return MyKeyInfo End Function End Class
Public Delegate Sub BeforeAfterNewAndEditEvent(ByVal Sender As Object, ByVal e As BeforeAfterEditNewEventAgrs)
Public Class BeforeAfterEditNewEventAgrs Inherits EventArgs Public IsNew As Boolean Public Cancel As Boolean End Class
Public Structure FormKeyInfo Public KeyNo As Integer Public KeyCode As String End Structure
Public Enum FormDBStatus FormAdd FormEdit FormDel FormBrowse End Enum |