简体   繁体   English

自定义控件导致Visual Studio 2008到CRASH

[英]Custom control cause Visual Studio 2008 to CRASH

The following code is a custom control. 以下代码是自定义控件。 Using this control in the Visual Studio designer cause Visual Studio to CRASH without any noticeable details. 在Visual Studio设计器中使用此控件会导致Visual Studio崩溃,而没有任何明显的细节。

I'm using Visual Studio 2008. 我正在使用Visual Studio 2008。

Am I doing something wrong here? 我在这里做错了吗?

using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace InstalacionesSayma.GUI
{
    public class CustomControlTest : Panel
    {
        private Label _label;

        public CustomControlTest()
        {
            _label = new Label();
            this.Controls.Add(_label);
        }

        public override Font Font
        {
            get
            {
                return _label.Font;
            }
            set
            {
                _label.Font = value;
            }
        }
    }
}

The crash happens because of what you have in your Font behavior. 崩溃的发生是因为您在Font行为中拥有的内容。 You're overriding the expected behavior of a panel's Font property. 您将覆盖面板的Font属性的预期行为。 Changing your Font code to that below makes the crash go away: 将字体代码更改为以下内容会导致崩溃消失:

 public override Font Font
  {
     get
     {
        return base.Font;
     }
     set
     {
        base.Font = value;
        _label.Font = value;
     }
  }

VS2005 had a knack for crashing to the desktop when an exception was raised during design time. 在设计时提出异常时,VS2005具有崩溃到桌面的诀窍。 That cannot be caused by the code you posted. 这不是由您发布的代码引起的。 I doubt we're looking at the real code, this control doesn't do anything. 我怀疑我们正在查看真正的代码,这个控件什么都不做。

Be careful with the constructor and event handlers, they'll run a design time as well. 小心构造函数和事件处理程序,它们也会运行设计时间。 If you do anything that critically depends on the state of the program, like trying to open files or talk to a dbase server etcetera then avoid running such code by checking the this.DesignMode property. 如果你做任何关键取决于程序状态的事情,比如试图打开文件或与dbase服务器等交谈,那么通过检查this.DesignMode属性来避免运行这样的代码。

I think the problem is your Font property in light of the nature of how the Designer works with regard to Panels and their contents. 我认为问题是你的Font属性,因为Designer的工作方式与Panels及其内容有关。 By default the Font of the label inherits from the Font of its container (You can test this by adding a panel to a control, and then add a label to that panel. Then change the Font of the Panel, and viola, your label -- unless you've explicity given it a non-default font -- will update with the new container Font). 默认情况下,标签的Font继承自其容器的Font(您可以通过向控件添加面板来测试它,然后向该面板添加标签。然后更改Panel的Font,以及中提琴,您的标签 - - 除非你明确指出它是非默认字体 - 将使用新容器Font更新。

So, as it stands, when the control is added to the form, the Font of the label is updated to match the Font of the container, and and endless loop is started. 因此,就目前而言,当控件添加到表单时,标签的Font会更新以匹配容器的Font,并启动无限循环。

It looks like you can fix this by giving your label an explicit font when it's created. 看起来您可以通过在标签创建时为其标注字体来解决此问题。 Something like this seems to work around the problem: 这样的事情似乎解决了这个问题:

   public CustomControlTest() 
    { 
        _label = new Label(); 
        _label.Font = new Font("Ariel", 8.5f);
        this.Controls.Add(_label); 
    } 

ETA: In response to Hans, I was able to reproduce the crash using the code provided and VS2008, and I was able to avoid the crash by initializing the Font. ETA:为了回应Hans,我能够使用提供的代码和VS2008重现崩溃,并且我能够通过初始化Font来避免崩溃。

ETA2: In my previous ETA, I realized my response may have sounded harsher than I intended. ETA2:在我之前的ETA中,我意识到我的回答听起来可能比我预想的更严厉。 Maybe I should have elaborated to mention that I didn't see the crash until I tried to add the control to a form. 也许我应该详细说明,在我尝试将控件添加到表单之前,我没有看到崩溃。 Hans is right that in and of itself, that code shouldn't cause Visual Studio to crash...it's when the designer steps in to try to initialize and draw the control that it runs into problems. 汉斯是正确的,这个代码本身不应该导致Visual Studio崩溃......当设计师介入尝试初始化并绘制它遇到问题的控件时。 On that note, how does one add comments directly to another person's post? 在那个问题上,如何将评论直接添加到另一个人的帖子中? Is a certain minimum reputation needed? 是否需要一定的最低声誉?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM