Back to Devexpress

How to: Create a Custom Formatter to Change the Case of String Values

windowsforms-2150-common-features-formatting-values-how-to-create-a-custom-formatter-to-change-the-case-of-string-values.md

latest3.6 KB
Original Source

How to: Create a Custom Formatter to Change the Case of String Values

  • Nov 13, 2018
  • 2 minutes to read

This sample creates a custom formatter that modifies the case of an editor’s text. The editor’s display text will obey the specified format string (the FormatInfo.FormatString property value). This string is passed to the Format method as the format parameter. If this parameter value is ‘ u ‘, the display value is converted to upper case, if the format string is ‘ l ‘, it is converted to lower case.

The following image shows the text editors’ appearance before and after sample code execution.

csharp
// A custom formatter object.
class CustomFormatter : IFormatProvider, ICustomFormatter{
    // The GetFormat method of the IFormatProvider interface.
    // This must return an object that provides formatting services for the specified type.
    public object GetFormat(System.Type type){
        return this;
    }
    // The Format method of the ICustomFormatter interface.
    // This must format the specified value according to the specified format settings.
    public string Format(string format, object arg, IFormatProvider formatProvider){
        string formatValue = arg.ToString();
        if (format == "u")
            return formatValue.ToUpper();
        else if (format == "l")
            return formatValue.ToLower();
        else return formatValue;
    }
}
// ...
// Assign the custom formatter to the editors.
textEdit1.Properties.DisplayFormat.FormatType = FormatType.Custom;
textEdit1.Properties.DisplayFormat.FormatString = "u";
textEdit1.Properties.DisplayFormat.Format = new CustomFormatter();

textEdit2.Properties.DisplayFormat.FormatType = FormatType.Custom;
textEdit2.Properties.DisplayFormat.FormatString = "l";
textEdit2.Properties.DisplayFormat.Format = new CustomFormatter();
vb
' A custom formatter object.
Class CustomFormatter : Implements IFormatProvider, ICustomFormatter
    ' The GetFormat method of the IFormatProvider interface.
    ' This must return an object that provides formatting services for the specified type.
    Public Function GetFormat(ByVal type As Type) As Object _
    Implements IFormatProvider.GetFormat
        Return Me
    End Function
    ' The Format method of the ICustomFormatter interface.
    ' This must format the specified value according to the specified format settings.
    Public Function Format(ByVal formatString As String, ByVal arg As Object, _
    ByVal formatProvider As IFormatProvider) As String Implements ICustomFormatter.Format
        Dim formatValue As String = arg.ToString()
        If (formatString = "u") Then
            Format = formatValue.ToUpper()
        ElseIf formatString = "l" Then
            Format = formatValue.ToLower()
        Else
            Format = formatValue
        End If
    End Function
End Class
' ...
' Assign the custom formatter to the editors.
TextEdit1.Properties.DisplayFormat.FormatType = FormatType.Custom
TextEdit1.Properties.DisplayFormat.FormatString = "u"
TextEdit1.Properties.DisplayFormat.Format = New CustomFormatter()

TextEdit2.Properties.DisplayFormat.FormatType = FormatType.Custom
TextEdit2.Properties.DisplayFormat.FormatString = "l"
TextEdit2.Properties.DisplayFormat.Format = New CustomFormatter()

See also: RepositoryItemTextEdit.CharacterCasing.