This document is archived and information here might be outdated.  Recommended version.


Disabling or validating label edits in the TOCControl (ArcObjects .NET 10.5 SDK)

Disabling or validating label edits in the TOCControl


Summary
This topic shows how to disable and validate edits made to the TOCControl's labels.

About disabling or validating label edits in the TOCControl

To allow a user to edit TOCControl labels without programmatically validating the changes, set the ESRI.ArcGIS.Controls.AxTOCControlLabelEdit property to esriTOCControlAutomatic. This is the default value.
To allow a user to edit TOCControl labels and validate their changes, set the ESRI.ArcGIS.Controls.AxTOCControl LabelEdit property to esriTOCControlManual. This setting triggers OnBeginLabelEdit and OnEndLabelEdit events, which can be handled. See the following code:
[C#]
axTOCControl1.LabelEdit=esriTOCControlEdit.esriTOCControlAutomatic;
axTOCControl1.LabelEdit=esriTOCControlEdit.esriTOCControlManual;
[VB.NET]
AxTOCControl1.LabelEdit=esriTOCControlEdit.esriTOCControlAutomatic
AxTOCControl1.LabelEdit=esriTOCControlEdit.esriTOCControlManual
In the following code example, if label editing is set to manual, the HitTest method is used within the OnBeginLabelEdit event to determine the type of label being edited. In this code, only layer labels can be edited. The OnEndLabelEdit event ensures there are no empty layer labels.
[C#]
private void axTOCControl1_OnBeginLabelEdit(object sender,
    ESRI.ArcGIS.Controls.ITOCControlEvents_OnBeginLabelEditEvent e)
{
    IBasicMap map=null;
    ILayer layer=null;
    object other=null;
    object index=null;
    esriTOCControlItem item=esriTOCControlItem.esriTOCControlItemNone;
    //Determine what kind of item has been clicked.
    axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref
        index);
    //Only layer items can have their labels edited.
    if (item != esriTOCControlItem.esriTOCControlItemLayer)
    {
        e.canEdit=false;
    }
}

private void axTOCControl1_OnEndLabelEdit(object sender,
    ESRI.ArcGIS.Controls.ITOCControlEvents_OnEndLabelEditEvent e)
{
    //Prevent empty labels.
    if (e.newLabel.Trim() == "")
    {
        e.canEdit=false;
    }
}
[VB.NET]
Private Sub AxTOCControl1_OnBeginLabelEdit(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.ITOCControlEvents_OnBeginLabelEditEvent) Handles AxTOCControl1.OnBeginLabelEdit
    Dim pMap As Map=Nothing
    Dim pLayer As ILayer=Nothing
    Dim pOther As Object=Nothing
    Dim pItem As esriTOCControlItem
    Dim pIndex As Object=Nothing
    'Determine what kind of item has been clicked.
    AxTOCControl1.HitTest(e.x, e.y, pItem, CType(pMap, IBasicMap), pLayer, pOther, pIndex)
    'Only layer items can have their labels edited.
    If pItem <> esriTOCControlItem.esriTOCControlItemLayer Then
        e.canEdit=False
    End If
End Sub


Private Sub AxTOCControl1_OnEndLabelEdit(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.ITOCControlEvents_OnEndLabelEditEvent) Handles AxTOCControl1.OnEndLabelEdit
    'Prevent empty labels.
    If Trim(e.newLabel)="" Then
        e.canEdit=False
    End If
End Sub


See Also:

TOCControl
ITOCControl




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced