Editor for WinForms | ComponentOne
Creating an XHTML Editor in Code / The ToolStripStyles class / Bold, Italic, Underline, Clear Formatting
In This Topic
    Bold, Italic, Underline, Clear Formatting
    In This Topic

    The bold, italic, underline, and clear formatting commands are very easy to implement, they simply delegate the command to corresponding methods in the C1Editor:

    To write code in C#

    C#
    Copy Code
    void Bold_Click(object sender, EventArgs e)
    {
        _btnBold.Checked = SelectionFontBold = !SelectionFontBold;
    }
    void Italic_Click(object sender, EventArgs e)
    {
        _btnItalic.Checked = SelectionFontItalic = !SelectionFontItalic;
    }
    void Underline_Click(object sender, EventArgs e)
    {
        _btnUnderline.Checked = SelectionFontUnderline = !SelectionFontUnderline;
    }
    void ClearFormatting_Click(object sender, EventArgs e)
    {
        Selection s = Editor.Selection;
        s.ClearFormatting();
    }
    

    The first three commands use helper properties defined by the ToolStripStyles class: SelectionFontBold, SelectionFontItalic, and SelectionFontUnderline. These helper properties are implemented as follows:

    To write code in C#

    C#
    Copy Code
    private bool SelectionFontBold
    {
        get 
        { 
            return Editor.Mode == EditorMode.Design 
                ? Editor.Selection.IsTagApplied("strong") 
                : false; 
        }
        set
        {
            if (value)
            {
                Editor.Selection.ApplyTag("strong");
            }
            else
            {
                Editor.Selection.RemoveTag("strong");
            }
        }
    }
    

    The implementation uses the C1Editor's Selection property, which returns a Selection object that represents the current selection. The Selection object has methods to apply, remove, and check whether formatting tags are applied to the selection.

    The same logic used above is used to implement the SelectionFontItalic and SelectionFontUnderline helper properties.