Custom Data Controls Sample
This sample demonstrates how to use your own controls with the Data Sources window.
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
To open the sample file in Solution Explorer
Click Download Sample.
The File Download message box appears.
Click Open, and on the left column of the zip folder window, click Extract all files.
The Extraction Wizard opens.
Click Next. You can change the directory that the files will be extracted to, and then click Next again.
Make sure that the Show extracted files check box is selected, and click Finish.
Double-click the sample's .sln file.
The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.
To run this sample
This sample requires the Northwind sample database. For more information, see.
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 fromcontrol and sets the 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. 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.