\
ComponentOne List for WinForms
Data Presentation Techniques / Text Formatting / Formatting with a Custom Event Handler
In This Topic
    Formatting with a Custom Event Handler
    In This Topic

    On occasion, you may find that your current formatting options do not suit your particular needs. Furthermore, you may be restricted in the type of formatting that you can use, or you may need a custom formatting option. In these cases, the FormatText event option can be specified for the NumberFormat property. Choosing this option for a column will cause the FormatText event to fire each time data is about to be displayed in that column. The event allows you to reformat, translate, indent, or do anything you want to the data just prior to display:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub C1List1_FormatText(ByVal sender As Object, ByVal e As C1.Win.C1List.FormatTextEventArgs) Handles C1List1.FormatText
    

    To write code in C#

    C#
    Copy Code
    private void C1List1_FormatText(object sender, C1.Win.C1List.FormatTextEventArgs e)
    

    ColIndex, a member of the FormatTextEventArgs object, is the column number of the list to be reformatted. The Value member contains the current value of the data and serves as a placeholder for the formatted display value. For example, suppose the first column contains numeric values from 1 to 30, and you wish to display the data as Roman numerals:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub C1List1_FormatText(ByVal sender As Object, ByVal e As C1.Win.C1List.FormatTextEventArgs) Handles C1List1.FormatText
           
     Dim result As String     
        If e.ColIndex = 0 Then
    
            ' Determine how many X's. 
            While e.Value >= 10
                result = result & "X"
                e.Value = e.Value - 10
            End While
    
            ' Append "digits" 1-9.
            Select Case e.Value
                Case 1
                    result = result & "I"
                Case 2
                    result = result & "II"
                Case 3
                    result = result & "III"
                Case 4
                    result = result & "IV"
                Case 5
                    result = result & "V"
                Case 6
                    result = result & "VI"
                Case 7
                    result = result & "VII"
                Case 8
                    result = result & "VIII"
                Case 9
                    result = result & "IX"
            End Select
    
            ' Change the actual format.
            e.Value = result
        End If 
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void C1List1_FormatText( object sender, C1.Win.C1List.FormatTextEventArgs e)
           
    {  
        string  result;
        if ( e.ColIndex == 0 )   
        {
    
            // Determine how many X's.
            while ( e.Value >= 10)
            {
                result = result + "X";
                e.Value = e.Value - 10;
            }
    
            // Append "digits" 1-9.
            switch (e.Value) 
            {
                case 1;
                    result = result + "I";
                    break;
                case 2;
                    result = result + "II";
                    break;
                case 3;
                    result = result + "III";
                    break;
                case 4;
                    result = result + "IV";
                    break;
                case 5;
                    result = result + "V";
                    break;
                case 6;
                    result = result + "VI";
                    break;
                case 7;
                    result = result + "VII";
                    break;
                case 8;
                    result = result + "VIII";
                    break;
                case 9;
                    result = result + "IX";
                    break;     
            }
    
            // Change the actual format.
            e.Value = result;     
        }      
    }
    

    Since the FormatText event has fewer restrictions than other formatting techniques, you can always use it to gain full control over the textual content of any value displayed in the list.