Editor for WinForms | ComponentOne
Creating an XHTML Editor in Code / The ToolStripStyles class / Enabling and Disabling Commands
In This Topic
    Enabling and Disabling Commands
    In This Topic

    The commands in the ToolStripStyles class need to be updated when the selection changes, so the bold button is checked when the selected text is bold for example.

    As before, this task is performed by the UpdateState method:

    To write code in C#

    C#
    Copy Code
    public override void UpdateState()
    {
        if (_selectionChanged)
        {
            _selectionChanged = false;
            ShowStyles();
        }
    }
    

    To avoid updating the toolstrip commands too frequently, we use a selectionChanged flag to keep track of changes in the selection. If there were any changes since the last time the method was invoked, then the ShowStyles method is called. Here is the implementation:

    To write code in C#

    C#
    Copy Code
    void ShowStyles()
    {
        // show inline styles
        Selection s = Editor.Selection;
        if (s != null)
        {
            _btnBold.Checked = SelectionFontBold;
            _btnItalic.Checked = SelectionFontItalic;
            _btnUnderline.Checked = SelectionFontUnderline;
        }
     
         // find selected style
        XmlNode node = GetSelectedNode();
        if (node == null)
        {
            _cmbStyle.SelectedIndex = 0;
        }
        else
        {
            bool found = false;
            while (node != null && !found)
            {
                string style = string.Format("<{0}>", node.Name);
                foreach (string item in _cmbStyle.Items)
                {
                    if (item.IndexOf(style) > -1)
                    {
                        _cmbStyle.Text = item;
                        found = true;
                        break;
                    }
                }
                node = node.ParentNode;
            }
        }
    }
    

    The method starts by updating the state of the bold, italic, and underline buttons using the helper properties described earlier. Then it calls the GetSelectedNode to retrieve the XmlNode that represents the current selection and looks for the matching node type in the cmbStyle combo box. If a match is not found, parent nodes are scanned until a match is found or until we reach the top level node in the document.

    The GetSelectedNode method is implemented as follows:

    To write code in C#

    C#
    Copy Code
    XmlNode GetSelectedNode()
    {
         // return node if start and end nodes are the same
        Selection selRange = Editor.Selection;
        if (selRange != null)
        {
            XmlNode startNode = selRange.Start.Node;
            XmlNode endNode = selRange.End.Node;
            return object.Equals(startNode, endNode)
                ? startNode
                : null;
        }
        return null;
    }
    

    The method starts by retrieving the Selection property, then checking that its start and end nodes are the same. If the selection spans multiple nodes, the method returns null which indicates there is no single style representing the selection.