Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am working in SharpDevelop.

My question is how to inherit a form from another form? I inherited a form from a base class but designer shows following error:

Failed to load designer. Check the source code for syntax errors and check if all references are available.

ICSharpCode.FormsDesigner.FormsDesignerLoadException: System.NullReferenceException: Object reference not set to an instance of an object.


Project builds with no error and it runs fine. But inherited form is not displayed in designer so that I can edit or change few things of the derived form.

Should I use Microsoft's Visual Studio?
Posted

1 solution

I suggest a special method for handling such unpleasant situations. This is not always related to form inheritance which itself presents no special issues. There are many ways in which one can screw up behavior in design mode.

You should minimize the code executing with the designer. At first, you should leave the code as it is, but make sure that almost nothing runs in design mode. All you need to do on this step is to make the project compile and run, without removing your existing code, but the way it could be fixed later. How to do it?

Pay attention for the property System.Windows.Forms.Control.DesignMode:
http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx[^].

Sandwich all the code you created manually for you forms, inside each method, in the check:
C#
if (!this.DesignMode) {
    // your existing code
}


Are you getting the idea? It doesn't matter that some code won't be edited in design mode as you wanted. You will be able to compile and run it. On the next step, you should see if it creates some problem during run time. But they are much easier to resolve. This should be your next step. This exception is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

See also: http://www.codeproject.com/Answers/505406/wantplustoplusdisplayplusnextplusrecordplusonplusb#answer1

Locate the problem and fix the application behavior. Now, it's time to investigate what have you done wrong so the design mode did not work. Step by step, move you code out of "if" blocks shown above and analyze the results. First of all, the fix of your bugs on the previous step may already make the problem resolved. What if it is not yet resolved? If the project isn't loaded again, retrace you last step and move the offended piece back under the "if" block.

Now, when the problem is finally solved, you need to learn some lesson from the adventure.

  • This advice is not so easy to explain: don't overuse the designer. Try to use the designer only to make general layout of your forms and user controls. Don't try to include any sophisticated processing. Move it all to the runtime code which is not executed in design mode. Generally, even putting many controls in the layout using the designed means way much too manual repetitive work. Avoid it.

    I usually add only main menu, status bar and hierarchy of panels using docking and Padding, maybe something else, just a bit; I never add event handler. Everything else is much better done by writing code. Designer is only needed when you define something visual.
  • Use Revision Control system and practice frequent commits, so you could easily retrieve your steps. Actually, if you are not using some Revision Control system even for small individual project, you are not really programming; all your work can be easily lost or messed up. If so, stop doing what you are doing and install such system. It can be open-source and very light weight.

    Please see this discussion: http://www.codeproject.com/Questions/147818/Revision-control-systems-which-to-choose-from.aspx[^].


Good luck,
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900