Friday, December 2, 2011

Writing Control Validation in common place without writing on every control's event

Writing Control Validation in common place without writing on every control's event

here i'm going to allowing only numeric validation, i'm going to write this code in ControlValidation Class. its my own class.

Public Sub Allow_Control_Numeric(ByRef Cntrl As Control, ByVal pMsgTitle As String, Optional ByVal pNoOfDecimal As Integer = 0, Optional ByVal AllowNegative As Boolean = False, Optional ByVal AllowBlank As Boolean = True, Optional ByVal isCorrectFormatRequired As Boolean = True, Optional ByVal AllowZero As Boolean = True, Optional ByVal pIsQtyColumn As Boolean = False, Optional ByVal pTxtValidationCntrl As Control = Nothing, Optional ByVal pIsReadOnlyCntrl As Boolean = False, Optional ByVal pTxtValidationCntrlFlag As ValidationColumnFlag = ValidationColumnFlag.PartNo, Optional ByVal pIsPriceColumn As Boolean = False)
        Dim _controlValidation = New ControlValidation
        _controlValidation._Txt_Cntrl = Cntrl
        _controlValidation._MsgTitle = pMsgTitle
        _controlValidation._NoOfDecimal = pNoOfDecimal
        _controlValidation._AllowNegative = AllowNegative
        _controlValidation._AllowBlank = AllowBlank
        _controlValidation._isCorrectFormatRequired = isCorrectFormatRequired
        _controlValidation._AllowZero = AllowZero
        _controlValidation._IsQtyColumn = pIsQtyColumn
        _controlValidation._Txt_Validation_Cntrl = pTxtValidationCntrl
        _controlValidation._validationFlag = pTxtValidationCntrlFlag
        _controlValidation._IsPriceColumn = pIsPriceColumn

        If _gQtyValidationClasses.Contains(_stackTrace.GetFrames(1).GetMethod.ReflectedType.Name) Or _gQtyValidationClasses.Contains("*") Then
            _controlValidation._QtyAllowClass = True
        Else
            _controlValidation._QtyAllowClass = False
        End If

        '' To Set max Length for all Qty Controls
        If pIsQtyColumn Then
            If TypeOf Cntrl Is TextBox Then
                If _controlValidation._QtyAllowClass = True Then
                    CType(Cntrl, TextBox).MaxLength = _gQtyTotalSize
                Else
                    CType(Cntrl, TextBox).MaxLength = _gQtyTotalSizeOthers
                End If
            End If
        ElseIf pIsPriceColumn Then
            If TypeOf Cntrl Is TextBox Then
                CType(Cntrl, TextBox).MaxLength = _gPriceNumberSize + _controlValidation._NoOfDecimal + 1
            End If
        End If

        RemoveHandler Cntrl.KeyUp, AddressOf _controlValidation.Control_KeyUp
        RemoveHandler Cntrl.Validated, AddressOf _controlValidation.Control_Validated
        AddHandler Cntrl.KeyUp, AddressOf _controlValidation.Control_KeyUp
        AddHandler Cntrl.Validated, AddressOf _controlValidation.Control_Validated
    End Sub
   
    Private Sub Control_KeyUp()
        If _IsQtyColumn Then
            If _QtyAllowClass Then
                _NoOfDecimal = Get_Part_UOM_Result(_Txt_Validation_Cntrl.Text, _validationFlag)
            Else
                _NoOfDecimal = _gQtyDecimalSizeOthers
            End If
        End If
        _Txt_Cntrl.Text = _validation.Check_Numeric(_Txt_Cntrl.Text, _MsgTitle, _NoOfDecimal, _AllowNegative, _AllowBlank, True, False, _AllowZero, _IsQtyColumn, _QtyAllowClass, _IsPriceColumn)
    End Sub

 Private Sub Control_Validated()
        If _IsQtyColumn Then
            If _QtyAllowClass Then
                _NoOfDecimal = Get_Part_UOM_Result(_Txt_Validation_Cntrl.Text, _validationFlag)
            Else
                _NoOfDecimal = _gQtyDecimalSizeOthers
            End If
        End If
        _Txt_Cntrl.Text = _validation.Check_Numeric(_Txt_Cntrl.Text, _MsgTitle, _NoOfDecimal, _AllowNegative, _AllowBlank, True, _isCorrectFormatRequired, _AllowZero, _IsQtyColumn, _QtyAllowClass, _IsPriceColumn)
    End Sub

In validation class,

Public Function Check_Numeric(ByVal pValue As String, ByVal pMsgTitle As String, Optional ByVal pNoOfDecimal As Integer = 0, Optional ByVal AllowNegative As Boolean = False, Optional ByVal AllowBlank As Boolean = True, Optional ByVal pErrMsgRequired As Boolean = True, Optional ByVal isCorrectFormatRequired As Boolean = True, Optional ByVal AllowZero As Boolean = True, Optional ByVal pIsQty As Boolean = False, Optional ByVal pQtyAllowClass As Boolean = False, Optional ByVal pIsPrice As Boolean = False) As String  ',  ByVal pMsgBoxTitle As String, Optional ByVal pFrac As Boolean = False) As Char
        ' Checking Fractions value it must be >= 0
        If Not (pNoOfDecimal >= 0 And pNoOfDecimal <= 99) Then
            If pErrMsgRequired Then
                MessageBox.Show(My.Resources.E709, pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            Return ""
        End If


        '' Checking Numerics
        If IsNumeric(pValue) Or (pValue = "-" And AllowNegative = True) Or (pValue = "." And pNoOfDecimal > 0) Then

            If pIsQty Then   ' Qty Validation
                Dim _QtyDecValue As String = ""
                Dim _QtyNoValue As String = ""
                'Dim _RetValue As String = ""

                If Len(pValue) > IIf(pQtyAllowClass = True, _gQtyTotalSize, _gQtyTotalSizeOthers) Then
                    MessageBox.Show(My.Resources.E1606 & " " & IIf(pQtyAllowClass = True, _gQtyTotalSize, _gQtyTotalSizeOthers), pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return ""
                End If

                _QtyNoValue = Mid(pValue, 1, IIf(InStr(pValue, ".") > 0, InStr(pValue, ".") - 1, Len(pValue)))
                _QtyDecValue = IIf(InStr(pValue, ".") > 0, Mid(pValue, InStr(pValue, ".") + 1), "")

                If Len(_QtyNoValue) > IIf(pQtyAllowClass = True, _gQtyNumberSize, _gQtyNumberSizeOthers) Or Len(_QtyDecValue) > pNoOfDecimal Then
                    If Not pQtyAllowClass Then
                        MessageBox.Show(My.Resources.E1607 & " (" & IIf(pQtyAllowClass = True, _gQtyNumberSize, _gQtyNumberSizeOthers) & " , " & pNoOfDecimal & " ) ", pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Else
                        If pNoOfDecimal <= 0 Then
                            MessageBox.Show(My.Resources.E1607 & " (" & IIf(pQtyAllowClass = True, _gQtyNumberSize, _gQtyNumberSizeOthers) & " , " & pNoOfDecimal & " ) " & My.Resources.E1608, pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                        Else
                            MessageBox.Show(My.Resources.E1607 & " (" & IIf(pQtyAllowClass = True, _gQtyNumberSize, _gQtyNumberSizeOthers) & " , " & pNoOfDecimal & " ) " & My.Resources.E1609, pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End If
                    End If
                    '_RetValue = Mid(_QtyNoValue, 1, _gQtyNumberSize)
                    'If Len(_QtyDecValue) > pNoOfDecimal Then
                    '    If pNoOfDecimal > 0 Then
                    '        _RetValue = _RetValue & "." & Mid(_QtyDecValue, 1, pNoOfDecimal)
                    '    End If
                    'End If
                    Return ""
                End If
            End If

            If pIsPrice Then
                'Dim _PriceDecValue As String = ""
                Dim _PriceNoValue As String = ""

                _PriceNoValue = Mid(pValue, 1, IIf(InStr(pValue, ".") > 0, InStr(pValue, ".") - 1, Len(pValue)))
                '_PriceDecValue = IIf(InStr(pValue, ".") > 0, Mid(pValue, InStr(pValue, ".") + 1), "")
                If Len(_PriceNoValue) > _gPriceNumberSize Then
                    MessageBox.Show(My.Resources.E1610 & " (" & _gPriceNumberSize & " , " & pNoOfDecimal & " ) ", pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return ""
                End If
            End If


            If AllowNegative = False Then  ' checking if allownegative is false it should be positive values
                If Val(pValue) < 0 Then 'Or Val(FormatNumber(pValue, pNoOfDecimal, TriState.True, , TriState.False)) < 0 Then
                    If pErrMsgRequired Then
                        MessageBox.Show(My.Resources.E710, pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                    Return "" ''Math.Abs(Val(pValue)) ''Commented by Ravi on 12-09-2011. just clear,no need to correct the format. 
                End If
                If IsNumeric(pValue) Then
                    If Val(FormatNumber(pValue, pNoOfDecimal, TriState.True, , TriState.False)) < 0 Then
                        If pErrMsgRequired Then
                            MessageBox.Show(My.Resources.E710, pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End If
                        Return ""
                    End If
                End If
            End If




            ' To get first . (dot) position
            Dim _DotExists As Boolean = False
            Dim _DotPosition = InStr(pValue, ".")

            If _DotPosition = 0 Then
                _DotPosition = Len(pValue)
            Else
                _DotExists = True
            End If

            ' getting Decimal values after .
            Dim _DecimalValue = Mid(pValue, _DotPosition + 1, Len(pValue))
            'Dim _IntValue As Integer

            'If _DotExists Then
            '    _IntValue = Mid(pValue, 1, _DotPosition - 1)
            'Else
            '    _IntValue = Mid(pValue, 1, _DotPosition)
            'End If

            If Not AllowZero Then
                If pValue <> "-" And pValue <> "." Then
                    If Val(pValue) = 0 Then
                        MessageBox.Show(My.Resources.E1611, pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                        Return ""
                    End If
                End If
            End If

            If (Len(_DecimalValue) > pNoOfDecimal) Or (pNoOfDecimal = 0 And _DotExists = True) Then
                ' careful focus when changing this conditions
                If pErrMsgRequired Then
                    If pNoOfDecimal = 0 Then
                        MessageBox.Show(My.Resources.E711, pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Else
                        MessageBox.Show(My.Resources.E712 & pNoOfDecimal, pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                End If
                If pValue = "-" Or pValue = "." Then
                    Return pValue
                Else
                    If isCorrectFormatRequired Then
                        Return "" 'FormatNumber(pValue, pNoOfDecimal, TriState.True, , TriState.False)  '' No need to round off
                    Else
                        Return ""
                    End If
                End If

            End If


            '' when no error , it comes here..
            '' Convert valid numeric value to correct format
            If isCorrectFormatRequired Then
                If pValue = "." Or pValue = "-" Then
                    Return ""
                Else
                    Return FormatNumber(pValue, pNoOfDecimal, TriState.True, , TriState.False)
                End If
            Else
                Return pValue
            End If
        Else
            If Trim(pValue) = "" And AllowBlank = True Then
                '' to skip when numeric values can allow blank values
            Else
                If pErrMsgRequired Then
                    MessageBox.Show(My.Resources.E713, pMsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
                Return ""
            End If
        End If
        Return pValue

    End Function



Callling this function :
_controlValidation.Allow_Control_Numeric(_Value, MsgTitle, _2, False, True, False)

No comments:

Post a Comment