FlexSheet for WPF | ComponentOne
Customizing Appearance / Customizing Cells / Cell Formatting / Formatting Font
In This Topic
    Formatting Font
    In This Topic

    You might want to change the appearance of the text in cell(s) so that the text stands out from the rest. In C1FlexSheet, you can easily format the font of text in a cell by changing the font style, font family, font size, and font color.

    SetCellFormat method is used to set the format for the cell in C1FlexSheet.

    The following code uses SetCellFormat method to change the font family of the font in a cell:

    If flex IsNot Nothing Then
       flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontFamily, 
                          fontFamilyCombo.SelectedValue)
    End If
    
    if (flex != null)
        flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontFamily,
                           fontFamilyCombo.SelectedValue);
    

    To change the font size, first you need to set the size limit of the font. The following code uses SetCellFormat method to change the font size after setting the limit:

    If flex IsNot Nothing Then
       flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontSize,
                          fontSize.SelectedValue)
    End If
    
    if (flex != null)
        flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontSize,
                           fontSize.SelectedValue);
    

    The following code illustrates the use of SetCellFormat method to change the Font style:

    If flex IsNot Nothing Then
       Select Case fontStyle.SelectedValue.ToString()
          Case "Bold"
             flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontWeight, FontWeights.Bold)
             Exit Select
          Case "Italic"
             flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontStyle, FontStyles.Italic)
             Exit Select
          Case "BoldAndItalic"
             flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontWeight, FontWeights.Bold)
             flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontStyle, FontStyles.Italic)
             Exit Select
          Case Else
             flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontStyle, FontStyles.Normal)
             flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontWeight, FontWeights.Normal)
             Exit Select
       End Select
    End If
    
    if (flex != null)
    {
        switch (fontStyle.SelectedValue.ToString())
        {
            case "Bold": flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontWeight,
                                            FontWeights.Bold);
                break;
            case "Italic": flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontStyle,
                                              FontStyles.Italic);
                break;
            case "BoldAndItalic": flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontWeight,
                                                     FontWeights.Bold);
                flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontStyle, FontStyles.Italic);
                break;
            default: flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontStyle, FontStyles.Normal);
                flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontWeight, FontWeights.Normal);
                break;
        }
    }
    

    You can create your own color picker using system colors and use its reference in the code to change the font color. The following code can then be used to change the font color:

    If flex IsNot Nothing Then
       flex.SetCellFormat(flex.Selection.Cells, CellFormat.Foreground,
                          New SolidColorBrush(obj))
    End If
    
    if (flex != null)
        flex.SetCellFormat(flex.Selection.Cells, CellFormat.Foreground, new SolidColorBrush(obj));