# Enabling and Disabling Commands

C1Editor for WinForms control allows you to easily enable or disable different commands available in the ToolStripStyles class.

## Content



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:

DOC-DETAILS-TAG-OPEN

DOC-SUMMARY-TAG-OPEN

To write code in C#

DOC-SUMMARY-TAG-CLOSE

```csharp
public override void UpdateState()
{
    if (_selectionChanged)
    {
        _selectionChanged = false;
        ShowStyles();
    }
}
```

DOC-DETAILS-TAG-CLOSE

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:

DOC-DETAILS-TAG-OPEN

DOC-SUMMARY-TAG-OPEN

To write code in C#

DOC-SUMMARY-TAG-CLOSE

```csharp
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;
        }
    }
}
```

DOC-DETAILS-TAG-CLOSE

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:

DOC-DETAILS-TAG-OPEN

DOC-SUMMARY-TAG-OPEN

To write code in C#

DOC-SUMMARY-TAG-CLOSE

```csharp
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;
}
```

DOC-DETAILS-TAG-CLOSE

The method starts by retrieving the [Selection](/componentone/api/win/online-richtexteditor/dotnet-framework-api/C1.Win.C1Editor.4.8/C1.Win.C1Editor.C1Editor.Selection.html) 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.