Showing posts with label ToolStripControlHost designer .net visual studio. Show all posts
Showing posts with label ToolStripControlHost designer .net visual studio. Show all posts

Thursday, September 23, 2010

Visual studio designer hangs and fails to load ToolStripControlHost derived class

I found a problem using a ToolStripControlHost derived class when the class is being built in the same solution as the form that uses the component.

When performing a rebuild the designer would freeze and wouldn't redraw the toolbar. The fix I discovered solves the problem where the designer disposes the object and then attempts to access it after the rebuild.

public class CustomButtonToolStripHost : ToolStripControlHost
{
public CustomButtonToolStripHost ()
: base(CustomToolStripButton())
{
}
/// Used to overcome a designer error when rebuilding the project.
private class CustomToolStripButton : Button // Replace with your control
{
/// Override CreateHandle to avoid exception if called when object is disposed.
/// This occurs when the designer updates after a rebuild.

protected override void CreateHandle()
{
if (!IsDisposed)
{
base.CreateHandle();
}
}
}
}