Custom Data Controls Sample
This sample demonstrates how to use your own controls with the Data Sources window.
To get samples and instructions for installing them
Do one or more of the following:
On the Help menu, click Samples.
The Readme displays information about samples.
Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.
Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.
For more information, see Visual Studio Samples.
This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.
To run this sample
- Press F5.
This sample requires the Northwind sample database. For more information, see How to: Install and Troubleshoot Database Components for Samples.
The sample solution includes two projects. One is a Windows Control Library project named MyCompanyControls. The second is a Windows Application project named CustomDataControls.
The control library defines the PhoneBox control. The control simply inherits from MaskedTextBox control and sets the Mask property to a Phone Number with Area Code. The library project also contains an embedded resource, PhoneBox.bmp, to represent the icon in the ToolBox and Data Sources window.
The PhoneBox control is available in the CustomDataControls project. By selecting Rebuild from the project's shortcut menu, the PhoneBox will appear in the Toolbox in a new section named MyCompanyControls. You need to have Form1 opened in the designer in order to see the ToolBox items.
The Data Sources window is used to add the PhoneBox to the list of controls for String. In the Data Sources window, select the HomePhone column under the Employee table. Select the drop-down and choose Customize. This opens the dialog box for customizing the list of controls for given types. This list is populated with the list of controls currently in the Visual Studio ToolBox that implement the DefaultBindingPropertyAttribute. The PhoneBox control inherits from MaskedTextBox, which supports the DefaultBindingPropertyAttribute. Select String under Data Type and find the PhoneBox control in the list. The check by the PhoneBox control indicates to Visual Studio that the PhoneBox control is one of the available controls for any object that has a string property.