338 lines
13 KiB
VB.net
338 lines
13 KiB
VB.net
Imports System.Windows.Forms
|
|
|
|
Public Class BaseEditForm
|
|
Enum EditingOrAdding
|
|
Editing
|
|
Adding
|
|
Loading
|
|
NoAction
|
|
End Enum 'EditingOrAdding
|
|
|
|
Private mvalEditingOrAdding As EditingOrAdding
|
|
Private mintCurrentSortColumn As Integer
|
|
Private mobjEditFormExistingItemColumnSortRules() As clsGridColumnSortRule
|
|
|
|
Private Sub BaseEditForm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
|
|
MyGlobals.FlushCachedObjects()
|
|
End Sub
|
|
|
|
Private Sub BaseEditForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
|
If Me.DesignMode Then
|
|
MessageBox.Show("The BaseEditForm class must be inherited, and contains methods that must be overridden." + vbCrLf + "They have been declared in the base class and will throw NotImplementedException at runtime if not overridden.")
|
|
End If
|
|
MyGlobals.FlushCachedObjects()
|
|
lvwExistingItemsList.Sorting = SortOrder.None
|
|
mintCurrentSortColumn = 0
|
|
mvalEditingOrAdding = EditingOrAdding.NoAction
|
|
LoadExistingItemsList()
|
|
InitializeDetailControls()
|
|
EnableDisableControls()
|
|
'Dim lobjColumnClickEventArgs As New System.Windows.Forms.ColumnClickEventArgs(0)
|
|
'lvwExistingItemsList_ColumnClick(Me, lobjColumnClickEventArgs)
|
|
lvwExistingItemsList.Height = fraAddEdit.Top - lvwExistingItemsList.Top - 10
|
|
End Sub
|
|
|
|
Protected Overridable Sub LoadExistingItemsList()
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
End Sub
|
|
|
|
Protected Overridable Sub InitializeDetailControls()
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
End Sub
|
|
|
|
Protected Overridable Sub EmptyOutDetailControls()
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
End Sub
|
|
|
|
Protected Overridable Function DetailControlsHaveSignificantData() As Boolean
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
Return False
|
|
End Function
|
|
|
|
Protected Overridable Function DetailControlsHaveDifferentDataThanExistingData(ByVal lstvwExistingItem As ListViewItem) As Boolean
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
Return False
|
|
End Function
|
|
|
|
Protected Overridable Function DeleteExistingItem(ByVal lstvwItemToDelete As ListViewItem) As Boolean
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
Return True
|
|
End Function
|
|
|
|
Protected Overridable Function ConfirmOKToDeleteExistingItem(ByVal lstvwItemToDelete As ListViewItem) As Boolean
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
Return True
|
|
End Function
|
|
|
|
Protected Overridable Function SaveNewItem() As Boolean
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
Return True
|
|
End Function
|
|
|
|
Protected Overridable Function SaveNewDataForExistingItem(ByVal lstvwExistingItem As ListViewItem) As Boolean
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
Return True
|
|
End Function
|
|
|
|
Protected Overridable Function LoadExistingDataToDetailControls(ByVal lstvwExistingItem As ListViewItem) As Boolean
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
Return True
|
|
End Function
|
|
|
|
Protected Overridable Sub SetFocusToFirstDetailControlForEdit()
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
End Sub
|
|
|
|
Protected Overridable Sub UserRequestedToExit()
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
End Sub
|
|
|
|
Protected Overridable Sub EditOrAddStarting()
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
End Sub
|
|
|
|
Protected Overridable Sub EditOrAddCompleted()
|
|
If Not Me.DesignMode Then
|
|
Throw New NotImplementedException
|
|
End If
|
|
End Sub
|
|
|
|
Protected ReadOnly Property ExistingObjectsListItems() As ListView.ListViewItemCollection
|
|
Get
|
|
Return lvwExistingItemsList.Items
|
|
End Get
|
|
End Property
|
|
|
|
Protected ReadOnly Property ExistingObjectsListColumns() As ListView.ColumnHeaderCollection
|
|
Get
|
|
Return lvwExistingItemsList.Columns
|
|
End Get
|
|
End Property
|
|
|
|
Protected ReadOnly Property GetEditingOrAdding() As EditingOrAdding
|
|
Get
|
|
Return mvalEditingOrAdding
|
|
End Get
|
|
End Property
|
|
|
|
Protected Property MyBaseEditFormColumnSortRules() As clsGridColumnSortRule()
|
|
Get
|
|
Return mobjEditFormExistingItemColumnSortRules
|
|
End Get
|
|
Set(ByVal iValue As clsGridColumnSortRule())
|
|
mobjEditFormExistingItemColumnSortRules = iValue
|
|
End Set
|
|
End Property
|
|
Private Sub EnableDisableControls()
|
|
Select Case mvalEditingOrAdding
|
|
Case EditingOrAdding.Loading
|
|
Exit Sub
|
|
Case EditingOrAdding.Adding, EditingOrAdding.NoAction
|
|
If DetailControlsHaveSignificantData() Then
|
|
If btnCancel.Enabled = False Then
|
|
EditOrAddStarting()
|
|
End If
|
|
mvalEditingOrAdding = EditingOrAdding.Adding
|
|
lvwExistingItemsList.Enabled = False
|
|
btnEdit.Enabled = False
|
|
btnDelete.Enabled = False
|
|
btnExit.Enabled = False
|
|
fraAddEdit.Text = "Add..."
|
|
btnSave.Enabled = True
|
|
btnCancel.Enabled = True
|
|
btnRevert.Enabled = False
|
|
Else
|
|
If btnCancel.Enabled = True Then
|
|
EditOrAddCompleted()
|
|
End If
|
|
mvalEditingOrAdding = EditingOrAdding.NoAction
|
|
lvwExistingItemsList.Enabled = True
|
|
btnEdit.Enabled = True
|
|
btnDelete.Enabled = True
|
|
btnExit.Enabled = True
|
|
fraAddEdit.Text = "Add..."
|
|
btnSave.Enabled = False
|
|
btnCancel.Enabled = False
|
|
btnRevert.Enabled = False
|
|
End If
|
|
Case EditingOrAdding.Editing
|
|
If btnCancel.Enabled = False Then
|
|
EditOrAddStarting()
|
|
End If
|
|
lvwExistingItemsList.Enabled = False
|
|
btnEdit.Enabled = False
|
|
btnDelete.Enabled = False
|
|
btnExit.Enabled = False
|
|
fraAddEdit.Text = "Edit..."
|
|
btnCancel.Enabled = True
|
|
If DetailControlsHaveDifferentDataThanExistingData(lvwExistingItemsList.SelectedItems(0)) Then
|
|
btnSave.Enabled = True
|
|
btnRevert.Enabled = True
|
|
Else
|
|
btnSave.Enabled = False
|
|
btnRevert.Enabled = False
|
|
End If
|
|
End Select
|
|
If Not Me.DesignMode Then
|
|
If btnCancel.Enabled Then
|
|
Me.CancelButton = btnCancel
|
|
Else
|
|
Me.CancelButton = Nothing
|
|
End If
|
|
If btnSave.Enabled Then
|
|
Me.AcceptButton = btnSave
|
|
Else
|
|
Me.AcceptButton = Nothing
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
|
|
Private Sub btnEdit_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click
|
|
DoEditClick()
|
|
End Sub
|
|
|
|
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
|
|
If lvwExistingItemsList.SelectedItems.Count = 0 Then
|
|
MessageBox.Show("Must select an item to delete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
|
|
Exit Sub
|
|
End If
|
|
If ConfirmOKToDeleteExistingItem(lvwExistingItemsList.SelectedItems(0)) Then
|
|
If DeleteExistingItem(lvwExistingItemsList.SelectedItems(0)) Then
|
|
lvwExistingItemsList.Items.Remove(lvwExistingItemsList.SelectedItems(0))
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
|
|
EmptyOutDetailControls()
|
|
mvalEditingOrAdding = EditingOrAdding.NoAction
|
|
EnableDisableControls()
|
|
SetFocusToFirstDetailControlForEdit()
|
|
End Sub
|
|
|
|
Private Sub btnRevert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRevert.Click
|
|
LoadExistingDataToDetailControls(lvwExistingItemsList.SelectedItems(0))
|
|
SetFocusToFirstDetailControlForEdit()
|
|
End Sub
|
|
|
|
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
|
|
If mvalEditingOrAdding = EditingOrAdding.Adding Then
|
|
If SaveNewItem() Then
|
|
mvalEditingOrAdding = EditingOrAdding.NoAction
|
|
EmptyOutDetailControls()
|
|
EnableDisableControls()
|
|
End If
|
|
Else
|
|
If SaveNewDataForExistingItem(lvwExistingItemsList.SelectedItems(0)) Then
|
|
mvalEditingOrAdding = EditingOrAdding.NoAction
|
|
EmptyOutDetailControls()
|
|
EnableDisableControls()
|
|
End If
|
|
End If
|
|
SetFocusToFirstDetailControlForEdit()
|
|
End Sub
|
|
|
|
Private Sub DoEditClick()
|
|
If lvwExistingItemsList.SelectedItems.Count = 0 Then
|
|
MessageBox.Show("Must select an item to edit", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
|
|
Exit Sub
|
|
End If
|
|
|
|
mvalEditingOrAdding = EditingOrAdding.Loading
|
|
If LoadExistingDataToDetailControls(lvwExistingItemsList.SelectedItems(0)) Then
|
|
mvalEditingOrAdding = EditingOrAdding.Editing
|
|
EnableDisableControls()
|
|
Else
|
|
mvalEditingOrAdding = EditingOrAdding.NoAction
|
|
EmptyOutDetailControls()
|
|
Exit Sub
|
|
End If
|
|
End Sub
|
|
|
|
Protected Sub DetailDataHasChanged()
|
|
EnableDisableControls()
|
|
End Sub
|
|
|
|
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
|
|
UserRequestedToExit()
|
|
End Sub
|
|
|
|
Private Sub lvwExistingItemsList_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvwExistingItemsList.ColumnClick
|
|
mintCurrentSortColumn = e.Column
|
|
lvwExistingItemsList.ListViewItemSorter = New clsGridSorter(e.Column, mobjEditFormExistingItemColumnSortRules)
|
|
End Sub
|
|
|
|
Private Sub lvwExistingItemsList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwExistingItemsList.DoubleClick
|
|
DoEditClick()
|
|
End Sub
|
|
|
|
Private Sub BaseEditForm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
|
|
lvwExistingItemsList.Height = fraAddEdit.Top - lvwExistingItemsList.Top - 10
|
|
End Sub
|
|
|
|
Private Sub fraAddEdit_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles fraAddEdit.Resize
|
|
lvwExistingItemsList.Height = fraAddEdit.Top - lvwExistingItemsList.Top - 10
|
|
End Sub
|
|
|
|
Private Sub BaseEditForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
|
|
If mobjEditFormExistingItemColumnSortRules Is Nothing Then
|
|
Exit Sub
|
|
End If
|
|
|
|
Dim intSavedSortColumn As Integer = mintCurrentSortColumn
|
|
'If there is more than one column, temporarily sort by some other column
|
|
'This is because, when first shown, for some reason, it was sorting only by the first
|
|
'column, with no subsorts. I'm not sure if this is because .NET thinks that we're already
|
|
'sorted by the first column.
|
|
If mobjEditFormExistingItemColumnSortRules.Length > 1 Then
|
|
Dim intTempSortColumn As Integer
|
|
If mintCurrentSortColumn = 0 Then
|
|
intTempSortColumn = 1
|
|
Else
|
|
intTempSortColumn = 0
|
|
End If
|
|
Dim lobjTempColumnClickEventArgs As New System.Windows.Forms.ColumnClickEventArgs(intTempSortColumn)
|
|
lvwExistingItemsList_ColumnClick(Me, lobjTempColumnClickEventArgs)
|
|
End If
|
|
|
|
'Sort by the current column
|
|
Dim lobjColumnClickEventArgs As New System.Windows.Forms.ColumnClickEventArgs(intSavedSortColumn)
|
|
lvwExistingItemsList_ColumnClick(Me, lobjColumnClickEventArgs)
|
|
End Sub
|
|
|
|
Public Sub ExistingItemsListSuspendLayout()
|
|
lvwExistingItemsList.SuspendLayout()
|
|
End Sub
|
|
|
|
Public Sub ExistingItemsListResumeLayout()
|
|
lvwExistingItemsList.ResumeLayout()
|
|
End Sub
|
|
|
|
End Class |