Window's Control Property Extender

Ambily.raj
Posted by in Windows Forms category on for Beginner level | Points: 250 | Views : 7341 red flag

In Windows application development, we have few built-in extender components like ErrorProvider, Tooltip, etc. Extender component will add few more properties to built-in controls to enhance the functionality. In this article we will discuss about creating a custom extender component for Windows controls.

Introduction

In Windows application development, we have few built-in extender components like ErrorProvider, Tooltip, etc. Extender component will add few more properties to built-in controls to enhance the functionality. In this article we will discuss about creating a custom extender component for Windows controls.

How to Add a Custom Extender

Create a Class Library project. Add a component using the Add New Item window. For the sample, I am adding a component called MyComponent.



 
Once, the component got added, implement the IExtenderProvider interface. Add the ProvideProperty attribute to the class indicating the extended property name and specify the type of controls to which this property should be applied.

Add the Properties inner class, which will hold all the extended properties we are going to add to the control. In our sample, we have only one property named MyProperty.

Once we drag and drop the component to a Window’s Form, it will add the new property to all controls. The Hahtable defined in our extender class will hold the properties and associated control mapping. Hashtable will have the control instance and an object of our Properties class.

Now add the getter and setter for the property. In the getter, we are checking whether the hashtable contains our control and associated properties class or not.

 

[ProvideProperty("MyProperty", typeof(Control))]   

    public partial class MyComponent : Component, IExtenderProvider

    {

        private Hashtable properties;

        public MyComponent()

        {

            properties = new Hashtable();

            InitializeComponent();

        }

 

        public MyComponent(IContainer container)

        {

            container.Add(this);

            properties = new Hashtable();

            InitializeComponent();

        }

 

        public bool CanExtend(object extendee)

        {

            if (extendee == this)

            {

                return false;

            }

 

            return (extendee is Control || extendee is UserControl);

        }

        private Properties EnsurePropertiesExists(object key)

        {

            Properties p = (Properties)properties[key];

            if (p == null)

            {

                p = new Properties();

                properties[key] = p;

            }

            return p;

        }

 

        private class Properties

        {

            public string MyProperty;                      

            public Properties()

            {               

                MyProperty = String.Empty;          

            }

        }

 

        [Category("MyProperty")]       

        [Description("Mapper file path.")]

        public string GetMyProperty(Control p)

        {

            return EnsurePropertiesExists(p).MyProperty;

        }

        public void SetMyProperty(Control p, string value)

        {

            EnsurePropertiesExists(p).MyProperty = value;

        }

    }

Add to Windows Form

Open the Windows application. Add the component to the toolbox using Choose Item option. Drag and drop the component to your form. It will appear on Bottom area of the Form. Now, open the properties window of our Form, we can observe the new property “MyProperty” added to the properties of the Form.



Conclusion

In this article we discussed about how to create a simple extender component and how to add another property to a Form or Window’s control. We can add more functionality to the extender code to support extensibility to a control.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Ambily.raj
Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K http://ambilykk.com/
http://ambilykk.com/
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)