23

I've created a class that extends DbConnection in a brand new project.

public class FakeDbConnection : DbConnection { ... }

In the Solution Explorer the class looks like this: enter image description here

And when double-clicking it wants to open it in design mode which won't work. Opening up the .csproj-file reveals the problem

<ItemGroup>
  <Compile Include="FakeADO\FakeDbConnection.cs">
    <SubType>Component</SubType>
  </Compile>
</ItemGroup>

Even if I remove the SubType tag VS2010 immediately re-adds it. Very annoying.

How can I stop VS2010 from opening up my .cs file in designer mode and just open it up as a regular code file?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
vidstige
  • 12,492
  • 9
  • 66
  • 110

3 Answers3

42

As described in an answer to this question you can do this:

[System.ComponentModel.DesignerCategory("Code")]
class FakeDbConnection: DbConnection { ... }

Important: The attribute needs to be fully qualified otherwise VS2010 will ignore this.

Important (thanks to jmbpiano): The attribute only applies to the first class in the file.

Community
  • 1
  • 1
vidstige
  • 12,492
  • 9
  • 66
  • 110
  • Interesting...it does not work for me :-( I am using VS2010 and tried with a class that just inherits Component, and also tried with a class that inherits TabControl...no such luck...maybe it does not work on Express editions, although I find this highly unlikely! – Matthew Layton Aug 10 '12 at 08:15
  • Hmm, weird. Did you manually remove the child-tag of the tag in the .csproj-file? – vidstige Aug 10 '12 at 08:40
  • 3
    Found the problem, you need to *fully* qualify the attribute, or it will not work. – vidstige Aug 10 '12 at 08:44
  • 3
    +1 for finding the right answer instead of saying, "just live with it" – aponzani Mar 01 '13 at 15:01
  • Anyone know if this has changed recently? I'm using VS Pro 2013 and targeting .NET 3.5. I made a class derived from WebClient (which, down the chain, derives from Component) and I have this same thing happening, but slapping this attribute on there (yes, fully qualified) does nothing. – bubbleking Sep 10 '14 at 21:11
  • I can confirm this works on VS Express 2013 for Desktop targeting both 3.5 and 4.5. I ran tests on classes derived from both Component and WebClient and everything worked as desired. I doubt Pro would work any differently. – jmbpiano Aug 26 '15 at 19:18
  • 2
    There is one additional requirement to making this work that's worth mentioning: The DesignerCategory attribute need to be applied to the _first_ class in the file. If you've got multiple classes in the same code file, the IDE will ignore any DesignerCategory attributes applied to later classes. – jmbpiano Aug 26 '15 at 19:25
1

The inheritance hierarchy indicates that this class (DbConnection) inherits from System.ComponentModel.Component. Try right click the file and View Source instead.

As always you can check MSDN! Here is the documentation for DbConnection.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • Thanks, I already knew this, but it's very cumbersome. Opening a file shouldn't be that complicated. – vidstige Aug 09 '12 at 10:57
  • 1
    @vidstige It's not exactly complicated, just not as easy as double clicking. Have a look in the Visual Studio settings and options. You might be able to find an option like "Open Component derivatives in source view by default". I know what you mean though, its annoying that components open in the designer when not all components are necessarily designable. – Matthew Layton Aug 09 '12 at 11:54
  • He who searches will find - See my answer :) – vidstige Aug 10 '12 at 07:56
0

Thats because DBConnection inherits "Component". About disabling VS to add "Subtype" in csproj-file - I don't think thats possible.

You can still aceess the code, by right-clicking in designer -> show code (I think "F7" is the shortcut key for that)

  • Welcome to stackoverflow! Thanks for the answer. I already knew that, but didn't put it in the question. Seems cumbers some. But if it's the only way I guess its a sort of work-around. – vidstige Aug 09 '12 at 11:02