摘要:GetCheckBoxTag
因為每張form都有很多Control
所以為了不想programmer 花時間為每個Control 作setting
就開發了這個function

"GetCheckBoxTag_Master"#Region "GetCheckBoxTag_Master"
' Function purpose : Get all CheckBox's Tag, Add CheckChangeHandler to CheckBox
' and Group CheckBox Together(Child and parent)
' We store the Feecode in Tag,so we does not need to hard code the FeeCode list
' 2008-11-28 - add a property by hoi, Disable ChkBox at initital stage, Enable based on Specimens 'LS_Code' 
Public Sub GetCheckBoxTag_Master() Sub GetCheckBoxTag_Master(ByVal pform As System.Windows.Forms.Form, ByRef psTagList As String)
Try
psTagList = BLANK
Call GetCheckBoxTag_SearchingChk(pform.Controls, psTagList)
psTagList = "(" + psTagList + ")"
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Function bCheckTag() Function bCheckTag(ByVal psTag As String) As Boolean
Try
bCheckTag = False
If psTag <> BLANK Then
If psTag.IndexOf("chk") = -1 Then
bCheckTag = True
End If
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Function 
Private Sub GetCheckBoxTag_SearchingChk() Sub GetCheckBoxTag_SearchingChk(ByVal pControls As System.windows.Forms.Control.ControlCollection, ByRef psTagList As String)
Try
Dim lcControl As Control
For Each lcControl In pControls
Select Case lcControl.GetType.Name
Case "Panel"
If bCheckTag(lcControl.Tag) = True And bExistCode(gsPanelTag, lcControl.Tag) = False Then
' gsPanelTag : store the panel'tag which contains child feecode
gsPanelTag = sCombineCode(gsPanelTag, lcControl.Tag)
End If
End Select
If lcControl.Controls.Count > 0 Then
GetCheckBoxTag_SearchingChk(lcControl.Controls, psTagList)
Else
Select Case lcControl.GetType.Name
Case "CheckBox"
If bCheckTag(lcControl.Tag) = True Then
' add CheckedChanged event
Dim lCheckBox As CheckBox = lcControl
AddHandler lCheckBox.CheckedChanged, AddressOf GetCheckBoxTag_ChkHandler
' psTagList : Store the CheckBox's Tag which is used for SQL,so we need to use another method which is different from sCombineCode. sCombineCode's Code is used in VB only
If psTagList = BLANK Then
psTagList = VarSQLString(lcControl.Tag)
Else
psTagList += LOE_FEE_CODE_SEPARATOR_DESC + VarSQLString(lcControl.Tag)
End If
' add by hoi 2008-11-28
lCheckBox.Enabled = False
End If
End Select
End If
Next
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub GetCheckBoxTag_ChkHandler() Sub GetCheckBoxTag_ChkHandler(ByVal pChkBox As Object, ByVal e As System.EventArgs)
Try
If pChkBox.tag = BLANK Then Exit Sub
If pChkBox.checked = True Then
' Store the Change
gsSaveFeeCodeTemp = sCombineCode(gsSaveFeeCodeTemp, pChkBox.tag)
gsCancelFeeCodeTemp = sRemoveCode(gsCancelFeeCodeTemp, pChkBox.tag)
Call UpdateRootChildChkBox(pChkBox, gsPanelTag, True)
Else
Call UpdateRootChildChkBox(pChkBox, gsPanelTag, False)
Call UpdateRootChkBox(pChkBox.parent.parent, pChkBox.parent.tag, False, pChkBox.tag)
gsSaveFeeCodeTemp = sRemoveCode(gsSaveFeeCodeTemp, pChkBox.tag)
gsCancelFeeCodeTemp = sCombineCode(gsCancelFeeCodeTemp, pChkBox.tag)
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub UpdateRootChildChkBox() Sub UpdateRootChildChkBox(ByVal pChkBox As Object, ByVal psPanelTag As String, ByVal pbchkBoolean As Boolean)
Try
' Case : Based on RootChkBox is checked or not ,refresh the ChildChkBox
If psPanelTag.IndexOf(vntFixNull(pChkBox.tag)) > -1 Then
UpdateChildChkBoxInPanel(pChkBox.parent, pChkBox.tag, pbchkBoolean)
End If
If pbchkBoolean = True Then
' Case : if all ChildChkBox is checked , set RootChkBox.checked = true
If pChkBox.parent.GetType.Name = "Panel" Then
If pChkBox.parent.tag <> BLANK Then
If psPanelTag.IndexOf(vntFixNull(pChkBox.parent.tag)) > -1 Then
If bAllChildChecked(pChkBox.parent) = True Then
UpdateRootChkBox(pChkBox.parent.parent, pChkBox.parent.tag, True, BLANK)
End If
End If
End If
End If
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Function bAllChildChecked() Function bAllChildChecked(ByVal pcPanelControl As System.windows.Forms.Control) As Boolean
' when ChildChkBox is checked, then find out it's parent is Panel or not
' if it is Panel, then pass the Panel into this function
' then return the checking on all ChildChkBox in Panel is checked or not.
Try
bAllChildChecked = False
Dim lchkBox As CheckBox
Dim lsCancelFeeCodeTemp As String = BLANK
Select Case pcPanelControl.GetType.Name
Case "Panel"
If pcPanelControl.Tag <> BLANK Then
For Each lcPanelChildControl As Control In pcPanelControl.Controls
Select Case lcPanelChildControl.GetType.Name
Case "CheckBox"
lchkBox = lcPanelChildControl
If lchkBox.Checked = False Then
Exit Function
End If
' Spec : When RootChkBox is checked, all ChildBox must Checked too,
' but We only Store the RootChkBox's FeeCode
' solution : We add the ChildChkBox into Cancel List to achieve it
lsCancelFeeCodeTemp = sCombineCode(lsCancelFeeCodeTemp, lchkBox.Tag)
End Select
Next
End If
End Select
bAllChildChecked = True
gsCancelFeeCodeTemp = sCombineCode(gsCancelFeeCodeTemp, lsCancelFeeCodeTemp)
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Function 
Public Sub UpdateRootChkBox() Sub UpdateRootChkBox(ByVal pcGrpControl As System.windows.Forms.Control, ByRef psRootchkTag As String, ByVal pbChkBoolean As Boolean, ByRef psChildTag As String)
' psChildTag was required only when pbChkBoolean = false
Try
If psRootchkTag = BLANK Then Exit Sub
Dim lChkBox As CheckBox
If pbChkBoolean = True Then
' refresh the RootChkBox to Checked
For Each lcControl As Control In pcGrpControl.Controls
Select Case lcControl.GetType.Name
Case "CheckBox"
If bCheckTag(lcControl.Tag) = True Then
If lcControl.Tag = psRootchkTag Then
lChkBox = lcControl
lChkBox.Checked = pbChkBoolean
Exit Sub
End If
End If
End Select
Next
Else
' refresh the RootChkBox to unChecked
' as all child box will be unchecked too
' therefore, we have to check them again- -
For Each lcControl As Control In pcGrpControl.Controls
Select Case lcControl.GetType.Name
Case "CheckBox"
If bCheckTag(lcControl.Tag) = True Then
If lcControl.Tag = psRootchkTag Then
lChkBox = lcControl
If lChkBox.Checked = True Then
lChkBox.Checked = pbChkBoolean
' check the ChildChkBox again
For Each lcPanelControl As Control In pcGrpControl.Controls
Select Case lcPanelControl.GetType.Name
Case "Panel"
If lcPanelControl.Tag <> BLANK Then
For Each lcPanelChildControl As Control In lcPanelControl.Controls
Select Case lcPanelChildControl.GetType.Name
Case "CheckBox"
lChkBox = lcPanelChildControl
If lChkBox.Tag <> psChildTag Then
lChkBox.Checked = True
End If
End Select
Next
End If
End Select
Next
Else
lChkBox.Checked = pbChkBoolean
End If
Exit Sub
End If
End If
End Select
Next
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub UpdateChildChkBoxInPanel() Sub UpdateChildChkBoxInPanel(ByVal pcGrpControl As System.windows.Forms.Control, ByRef psPanelTag As String, ByVal pbChkBoolean As Boolean)
' when the RootChkBox is checked
' then pass it's parent(GroupBox) to this function
' then find out the Panel in GroupBox
' For each CheckBox in Panel
' Uncheck Or CHeck all box
' it depends on pbChkBoolean
Try
Dim lchkBox As CheckBox
For Each lcControl As Control In pcGrpControl.Controls
Select Case lcControl.GetType.Name
Case "Panel"
If bCheckTag(lcControl.Tag) = True Then
If lcControl.Tag = psPanelTag Then
For Each lcPanelChildControl As Control In lcControl.Controls
Select Case lcPanelChildControl.GetType.Name
Case "CheckBox"
lchkBox = lcPanelChildControl
lchkBox.Checked = pbChkBoolean
If pbChkBoolean = True Then
gsChildFeeCode = sCombineCode(gsChildFeeCode, lchkBox.Tag)
Else
gsChildFeeCode = sRemoveCode(gsChildFeeCode, lchkBox.Tag)
End If
End Select
Next
End If
End If
End Select
Next
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
#End Region
------------------
熱愛生命 喜愛新奇 有趣的事物
過去 是無法改變
將來 卻能夠創造
希望使大家生活更便利
世界更美好
a guy who loves IT and life