Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


How to Build Accessible Windows Forms Applications with .NET : Page 3

Open your favorite .NET application, then close your eyes and try using the program. Tough, isn't it? But that's what using your applications may be like for disabled computer users. As a responsible developer, you can solve this problem by using these techniques to add Active Accessibility features to your application.

Build a Sample Application
Figure 1. Sample Accessibility App: The figure shows the sample application displaying a button, some values, and the custom bar chart control.
For this article I've prepared a small sample application with a standard Button control and a custom control representing a bar char (see Figure 1).

Accessibility Information for Existing Controls
It's almost trival to implement Active Accessibility for existing framework-supplied controls. All you have to do is supply values for the four properties AccessibleName, AccessibleDescription, AccessibleDefaultActionDescription, and AccessibleRole.

You can set three of these properties in the Properties window as shown in Figure 2; however, you can't set the AccessibleDefaultActionDescription property via the properties window. Instead, you can set it with one line of code:

this.button1.AccessibleDefaultActionDescription = "some description about what will happen " + "when you press this button";

Figure 2. Accessible Properties: The figure shows a portion of the properties window in Visual Studio containing Active Accessibility properties.
Accessibility Information for Custom Controls
If you require more accessible information to be included with your control (for example, simple elements for child controls) you can write your own class that derives from the AccessibleObject or Control.ControlAccessibleObject classes. You can find those in the Accessibility namespace. You must add a reference to the Accessibility assembly installed with the .NET Framework to get access to the AccessibleObject.

The source showing this is far too long for this article, but you can download the full source, so I'll explain only the most important parts of the code here.

First you have to derive your class from Control.ControlAccessibleObject and write a constructor that supplies the base class with a reference to the parent of the AccessibleObject. The parent is always the control that you are extending with accessible information.

public class AccSampleCtrlAccessibleObject : Control.ControlAccessibleObject { private AccSampleCtrl ctrl; public AccSampleCtrlAccessibleObject( AccSampleCtrl owner) : base(owner) { ctrl = owner; } }

To supply custom accessibility information you have to override the CreateAccessibilityInstance() method of your control and return your custom AccessibleObject instead of the default one:

protected override AccessibleObject CreateAccessibilityInstance() { return new AccSampleCtrlAccessibleObject(this); }

Next, you'll need to override several properties and methods of the base class AccessibleObject as in the following code.

Author's Note: It's important to keep in mind that you also have to override the GetChildCount method if you are overriding the GetChild method!

public override string Name { get { string name = ctrl.AccessibleName; if(name!=null) { return name; } return ctrl.Text; } set { base.Name = value; } } public override AccessibleRole Role { get { return AccessibleRole.Chart; } } public override AccessibleStates State { get { return AccessibleStates.ReadOnly; } } public override int GetChildCount() { return ctrl.Legends.Length; } public override AccessibleObject GetChild(int index) { if ( index >= 0 && index < ctrl.Legends.Length ) return ctrl.Legends[index].AccessibilityObject; return null; }

When overriding the methods from AccessibleObject don't forget that every Active Accessibility object must support navigation. The most common form of navigation is logical navigation, which is implemented within the method NavigateFromChild shown below. Note that this method is declared as internal—there's no need to call this method from outside the control's assembly.

internal AccessibleObject NavigateFromChild( AccSampleCtrlLegend. AccSampleCtrlLegendAccessibleObject 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; }

To implement spatial navigation you use the directional constants in the AccessibleNavigation enumeration—Up, Down, Left, and Right. Note that the method presented here is called from the overridden method navigate which could also be used directly to implement the navigation.

These were the tough parts of the custom control code. For the complete implementation of the accessibility sample control check out the accompanying download.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date