Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Designer Loader failure when including IPersistComponentSettings interface within Component Class
 

Designer Loader failure when including IPersistComponentSettings interface within Component Class

I have build a Component based upon the DataGridView. This Component extends the DataGridView in several ways. I am using IPersistComponentSettings in order to save

I am using IPersistComponentSettings to persist Column Widths. So from the user's perspective all column width adjustments are persisted - much like the experience obtained when using MS Excel. This works well.

The problem I am experiencing is that all forms that use this Component fails to load. One of the methods required by the IPersistComponentSettings is the LoadComponentSettings(). In this method, I extract the persisted values for column widths and set the widths:

public virtual void LoadComponentSettings()
{
ColumnSettings[] columnSettings = Settings.Columns;
foreach (ColumnSettings settings in columnSettings)
{
DataGridViewColumn column = Columns[settings.Name];
column.Width = settings.Width;
}
}

The Problem:
In Visual Studio when I bring up the designer for forms that use this Component, an error page is displayed that says "Object reference not set to an instance of an object" and from the Call Stack, I am able to deduce that the problem is thatI use the following for the SettingsKey:
this.FindForm().Name + this.Name

The designer is crashing on this.FindForm().Name

Jackobee  Saturday, February 21, 2009 8:15 PM
The fix to the problem is to add a conditional within LoadComponentSettings() to block the designer from executing code that causes it to crash:


public virtual void LoadComponentSettings()
{
if (Form.ActiveForm != null)
{

ColumnSettings[] columnSettings = Settings.Columns;
foreach (ColumnSettings settings in columnSettings)
{
DataGridViewColumn column = Columns[settings.Name];
column.Width = settings.Width;
}
}
}

This is worth sharing because there may be code that you do not want the Visual Studios Designer to execute.
  • Marked As Answer byJackobee Saturday, February 21, 2009 8:20 PM
  • Unmarked As Answer byJackobee Saturday, February 21, 2009 8:23 PM
  • Marked As Answer byJackobee Saturday, February 21, 2009 8:24 PM
  •  
Jackobee  Saturday, February 21, 2009 8:20 PM
The fix to the problem is to add a conditional within LoadComponentSettings() to block the designer from executing code that causes it to crash:


public virtual void LoadComponentSettings()
{
if (Form.ActiveForm != null)
{

ColumnSettings[] columnSettings = Settings.Columns;
foreach (ColumnSettings settings in columnSettings)
{
DataGridViewColumn column = Columns[settings.Name];
column.Width = settings.Width;
}
}
}

This is worth sharing because there may be code that you do not want the Visual Studios Designer to execute.
  • Marked As Answer byJackobee Saturday, February 21, 2009 8:20 PM
  • Unmarked As Answer byJackobee Saturday, February 21, 2009 8:23 PM
  • Marked As Answer byJackobee Saturday, February 21, 2009 8:24 PM
  •  
Jackobee  Saturday, February 21, 2009 8:20 PM

You can use google to search for other answers

Custom Search

More Threads

• The path is not of a legal form.
• Extend default context menu
• AutoSize for DatagridView
• How to have drawings show up in Design view
• error "Object type cannot be converted to target type." in task list
• How to delete collection entries when control deleted?
• IToolboxService and the DesignSurface class
• Custom Report field in Windows Application
• How to Configure ListView Control's OwnerDraw property
• Resizing of Rows and Columns