AccessibleObject.GetChildCount Method

Retrieves the number of children belonging to an accessible object.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public virtual int GetChildCount()

Return Value

Type: System.Int32
The number of children belonging to an accessible object.

Notes to Inheritors

All objects must support this property. The default implementation returns -1. Override this method when an accessible object needs to provide custom accessible children. When you override this method, you must also override GetChild.

The following example demonstrates the creation of an accessibility-aware chart control, using the AccessibleObject and Control.ControlAccessibleObject classes to expose accessible information. The control plots two curves along with a legend. The ChartControlAccessibleObject class, which derives from ControlAccessibleObject, is used in the CreateAccessibilityInstance method to provide custom accessible information for the chart control. Since the chart legend is not an actual Control -based control, but instead is drawn by the chart control, it does not have any built-in accessible information. Because of this, the ChartControlAccessibleObject class overrides the GetChild method to return the CurveLegendAccessibleObject that represents accessible information for each part of the legend. When an accessible-aware application uses this control, the control can provide the necessary accessible information.

This code demonstrates shows overriding the GetChildCount method. See the AccessibleObject class overview for the complete code example.

// Inner class ChartControlAccessibleObject represents accessible information associated with the ChartControl. 
// The ChartControlAccessibleObject is returned in the ChartControl.CreateAccessibilityInstance override. 
public class ChartControlAccessibleObject : ControlAccessibleObject
{
    ChartControl chartControl;

    public ChartControlAccessibleObject(ChartControl ctrl) : base(ctrl) 
    {
        chartControl = ctrl;
    }

    // Gets the role for the Chart. This is used by accessibility programs. 
    public override AccessibleRole Role
    {  
        get {
            return AccessibleRole.Chart;
        }
    }

    // Gets the state for the Chart. This is used by accessibility programs. 
    public override AccessibleStates State
    {  
        get {                    
            return AccessibleStates.ReadOnly;
        }
    }

    // The CurveLegend objects are "child" controls in terms of accessibility so 
    // return the number of ChartLengend objects. 
    public override int GetChildCount()
    {  
        return chartControl.Legends.Length;
    }

    // Gets the Accessibility object of the child CurveLegend idetified by index. 
    public override AccessibleObject GetChild(int index)
    {  
        if (index >= 0 && index < chartControl.Legends.Length) {
            return chartControl.Legends[index].AccessibilityObject;
        }                
        return null;
    }

    // Helper function that is used by the CurveLegend's accessibility object 
    // to navigate between sibiling controls. Specifically, this function is used in 
    // the CurveLegend.CurveLegendAccessibleObject.Navigate function. 
    internal AccessibleObject NavigateFromChild(CurveLegend.CurveLegendAccessibleObject child, 
                                                AccessibleNavigation navdir) 
    {  
        switch(navdir) {
            case AccessibleNavigation.Down:
            case AccessibleNavigation.Next:
                return GetChild(child.ID + 1);

            case AccessibleNavigation.Up:
            case AccessibleNavigation.Previous:
                return GetChild(child.ID - 1);                        
        }
        return null;
    }

    // Helper function that is used by the CurveLegend's accessibility object 
    // to select a specific CurveLegend control. Specifically, this function is used 
    // in the CurveLegend.CurveLegendAccessibleObject.Select function. 
    internal void SelectChild(CurveLegend.CurveLegendAccessibleObject child, AccessibleSelection selection) 
    {   
        int childID = child.ID;

        // Determine which selection action should occur, based on the 
        // AccessibleSelection value. 
        if ((selection & AccessibleSelection.TakeSelection) != 0) {
            for(int i = 0; i < chartControl.Legends.Length; i++) {
                if (i == childID) {
                    chartControl.Legends[i].Selected = true;                        
                } else {
                    chartControl.Legends[i].Selected = false;
                }
            }

            // AccessibleSelection.AddSelection means that the CurveLegend will be selected. 
            if ((selection & AccessibleSelection.AddSelection) != 0) {
                chartControl.Legends[childID].Selected = true;                        
            }

            // AccessibleSelection.AddSelection means that the CurveLegend will be unselected. 
            if ((selection & AccessibleSelection.RemoveSelection) != 0) {
                chartControl.Legends[childID].Selected = false;                        
            }
        }            
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft