Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I'm using 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;
            }
        }
    }
}
like image 556
DxCK Avatar asked Oct 01 '10 14:10

DxCK


3 Answers

The crash happens because of what you have in your Font behavior. You're overriding the expected behavior of a panel's Font property. 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;
     }
  }
like image 68
msergeant Avatar answered Nov 09 '22 11:11

msergeant


VS2005 had a knack for crashing to the desktop when an exception was raised during design time. 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.

like image 2
Hans Passant Avatar answered Nov 09 '22 12:11

Hans Passant


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. 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).

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.

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.

ETA2: In my previous ETA, I realized my response may have sounded harsher than I intended. 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. On that note, how does one add comments directly to another person's post? Is a certain minimum reputation needed?

like image 2
Steven Avatar answered Nov 09 '22 10:11

Steven