C#,在Designer中编辑继承的表单 [英] C#, Editing inherited Form in Designer

查看:108
本文介绍了C#,在Designer中编辑继承的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我在 SharpDevelop 工作。



我的问题是如何从另一种形式继承表格?我从基类继承了一个表单,但设计者显示以下错误:



无法加载设计器。检查源代码是否存在语法错误并检查是否所有引用都可用。



ICSharpCode.FormsDesigner.FormsDesignerLoadException:System.NullReferenceException:对象引用未设置为an的实例对象。




项目构建没有错误,运行正常。但继承的表单不会显示在设计器中,以便我可以编辑或更改派生表单的一些内容。



我应该使用Microsoft的Visual Studio吗?

解决方案

我建​​议一种处理这种不愉快情况的特殊方法。这并不总是与表单继承有关,表单继承本身不会产生任何特殊问题。在设计模式中,有许多方法可以搞砸行为。



您应该尽量减少设计人员执行的代码。首先,您应该保留代码,但确保在设计模式下几乎不运行任何代码。您在此步骤中需要做的就是使项目编译并运行,而不删除现有代码,但可以在以后修复它。怎么做?



注意属性 System.Windows.Forms.Control.DesignMode

http://msdn.microsoft.com/en -us / library / system.componentmodel.component.designmode.aspx [ ^ ]。



在每个方法中,为您的表单手动创建所有代码,在支票中:

  if (!this.DesignMode){
// 您现有的代码
}





你明白了吗?根据需要,某些代码不会在设计模式下编辑并不重要。您将能够编译并运行它。在下一步,您应该看看它是否在运行时产生了一些问题。但它们更容易解决。这应该是你的下一步。此异常是最容易检测和修复的情况之一。它只是意味着某些引用类型的某个成员/变量通过使用和它的实例(非静态)成员解除引用,这要求此成员/变量为非null,但实际上它似乎为null。只需在调试器下执行它,它将停止抛出异常的执行。在该行上设置一个断点,重新启动应用程序并再次到达这一点。评估下一行中涉及的所有引用,并查看哪一个为null,而不需要为null。解决这个问题之后,修复代码:确保将成员/变量正确初始化为非空引用,或者将其检查为null,如果为null,则执行其他操作。



参见:http://www.codeproject.com/Answers/505406/wantplustoplusdisplayplusnextplusrecordplusonplusb#answer1



找出问题并修复应用程序行为。现在,是时候调查你做错了什么,所以设计模式不起作用。一步一步地,将代码移出上面显示的if块并分析结果。首先,修复上一步中的错误可能已经解决了问题。如果还没有解决怎么办?如果项目没有再次加载,请回溯最后一步并将冒犯的部分移回if块下面。



现在,当问题最终解决时你需要从冒险中学到一些教训。



  • 这个建议并不容易解释:不要过度使用设计师。尝试仅使用设计器来进行表单和用户控件的总体布局。不要试图包含任何复杂的处理。将其全部移动到未在设计模式下执行的运行时代码。一般来说,即使使用设计的手段在布局中放置许多控件也会过于手动重复工作。躲开它。



    我通常只使用对接和填充添加主菜单,状态栏和面板层次结构,也许是别的, 一点点;我从不添加事件处理程序。编写代码可以更好地完成其他任务。只有在定义可视化内容时才需要Designer。
  • 使用版本控制系统并练习频繁提交,这样您就可以轻松检索步骤。实际上,如果你没有使用某些版本控制系统,即使是小型的个别项目,你也不是真正的编程;你的所有工作都很容易丢失或搞砸。如果是这样,停止做你正在做的事情并安装这样的系统。它可以是开源的,重量很轻。



    请参阅此讨论:http://www.codeproject.com/Questions/147818/Revision-control-systems-which-to-choose-from.aspx [ ^ ]。




祝你好运,

-SA

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?

解决方案

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:

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


这篇关于C#,在Designer中编辑继承的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆