Click here to Skip to main content
15,792,771 members
Articles / Programming Languages / C#

Preventing Classes Derived from PrintDocument (and Other Components) from Opening in the Designer by Default

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
23 Dec 2017CPOL 10.1K   5   7
Does your derived class open in the Visual Studio Designer while having no UI? Annoying, isn't it? This is simple to fix.


If you derive a class from Component, then Visual Studio assumes it's a UI element. Unfortunately, PrintDocument is derived from Component - and isn't visual at all, it just uses graphics to draw onto output contexts. But your class counts as a visual element as well despite having no UI, so VS will open it in the design view.

Which promptly brings up a blank screen and invites you to add UI elements to it. :sigh:

To Fix It Is Simple

Just add an attribute to your class definition:

/// <summary>
/// Prints Label sheet.
/// </summary>
/// <remarks>
/// Note the addition of the ComponentModel.DesignerCategory attribute.
/// Without this, the class will always open in the designer by default as
/// PrintDocument derives from Component, which VS believes is a visual element.
/// It isn't - you can't display it if try - but that means it opens in the
/// design view: which promptly complains that you need to add items to it.
/// </remarks>
public class MyPrinter : PrintDocument
Now VS will put the icon back to normal, and open in the code view.


  • 2017-12-23 First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

QuestionMuch greater usability than you indicate Pin
RenniePet3-Jan-18 4:14
RenniePet3-Jan-18 4:14 
GeneralMy vote of 5 Pin
Member 373058726-Dec-17 1:40
Member 373058726-Dec-17 1:40 
Thank you, thank you, thank you, thank you very much!  :-D 
Some time ago I looked for the way to do it without success!  :(( 

GeneralRe: My vote of 5 Pin
OriginalGriff26-Dec-17 2:43
mvaOriginalGriff26-Dec-17 2:43 
GeneralMy vote of 5 Pin
Heriberto Lugo24-Dec-17 16:55
Heriberto Lugo24-Dec-17 16:55 
GeneralRe: My vote of 5 Pin
OriginalGriff26-Dec-17 2:43
mvaOriginalGriff26-Dec-17 2:43 
QuestionIt's amazing ... Pin
Richard MacCutchan23-Dec-17 3:41
mveRichard MacCutchan23-Dec-17 3:41 
AnswerRe: It's amazing ... Pin
OriginalGriff23-Dec-17 7:09
mvaOriginalGriff23-Dec-17 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.