antonio antonio - 1 month ago 6
C# Question

Prevent Visual Studio load a custom partial class as a designer

In Visual Studio, there are lot of designer editors, Windows Forms, XAML, Installer, and others.

Sometimes, I create a new source code as partial class to separate the logic.

For example:


  • Partial code only: class Form1 : Windows.Forms.Form => Form1.cs

  • Partial Designer only: partial class Form1 : Form => Form1.Designer.cs

  • Partial Custom : partial class form1 : form => Form.Print.cs



In this last, I include print only methods and properties, and then, when I double click this file on Solution Explorer, always open the designer, instead of code editor, of course, I try use F7 or right-click to do that, but when I share the project with co-workers, it becomes a problem.

Anyone know how to avoid this behavior? Maybe a class attribute!?

Answer

As an option add a dummy class decorated by [DesignerCategory("")]at the beginning of your file. This limits the behavior of DesignerCategory to the first class which the designer tries to load.

Not elegant but working:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    [System.ComponentModel.DesignerCategory("")]
    public class Dummy { }

    public partial class Form1
    {
    }
}
Comments