Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Custom properties in application hosted designer
 

Custom properties in application hosted designer

Hi,

We are writing an application that will host a form's designer to the user in a windows application. We looked at some sample designer hosting application and we were able to get this running.

Now, for the standard windows controls (like Button, Textbox etc) we need to provide custom properties in the property window. We need to hide most of the properties available for this standard controls and include our own custom properties. Is there any sample that is available for customizing the properties of standard controls?

We tried creating a UserControl with the Designer Attribute referencing a type of another class derived from ControlDesigner. When we try to reference this class instance for propertygrid.SelectedObject, the application is throwing "Cannot cast Button_Properties to IDesigner". Any help will be appreciated.


public class ButtonControlDesigner : System.Windows.Forms.Design.ControlDesigner {

protected override void PreFilterProperties(System.Collections.IDictionary properties)

{

base.PreFilterProperties(properties);

foreach (string prop in removeProperties ) {

System.Diagnostics.Debug.WriteLine("Hidding Property {0}", prop);

properties.Remove(prop);

}

}

private static readonly string[] removeProperties =

{

"CausesValidation", "ImeMode", "DockPadding", "ContextMenu","BackgroundImage","Tag"

};

}

[DesignerAttribute(typeof(ButtonControlDesigner ))]

public class Button_Properties : UserControl

{

private string name;

private bool saveonclose= false;

[CategoryAttribute("Control Settings"),

BrowsableAttribute(true),

ReadOnlyAttribute(false),

DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

public string Name

{

get { return name; }

set { name= value; }

}

[CategoryAttribute("Control Settings"),

BrowsableAttribute(true),

DefaultValueAttribute(false),

DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

public bool SaveOnClose

{

get { return saveonclose; }

set { saveonclose= value; }

}

}


Thanks

Anandan

Anandan  Wednesday, April 26, 2006 5:05 AM

Hi,

You can do this by overriding PreFilterProperties or PostFilterProperties on the control designer. You can then remove the properties you need, or all of them, and then add the ones you need. You should be aware that by doing so some of the designer features might stop working as they rely on various properties being available on the control.

If your PreFilterProperties/PostFilterProperties method is not being called, you need to install aTypeDescriptorFilterService, which for FilterProperties, gets the IDesigner for the component, and if that designer is IDesignerFilter calls designer.PreFilterProperties(properties) and designer.PostFilterProperties(properties). Should also do the same for the other methods (FilterAttributes, FilterEvents,). I.e. something like this:

bool ITypeDescriptorFilterService.FilterProperties(IComponent component, IDictionary properties) {

if (component != null && properties != null) {

IDesigner designer = <get the designer>

if (designer is IDesignerFilter) {

((IDesignerFilter)designer).PreFilterProperties(properties);

((IDesignerFilter)designer).PostFilterProperties(properties);

}

}

return designer != null;

}

Martin

Martin Thorsen - MSFT  Wednesday, April 26, 2006 4:42 PM

Hi,

You can do this by overriding PreFilterProperties or PostFilterProperties on the control designer. You can then remove the properties you need, or all of them, and then add the ones you need. You should be aware that by doing so some of the designer features might stop working as they rely on various properties being available on the control.

If your PreFilterProperties/PostFilterProperties method is not being called, you need to install aTypeDescriptorFilterService, which for FilterProperties, gets the IDesigner for the component, and if that designer is IDesignerFilter calls designer.PreFilterProperties(properties) and designer.PostFilterProperties(properties). Should also do the same for the other methods (FilterAttributes, FilterEvents,). I.e. something like this:

bool ITypeDescriptorFilterService.FilterProperties(IComponent component, IDictionary properties) {

if (component != null && properties != null) {

IDesigner designer = <get the designer>

if (designer is IDesignerFilter) {

((IDesignerFilter)designer).PreFilterProperties(properties);

((IDesignerFilter)designer).PostFilterProperties(properties);

}

}

return designer != null;

}

Martin

Martin Thorsen - MSFT  Wednesday, April 26, 2006 4:42 PM

Thanks Martin.

We are able to trim out unwantedproperties using the method you mentioned above. But what we really need is to add some custom properties. We tried creating a seperate class derived from UserControl (pl. see my previous post) with our own properties.

When we try to use this against PropertyGrid.SelectedObject class (propertygrid.selectedobject = new myclass()), we are getting a cast error. The error message in the exception is "Unable to cast myclass to IDesigner".

We are not sure what we are missing. Any code sample for overriding standard windows controls (like Button, Textbox) with new properties will help us greatly.

Thanks

Anandan  Thursday, May 04, 2006 5:46 AM

maybe this helps: there is a IExtenderProvider, which could helpadding properties to every control.

[ProvideProperty("YourPropertyName", typeof(IComponent))]
public class MyPropertyExtender : System.ComponentModel.Component, IExtenderProvider
{
public bool CanExtend(object extendee)
{
return (extendee is IComponent);
}

type GetYourPropertyName(IComponent component) { return values[compent];}

public void SetYourPropertyName(IComponent component, type value) { values[compent]=value;}

}

Online help for IExtenderProvider is available.

DirkR  Friday, May 05, 2006 3:56 PM

Hi,

You could also you PreFilterProperties to do something similar bycallingTypeDescriptor.CreateProperty.

properties["<your property>"] = TypeDescriptor.CreateProperty

Martin

Martin Thorsen - MSFT  Friday, May 05, 2006 6:50 PM

You can use google to search for other answers

Custom Search

More Threads

• Copy paste problems
• Usercontrols on tabcontrol
• Switching localizable forms language messes up design
• Disable tabPage?
• Is it possible to give more than one control focus?
• Designer changes DataGridView styles
• resize row tablelayoutpanel
• Problem with Visual Inheritance when i change the size of the Father form
• Error: "DataGridView control's columns has no cell template" in UserControl
• VS Bogging Down - How Do I Stop It