tags:

views:

267

answers:

3

C# 3.5 Winforms...

So I’ve recently discovered the IExtenderProvider and how it can be used to extend controls with additional properties.

In a prototype project that I setup i put a break point on the ‘set’ method for my extendee property and as the form loads I could see the ‘set’ method executing for every control on the form; which is exactly what I wanted. Following the successful prototype I implemented the extender component into my main project. All the forms in my project inherit from a base form which I’ve added my extender component to. On the base form I set the modifier of the extender component to public so that its accessible by the form inheriting this base form.

Doing the same thing before i added a break point on the ‘set’ method for my extendee property but the method doesn’t execute for the controls in the form (but only for the controls in the base form). HELP!

I should probably add at this point that i’ve source controlled my forms and so most of them are checked-in (ie lock from modification). For the forms that I’ve checked out and modified the provider property; I’ve noticed in the designer file that all controls have an additional statement which calls the ‘set’ method of the provider property.

this.MyProvider1.SetMyProperty(this.txtTextBox1, false);

Am I right in thinking that for the extender component to work it has to physically modify the designer file or should it be able to cope with locked files and therefore call the set method dynamically? I guess if it does have to modify the designer file then this isn’t a problem for new forms or forms that get modified after the extender component was added to the project – but it would be problem when you have 101 forms all locked by source-safe...

I’d appreciate any thoughts...

A: 

Maybe instead I should really be asking: At what point does the extender provider (IExtenderProvider) extend the 'type' (in my case a winforms control) that the extender was intended for; at design time or at run time?

mouters
+1  A: 

At what point does the extender provider (IExtenderProvider) extend the 'type' (in my case a winforms control) that the extender was intended for; at design time or at run time?

The designer is responsible for showing you the properties of the extender in the property editor
Method bool CanExtend(object) from the IExtenderProvider interface

Am I right in thinking that for the extender component to work it has to physically modify the designer file or should it be able to cope with locked files and therefore call the set method dynamically?

It has to physically modify the designer file, and write the extended properties there

I guess if it does have to modify the designer file then this isn’t a problem for new forms or forms that get modified after the extender component was added to the project – but it would be problem when you have 101 forms all locked by source-safe...

This is is not a problem for new forms, and not for old forms.
If you want to set some extended properties, open the old form and set the extended properties (a check out of the file is necessary)

Peter Gfader
A: 

This really does confirm my suspicions, many thanks. But this does leave a problem in that the components are only extended if some physical change is made to the old form.

I was trying to hijack the Set property method to also add and remove an event handler to the component (if the component was a control). Image the property is a Boolean and when set to false it adds the event handle and therefore the default behaviour (setting to true doesn’t add and event handler)

To cut a long story short the controls which were part of newly added forms automatically have an event handler added even without me explicitly setting the property to false but the designer file of the old forms never modifier so the event handler wasn’t added.

As some background, I was trying to add a global event handler for all controls http://stackoverflow.com/questions/772079/global-event-handler-for-all-controls-for-user-help

The theme here is to add context help to my forms here’s example of the extender ( the event handler is added as part of the end initialiser)

public partial class HelpProvider : Component, IExtenderProvider, ISupportInitialize

... other code of the extender omitted ...

    #region ISupportInitialize Members

    public void BeginInit()
    {
        // do nothing
    }

    public void EndInit()
    {
        if (DesignMode)
            return;

        foreach (Component item in _disableOnlineHelp)
        {
            if (item == null)
                continue;

            if (GetDisableOnlineHelp(item)) // developer has decide to set property to TRUE
                continue;

            Control control = item as Control;

            if (control != null)
                continue;

            control.HelpRequested += new HelpEventHandler(HelpProvider_HelpRequested);
            _toolTip.SetToolTip(control, GetHelpText(control));
        }
    }

    #endregion

    #region DisableOnlineHelp Provider Property

    public virtual bool GetDisableOnlineHelp(Component component)
    {
        object flag = _disableOnlineHelp[component];

        if (flag == null)
            return false;

        return (bool)flag;
    }

    public virtual void SetDisableOnlineHelp(Component component, bool value)
    {
        _disableOnlineHelp[component] = value;
    }

    #endregion
mouters