4

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 (custom) 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 class (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!?

antonio
  • 548
  • 8
  • 16
  • @grant winey Thanks for the tips, I can change this option in VS, for me, but for others ... I don't know, There is another [link](http://stackoverflow.com/questions/1651601/how-do-i-make-visual-studio-always-show-the-source-view-instead-of-the-design-vi) can be useful. I looking for a class level configuration. – antonio Oct 28 '16 at 01:28
  • I think that it is the answer. [Disable designer in Visual Studio](http://stackoverflow.com/questions/567606/disable-designer-in-visual-studio) But, this disable whole the class, not only the partial custom class in separate source code, as I had asked. Here is the MSDN link [DesignerCategoryAttribute Class](https://msdn.microsoft.com/en-us/library/system.componentmodel.designercategoryattribute(v=vs.110).aspx) – antonio Oct 28 '16 at 03:02

3 Answers3

7

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
    {
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    You're welcome :). When your `Form` is not the first class in a file, when you try to open the file in designer, you receive this error: *The class Form1 can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.* So I used this fact that the designer tries to load the first class and decorated it with that attribute to show it as code. – Reza Aghaei Nov 01 '16 at 07:22
  • This works great. I named my class `DesignerBlocker` instead of `Dummy`, and kept it `private`. I maintain a winforms app where the original developers stuffed all their code into the Form class. For example: full Excel reporting code inside a button click event handler. To make the code more maintainable, I split the Form classes into multiple partial classes, but have been cursed with the having to launch the designer every time I open one of these files from the Solution Explorer. Now I can fix that annoyance. – Walter Stabosz Jan 20 '22 at 16:06
  • @WalterStabosz Nice! Thanks for the feedback. If most of your form use a similar file structure, as an idea you can create a new file template which generate the file hierarchy/structure that you want; a structure like what I suggested in [my other answer](https://stackoverflow.com/a/66054796/3110834). – Reza Aghaei Jan 20 '22 at 19:46
1

Another approach is nesting the file and adding .designer.cs extension to it:

enter image description here

To do so:

  1. Unload the project

  2. Edit the project file

  3. Find the filename that you want to make it child

  4. Add <DependentUpon>name of parent</DependentUpon> file to the child tag

    You should now have something like this:

    <Compile Include="Form1.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="Form1.Designer.cs">
      <DependentUpon>Form1.cs</DependentUpon>
    </Compile>
    <Compile Include="Form1.Methods.Designer.cs" >
      <DependentUpon>Form1.cs</DependentUpon>
    </Compile>
    
  5. Save and Close the project file.

  6. Reload the project.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • This solution prevents visual studio (tested on vs2019 community) from adding `Form` to the custom partial classes on first opening, which seems to be the source of the problem without using the workaround. I prefer that solution which seems less 'hacky' to me. This should also be the accepted solution. – guppy81 Feb 04 '22 at 10:05
1

C# and VB.Net code provided, because this is still an issue.

Base on the accepted answer, https://stackoverflow.com/a/40302216/3917091

Implement your class in its own file (or anywhere)

C#:

/// <summary>
/// Partial classes of Forms want to open in Form-View. Adding this stops it.
/// </summary>
[System.ComponentModel.DesignerCategory("")]
public class FormViewBlocker {
}

VB.net:

''' <summary>
''' Partial classes of Forms want to open in Form-View. Adding this stops it.
''' </summary>
<System.ComponentModel.DesignerCategory("")>
Public Class FormViewBlocker

End Class

And then, at the top of partial-forms,

C#:

partial class FormViewBlocker {
}

VB.net:

Partial Class FormViewBlocker

End Class

That's literally it

Regular Jo
  • 5,190
  • 3
  • 25
  • 47