9

I have a class named Print.cs that always stubbornly opens in Design View, when I need to see its Code View (F7). It also has a different icon to the rest of my classes in the Solution Explorer.

I've looked in the Properties and can't see anything relevant. I've also tried deleting and re-creating the class, but the icon comes back.

How can I force Print.cs to always open in Code View?

(Click to enlarge)

NB: disregard the green squiggly line, it's just a warning that unreachable code was detected.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • Can you show the definition of the class? I guess it is inheriting from Component or something like that – Juan Mar 18 '15 at 14:16
  • @Juan Sure, it's `public class Print : PrintDocument {...}` – Danny Beckett Mar 18 '15 at 14:25
  • So yes, it inherits from Component... so not sure if you can avoid that. Maybe instead of extending PrintDocument you can try to do some composition instead? – Juan Mar 18 '15 at 14:48
  • 1
    See [Disable designer in Visual Studio?](http://stackoverflow.com/a/602589/719186). Ignore the accepted answer, read the second paragraph of the highest voted answer. – LarsTech Mar 18 '15 at 14:50

4 Answers4

15

Taken from the suggestions from @LarsTech and @OrkunBekar, since neither posted this as an answer - this works:

[System.ComponentModel.DesignerCategory("Code")]

Added between the namespace and the class, e.g.

using System;
using System.Collections.Generic;
...

namespace POS
{

    [System.ComponentModel.DesignerCategory("Code")]

    public class Print : PrintDocument
    {
        ...
    }
}
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • 1
    As a side note after you open the file once you can remove the decoration from the class and it will still open up in code view. At least I could. – Chad Aug 30 '17 at 16:42
  • @Chad I cannot confirm this behavior. It opened properly at first. But then sometimes later it stopped working and I had to add [System.ComponentModel.DesignerCategory("Code")] again. – Maxter Jul 30 '18 at 14:48
8

Try right click on the file -> Open With -> CSharp Editor (remember to set it as default).

kha
  • 19,123
  • 9
  • 34
  • 67
2

Funny enough, it was totally other thing in my case. If the filename equal (=) to the first class in it, then Visual Studio decides it is a simple C# file. If you have 2 classes and the first is not equal to the file name, then the solution icon changes and default editor is designer. Visual Studio

Ronen
  • 1,225
  • 13
  • 10
1

I don't know if you have same conditions in that link but opening your class in notepad, changing codes, replacing file then building the solution again may fix the problem.

Community
  • 1
  • 1
Orkun Bekar
  • 1,447
  • 1
  • 15
  • 36