Problems with DesignMode in WinForms

Time for a quick post about a problem that I encountered today at work which should be worth sharing.

I have just started creating a level editor with Windows Forms that uses XNA for most of the rendering. As a starting point I decided to use the Windows Forms Series 1: Graphics Device sample by Microsoft which I had heard was the most recommended way of doing it.

So, I downloaded the sample, inherited my scene rendering control from the GraphicsDeviceControl class, and wrote my own Initialize and Draw methods. Everything seemed to work okay! However, once I tried adding my control as a child of another control (in this case, a simple panel) the Visual Studio’s designer could no longer display my form. Here’s what I got:

The error message hinted that even though I was in designer mode, Visual Studio was still calling my Initialize method along with all of the content loading that was happening there.

Okay… So I turned to GraphicDeviceControl’s source code in order to see where Initialize was being called. Here’s what I found:

Strange. DesignMode is a property that returns true (or should return true) when the control is being seen from Visual Studio’s designer instead of at run-time. Therefore, the condition on line 7 should be enough to ensure that the graphics device is not created and that Initialize is never called while in design mode. Then why?

After googling around for a bit, I ran into this thread and it turns that the DesignMode property is seriously broken to begin with. More specifically, it doesn’t work inside the control’s constructor (which is not the case here) AND it doesn’t work at all if the control is a child of some other control! And this seems to be a known bug of the framework which apparently won’t be fixed due to some compatibility problems. *sigh*

Luckily there are some workarounds for it, and since the check was being done outside of the constructor, I went with the recommendation I found on that thread:

1) I added the following method to the GraphicsDeviceControl class.

2) And I replaced the call to DesignMode on GraphicsDeviceControl.OnCreateControl with this new property instead.

Now it works correctly!

Although I’m still having some other minor problems, for instance, sometimes after I run my application once, I can no longer select my GraphicsDeviceControl on the form without closing it and re-opening it first. That and I’ve had my control disappear by itself from the form two or three times already… But I’ll get there eventually.

3 Comments.
  1. Jimjam says:

    Hurrah!
    I’m not going mad, I have been unsuccessfully trying to build a level editor using the XNA – Win forms series 1 example, but have been floundering because my user control keeps disappearing or can’t be selected while in design mode. I have mine inside a tab control. I suppose my alternative is to have seperate forms rather than seperate tabs. Thanks for posting this, it has helped me a lot.
    J

    • Hello J, thanks for commenting. I’d like to add something then, since the problem you’re having doesn’t really have anything to do with using tabs or not.

      So, using the IsDesignerHosted property I described in this article solves the specific problem where the designer breaks when Initialize tries to load Content without realizing it’s actually in design mode

      Unfortunately, it does nothing to prevent the annoying bugs you mentioned. From time to time the GraphicsDeviceControl just decides to disappear, or becomes unselectable. Those are bugs on the VS’s WinForms designer and I haven’t found a way to fix them.

      Eventually I got fed up with those bugs, and decided that it would be much easier to just bypass the designer completely when creating my GraphicsDeviceControl. So instead of adding your control using the designer, just add it through code. For instance, something like:

      XnaControl roomEditor = new XnaControl();
      roomEditor.Docking = DockStyle.Fill;
      tabControl.TabPages[0].Controls.Add(roomEditor);

      This way you don’t need to worry about your control disappearing ever again. I’m also creating a game editor at the moment, and after these initial problems I’ve managed to get XNA and WinForms playing along nicely!

  2. Anonymous says:

    Thanks for the help man. Microsoft is a bunch of idiots for not fixing this problem. I’ve been all over the net trying to figure out wtf was going wrong. Luckily I found your post and now I’m (hopefully) good to go.

Leave a Reply