Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Where can Custom Designer catches the Exception of Control
 

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 acustom 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

sangminny  Tuesday, September 12, 2006 9:34 AM
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  Tuesday, September 12, 2006 11:51 AM

Hi.

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

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

Is there any idea?

thank you.

sangminny  Friday, September 15, 2006 8:18 AM
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  Monday, September 18, 2006 1:45 AM

You can use google to search for other answers

Custom Search

More Threads

• Deserialization
• Question about creating a control from scratch
• combo box in relational two tables
• Child controls are not removed from Controls Collection from within IComponentChangeService event handlers
• Panel control
• The UserControl preview does not gets updated on deleting items in CollectionEditor.
• Adding dynamic panels to a panel in C#
• Windows Forms Designer
• need help with designer baddies(VS2005)(solved)
• How to apply an html format text to a label