Imports System.ComponentModelImports System.Drawing.DesignImports System.Windows.Forms
Let’s Define Our ProblemLet’s say in our project, each and every form should set the SQL server table which is used as the primary table for some security settings. For this we need create a class which inherits from System.Windows.Forms.Form class and we need to add a property to get the table name. Let’s name the property as “Primary Table”. For this we need to provide a UI in the property window, so that the implementers will use the same to select the table from SQL Server.
This is the class which helps us to create a component which can be plugged in as an UI for our property in the properties window of VS.NET. We need to create a class which inherits from this class and we can provide our own implementations by overriding some methods. Let’s start creating our pluggable component and let’s name the component as SQLObjectPicker.1. Create a class library project using vs.net and name the project as IDECustomComponents.2. In the project add a reference to System.Drawing.Design.dll.3. Create a class named SQLObjectPicker.4. Inherit the class from System.Drawing.Design.UITypeEditor class.5. In the class override the follwing functions. a. GetEditStyle b. EditValue
6. Now we will create a form which will populate all the tables from sql server and show it in combo box. For that add system.windows.forms.dll reference7. Create a new winform named “MyPicker”. In the form we need to connect to SQL server and get the tables. But in this article let’s hardcode the values in the form Load Event. 8. In the Edit Value sub-routine of SQLObjectPicker class copy the following lines of code. In this method we will show our custom picker.
1. In the same project add another winform and name the form as CustomFormBase and add the follwing property
This is through attributes. As soon as we place the following attribute to the property, vs.net calls the appropriate component specified in the attribure.In our case SQLObjectPicker is the component.
<BrowsableAttribute(True), EditorAttribute(GetType(SQLObjectPicker,GetType(System.Drawing.Design.UITypeEditor))>_
That’s it Our Custom picker is ready. Create a project and create a form which inherits from CustomFormBase. In the propery window we can see our PrimaryTable property with a button. As soon as we click on the button, our table picker will popup.
1. Don’t let the code to directly inherit from the class provided by FCL, at least have one level of inheritance and start the coding from that level (where ever applicable). This will helps us to get one level of control over all the classes which inherits your class.2. These types of pickers will increase the developers productivity3. These pickers will give professional look to our applications.4. This is best suited for projects which deals with developing a framework which will be used for development by developers.