Microsoft KB Archive/819351

= How to create accessible controls by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 819351

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework 2.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Background information
 * Create a sample control
 * Test the sample control
 * REFERENCES



SUMMARY
This step-by-step article describes how to create an accessible control that is derived from the Control.ControlAccessibleObject class in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
 * Microsoft Active Accessibility

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
 * Microsoft Visual Basic .NET or Microsoft Visual Basic 2005

back to the top

Background information
Microsoft Active Accessibility is a developer technology that may help to improve the way that programs and operating systems work with assistive technology products. This technology was first integrated in the Microsoft Windows operating system in Microsoft Windows 98. Microsoft continues to update this technology with each new Windows release.

Microsoft Windows Forms has built-in Active Accessibility support. Windows Forms provides information about your application that permits your application to work with assistive technology products. You can use the following property values of a control to provide additional information to assistive technology products:
 * AccessibleName
 * AccessibleDescription
 * AccessibleDefaultActionDescription
 * AccessibleRole

Alternatively, if you require more Active Accessibility information to be included with your control, you can write your own class that is derived from the AccessibleObject class or from the Control.ControlAccessibleObject class.

The Control.ControlAccessibleObject class inherits from the AccessibleObject class. AccessibleObject enables you to provide information about a control to an assistive technology product. Control.ControlAccessibleObject enables you to provide information about a control to an assistive technology product by exposing a set of standard properties. For example, Control.ControlAccessibleObject may expose the Role property, the Location property, and others. Active Accessibility uses these properties to expose information to assistive technology products.

Active Accessibility uses Windows Events (WinEvents) to notify assistive technology products of user interface changes in a control, such as a change in the name, in the state, or in the value of a user interface element.

For more information about Microsoft Active Accessibility architecture, visit the following Microsoft Web sites:

Microsoft Active Accessibility: Architecture

http://msdn2.microsoft.com/en-us/library/ms971310.aspx

Microsoft Active Accessibility Architecture: Part 2

http://msdn2.microsoft.com/en-us/library/ms971352.aspx

back to the top

Create a sample control
To create a custom CheckBox control (MyCheckBox) and to create a custom Control.ControlAccessibleObject class (MyCheckBoxAccessibleObject) that the custom CheckBox control uses to provide accessibility information to Active Accessibility applications, follow these steps:  Open Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, click New, and then click Project to display the New Project dialog box. In the Project Type window, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. In the Templates window, click Windows Control Library. In the Name box, type MyCheckBox, and then click OK to create the project. Add a reference to the Active Accessibility assembly that is installed with the Microsoft .NET Framework. To do this, follow these steps:  On the Project menu, click Add Reference.</li> On the .NET tab, double-click Accessibility.dll.</li> Click OK to close the Add Reference dialog box.</li></ol>

Note The AccessibleObject class implements the IAccessible interface. The IAccessible interface is defined by the Active Accessibility assembly. Therefore, you must add a reference to the Active Accessibility assembly that is installed with the .NET Framework.</li>  Replace the content of UserControl1.vb with the following code: Imports System Imports System.Windows.Forms Imports Accessibility Imports System.Drawing

Namespace MyCustomControls Public Class MyCheckBox Inherits CheckBox

Public Sub New ' Make the check box look similar to a toggle button. Me.Appearance = Appearance.Button ' Center the text on the button. Me.TextAlign = ContentAlignment.MiddleCenter End Sub

' Create an instance of the AccessibleObject that is        ' defined for the 'MyCheckBox' control. Protected Overrides Function CreateAccessibilityInstance _ As AccessibleObject Return New MyCheckBoxAccessibleObject(Me) End Function End Class

' Accessible object for use with the 'MyCheckBox' control. Friend Class MyCheckBoxAccessibleObject Inherits Control.ControlAccessibleObject

Public Sub New(ByVal owner As MyCheckBox) MyBase.New(owner) End Sub

Public Overrides ReadOnly Property DefaultAction As String Get ' Return the DefaultAction based on                ' the state of the control. If CType(Owner, MyCheckBox).Checked Then Return &quot;Toggle button up&quot; Else Return &quot;Toggle button down&quot; End If           End Get End Property

Public Overrides Property Name As String Get ' Return the Text property of the control ' if the AccessibleName is null. Dim accessibleName As String = Owner.AccessibleName If Not (accessibleName Is Nothing) Then Return accessibleName End If               Return CType(Owner, MyCheckBox).Text End Get

Set(ByVal Value As String) MyBase.Name = value End Set End Property

Public Overrides ReadOnly Property Role As AccessibleRole Get ' Because the check box looks similar to a button and functions like a button, ' make the Role the same as a button. Return AccessibleRole.PushButton End Get End Property End Class End Namespace </li> On the Build menu, click Build Solution to build the control.</li></ol>

back to the top

Test the sample control
<ol> Create a new Visual Basic .NET or Visual Basic 2005 Windows Application project in Visual Studio .NET or in Visual Basic 2005.</li> On the View menu, click Toolbox.</li> Right-click the toolbox.</li> If you are using Microsoft Visual Studio .NET 2002, click Customize Toolbox.

If you are using Microsoft Visual Studio .NET 2003, click Add/Remove Items.

If you are using Microsoft Visual Studio 2005, click Choose Items.</li> In the Customize Toolbox dialog box, click the .NET Framework Components tab, and then click the Browse button to locate the control assembly that you created in the &quot;Create a sample control&quot; section of this article.

Note The control assembly is MyCheckBox.dll in the code sample.</li> Click OK to close the Customize Toolbox dialog box.</li> Add the MyCheckBox control to the form.</li>  Add a Button control to the form.

By default, button1 is added to the form.

Add the following code in the Click event handler of the button: MsgBox(&quot;Role: &quot; + MyCheckBox1.AccessibilityObject.Role.ToString + vbCrLf _      + &quot;Name: &quot; + MyCheckBox1.AccessibilityObject.Name.ToString + vbCrLf _       + &quot;DefaultAction:&quot; + MyCheckBox1.AccessibilityObject.DefaultAction.ToString) </li> Press the F5 key to build the application and to run the application.</li> When the application starts, press button1 on the form.

You receive the following message:

Role: PushButton

Name: MyCheckBox1

DefaultAction: Toggle button dow

</li></ol>

back to the top

<div class="references_section">