Make DisplayFormat configurable for TextBoxes and GridColumns, Fix Typo, Properly Convert Dates and Numbers in Vector Fields
This commit is contained in:
118
app/TaskFlow/ClassFormat.vb
Normal file
118
app/TaskFlow/ClassFormat.vb
Normal file
@@ -0,0 +1,118 @@
|
||||
Imports System.Globalization
|
||||
Imports DevExpress.CodeParser
|
||||
Imports DevExpress.Data.Controls
|
||||
Imports DevExpress.XtraPrinting
|
||||
|
||||
Public Class ClassFormat
|
||||
Public Const CURRENCY_FORMAT = "C2"
|
||||
Public Const DECIMAL_FORMAT = "F"
|
||||
Public Const DATE_FORMAT = "d"
|
||||
Public Const DATETIME_FORMAT = "G"
|
||||
|
||||
Public Shared Function GetFormattedValue(pControlName As String, pValueObject As Object, pFormatString As String) As String
|
||||
Try
|
||||
If pFormatString <> String.Empty Then
|
||||
' https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tostring?view=net-7.0#system-datetime-tostring(system-string)
|
||||
|
||||
Select Case pFormatString
|
||||
Case ClassControlCreator.CONTROL_TYPE_CURRENCY ' 16,325.63 €
|
||||
If TypeOf pValueObject Is Double Then
|
||||
Dim oFormattedValue As Double = pValueObject
|
||||
Return oFormattedValue.ToString(CURRENCY_FORMAT)
|
||||
|
||||
ElseIf TypeOf pValueObject Is String Then
|
||||
Dim oFormattedValue As Double
|
||||
Double.TryParse(pValueObject, oFormattedValue)
|
||||
Return oFormattedValue.ToString(CURRENCY_FORMAT)
|
||||
|
||||
Else
|
||||
Return Nothing
|
||||
|
||||
End If
|
||||
|
||||
Case ClassControlCreator.CONTROL_TYPE_DOUBLE ' 16325,63
|
||||
If TypeOf pValueObject Is Double Then
|
||||
Dim oFormattedValue As Double = pValueObject
|
||||
Return oFormattedValue.ToString(DECIMAL_FORMAT)
|
||||
|
||||
ElseIf TypeOf pValueObject Is String Then
|
||||
Dim oFormattedValue As Double
|
||||
Double.TryParse(pValueObject, oFormattedValue)
|
||||
Return oFormattedValue.ToString(DECIMAL_FORMAT)
|
||||
|
||||
Else
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
Case ClassControlCreator.CONTROL_TYPE_DATE ' 15.06.2008
|
||||
Dim oFormattedValue As DateTime
|
||||
|
||||
If TypeOf pValueObject Is DateTime Then
|
||||
oFormattedValue = pValueObject
|
||||
Return oFormattedValue.ToString(DATE_FORMAT)
|
||||
|
||||
ElseIf TypeOf pValueObject Is String Then
|
||||
DateTime.TryParse(pValueObject, oFormattedValue)
|
||||
Return oFormattedValue.ToString(DATE_FORMAT)
|
||||
|
||||
Else
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
Case ClassControlCreator.CONTROL_TYPE_DATETIME ' 15.06.2008 9:15:07
|
||||
Dim oFormattedValue As DateTime
|
||||
|
||||
If TypeOf pValueObject Is DateTime Then
|
||||
oFormattedValue = pValueObject
|
||||
Return oFormattedValue.ToString(DATETIME_FORMAT)
|
||||
ElseIf TypeOf pValueObject Is String Then
|
||||
DateTime.TryParse(pValueObject, oFormattedValue)
|
||||
Return oFormattedValue.ToString(DATETIME_FORMAT)
|
||||
Else
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
Case Else ' Unknown Format String
|
||||
LOGGER.Warn("Format String [{0}] for Control [{1}] is not valid!", pFormatString, pControlName)
|
||||
Return Nothing
|
||||
|
||||
End Select
|
||||
Else
|
||||
Return Nothing
|
||||
End If
|
||||
Catch ex As Exception
|
||||
LOGGER.Warn($"Unexpected error while formatting Value for Control [{0}]", pControlName)
|
||||
LOGGER.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Converts a string according to the type information, using the invariant culture
|
||||
''' </summary>
|
||||
''' <param name="pValue"></param>
|
||||
''' <param name="pType"></param>
|
||||
''' <returns></returns>
|
||||
Public Shared Function GetConvertedValue(pValue As Object, pType As String) As Object
|
||||
Dim oConvertedValue = pValue
|
||||
|
||||
Select Case pType
|
||||
Case ClassControlCreator.CONTROL_TYPE_DOUBLE
|
||||
If Double.TryParse(pValue, NumberStyles.Float, CultureInfo.InvariantCulture, oConvertedValue) Then
|
||||
Return oConvertedValue
|
||||
End If
|
||||
|
||||
Case ClassControlCreator.CONTROL_TYPE_CURRENCY
|
||||
If Double.TryParse(pValue, NumberStyles.Currency, CultureInfo.InvariantCulture, oConvertedValue) Then
|
||||
Return oConvertedValue
|
||||
End If
|
||||
|
||||
Case ClassControlCreator.CONTROL_TYPE_INTEGER
|
||||
If Integer.TryParse(pValue, NumberStyles.Integer, CultureInfo.InvariantCulture, oConvertedValue) Then
|
||||
Return oConvertedValue
|
||||
End If
|
||||
End Select
|
||||
|
||||
Return oConvertedValue
|
||||
End Function
|
||||
End Class
|
||||
Reference in New Issue
Block a user