Where can Custom Designer catches the Exception of Control

Hi,

I'm developing custom designer like below

http://msdn.microsoft.com/msdnmag/issues/06/03/DesignerHosting/default.aspx

and a custom control.

this custom control has only specific controls as child. the related code is below

[Designer(typeof(CanParentExDesigner))]
public class CanParentEx : Panel
{
protected override void OnControlAdded(ControlEventArgs e)
{
if (!e.Control.GetType().Equals(typeof(Button)))
{
throw new InvalidOperationException(e.Control.Name);
}
else
{
base.OnControlAdded(e);
}
}
}

public class CanParentExDesigner : System.Windows.Forms.Design.ParentControlDesigner
{
public override bool CanParent(Control control)
{
return control.GetType().Equals(typeof(Button));
}

}

If user add another control on the custom control, it will throw Exception.

I want to catch the exception on custom designer. but, I don't know where I have to catch it.

If anybody knows, please let me know.

thanks

[1298 byte] By [sangminny] at [2007-12-24]
# 1
The CanParent override to prevent non-Button type controls from being added to your custom Panel is sufficient for design time functionality so you don't need to handle the exception thrown from your OnControlAdded override in the designer since it won't allow you (see the not-allowed mouse cursor when you attempt to do that).

User can still add non-Button types in your custom Panel through code so you added the OnControlAdded override, do you really want to throw an Exception there or just ignore the Control being added. the user code which adds the said non-button type instance should be the one to catch the said Exception and handle it.

joeycalisay at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 2

Hi.

In the eventhandler of OnComponentAdd event (at ComponentSerice), I added some code about control to be added newly.

in this case, OnComponentAdd event is already fired. so, I have to roll back the job after catching the exception.

Is there any idea?

thank you.

sangminny at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 3
sangminny wrote:

Hi.

In the eventhandler of OnComponentAdd event (at ComponentSerice), I added some code about control to be added newly.

in this case, OnComponentAdd event is already fired. so, I have to roll back the job after catching the exception.

Is there any idea?

thank you.


try to enclose your routine in a DesignerTransaction, any exception raised, rollback it.
joeycalisay at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...