[]
        
(Showing Draft Content)

Formatting Font

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:

vbnet

If flex IsNot Nothing Then
   flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontFamily, 
                      fontFamilyCombo.SelectedValue)
End If

csharp

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:

vbnet

If flex IsNot Nothing Then
   flex.SetCellFormat(flex.Selection.Cells, CellFormat.FontSize,
                      fontSize.SelectedValue)
End If

csharp

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:

vbnet

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

csharp

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:

vbnet

If flex IsNot Nothing Then
   flex.SetCellFormat(flex.Selection.Cells, CellFormat.Foreground,
                      New SolidColorBrush(obj))
End If

csharp

if (flex != null)
    flex.SetCellFormat(flex.Selection.Cells, CellFormat.Foreground, new SolidColorBrush(obj));