Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when opening designer on inherited form

I am having trouble opening an inherited form in a Windows CE application. This is a project that I am taking over from a ex-employee, but there is compiled versions running on some mobile devices so I would assume that it should be able to open. I have the correct VS version (2008) and have tried cleaning the solution and rebuilding the solution. When deploying the solution it works like a charm. As soon as I try to go into the designer of the inherited forms, I get the following error:

To prevent possible data loss before loading the designer, the following errors must be resolved:   

Object reference not set to an instance of an object. 

Stack Trace:

at MyApp.frmBase.UpdateOnline() in C:\Users\Corne\Documents\Visual Studio 2008\Projects\Test\MyApp\MyApp\frmBase.cs:line 35
at MyApp.frmBase.frmBase_Load(Object sender, EventArgs e) in C:\Users\Corne\Documents\Visual Studio 2008\Projects\Test\MyApp\MyApp\frmBase.cs:line 30
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Design.DesignerFrame.Initialize(Control view)
at System.Windows.Forms.Design.DocumentDesigner.Initialize(IComponent component)
at System.Windows.Forms.Design.FormDocumentDesigner.Initialize(IComponent component)
at System.ComponentModel.Design.DesignerHost.AddToContainerPostProcess(IComponent component, String name, IContainer containerToAddTo)
at System.ComponentModel.Design.DesignerHost.Add(IComponent component, String name)
at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.CreateComponent(Type componentType, String name)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(Type type, ICollection arguments, String name, Boolean addToContainer)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.System.ComponentModel.Design.Serialization.IDesignerSerializationManager.CreateInstance(Type type, ICollection arguments, String name, Boolean addToContainer)
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)  
like image 693
Corne Avatar asked Jun 27 '13 09:06

Corne


1 Answers

You can clearly see from the stack trace that your base form's Load event handler is running and throwing an exception. This is normal, events in the base form like Load and Paint run at design time as well. It provides the WYSIWYG designer view. This however goes poorly if that code can only work properly at runtime.

The Control.DesignMode property was meant to provide you with a way to check if the code in the the class is operating at design time. Unfortunately it is not available in CF so a different approach is needed. The magic incantation looks like this:

    private void frmBase_Load(object sender, EventArgs e) {
        if (this.Site == null || !this.Site.DesignMode) {
            // Not in design mode, okay to do dangerous stuff...
            this.UpdateOnline();
        }
    }
like image 90
Hans Passant Avatar answered Oct 15 '22 07:10

Hans Passant