摘要:TxtBoxNumberCheck
CheckDecimal

"TxtBoxNumberCheck_Master"#Region "TxtBoxNumberCheck_Master"
' Restrict the user to key-in Number in the TextBox
' dec0 - no decimal is allowed.
' dec1 - 1 decimal

Public Sub TxtBoxNumberCheck_Master() Sub TxtBoxNumberCheck_Master(ByVal pform As System.Windows.Forms.Form)
Try
Call TxtBoxNumberCheck_SearchTxt(pform.Controls)
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub TxtBoxNumberCheck_SearchTxt() Sub TxtBoxNumberCheck_SearchTxt(ByVal pControls As System.windows.Forms.Control.ControlCollection)
' if the name of textbox "number"
' or the Tag of textbox contain the special value "dec", "val"
Try
Dim lcControl As Control
Dim lCheckBox As CheckBox
For Each lcControl In pControls
If lcControl.Controls.Count > 0 Then
Call TxtBoxNumberCheck_SearchTxt(lcControl.Controls)
Else
Select Case lcControl.GetType.Name
Case "TextBox"
If lcControl.Tag <> BLANK Then
If lcControl.Tag.tolower.indexof("dec") > -1 Or lcControl.Tag.tolower.indexof("val") > -1 Then
AddHandler lcControl.KeyUp, AddressOf TxtBoxNumberCheck_TxtHandlerKeyUp
End If
End If
lcControl.Text = BLANK
End Select
End If
Next
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub TxtBoxNumberCheck_TxtHandlerKeyUp() Sub TxtBoxNumberCheck_TxtHandlerKeyUp(ByVal pTxtBox As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Try
Try
If pTxtBox.text <> BLANK Then
Dim liValue As Integer = CInt(pTxtBox.text)
End If
Catch ex As Exception
MsgBox("Please key-in Number.", MsgBoxStyle.Information + MsgBoxStyle.OKOnly, APP_NAME)
pTxtBox.text = BLANK
pTxtBox.focus()
Exit Sub
End Try
If pTxtBox.tag <> BLANK Then
If pTxtBox.tag.tolower.indexof("dec0") > -1 Then
Call CheckDecimal(pTxtBox, 0)
Else
If pTxtBox.tag.tolower.indexof("dec2") > -1 Then
Call CheckDecimal(pTxtBox, 2)
End If
End If
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub CheckDecimal() Sub CheckDecimal(ByVal psTxtBox As Object, ByVal piAllowDecimal As Integer)
Try
If psTxtBox.text <> BLANK Then
Dim liPosition As Integer = psTxtBox.text.indexof(".")
If liPosition > -1 Then
If piAllowDecimal = 0 Then
Dim ls As TextBox
psTxtBox.text = psTxtBox.text.replace(".", "")
psTxtBox.SelectionStart = liPosition
Else
If psTxtBox.text.length - liPosition > piAllowDecimal + 1 Then
MsgBox((piAllowDecimal).ToString & " decimal(s) is allowed", MsgBoxStyle.Information + MsgBoxStyle.OKOnly, APP_NAME)
psTxtBox.text = BLANK
psTxtBox.focus()
Exit Sub
End If
End If
End If
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
#End Region
------------------
熱愛生命 喜愛新奇 有趣的事物
過去 是無法改變
將來 卻能夠創造
希望使大家生活更便利
世界更美好
a guy who loves IT and life