Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Tip/Trick

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.6K   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.

Introduction

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:

C#
/// <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>
[System.ComponentModel.DesignerCategory("Code")]
public class MyPrinter : PrintDocument
Now VS will put the icon back to normal, and open in the code view.

History

  • 2017-12-23 First version

License

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


Written By
CEO
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 3:14
RenniePet3-Jan-18 3:14 
GeneralMy vote of 5 Pin
Member 373058726-Dec-17 0:40
Member 373058726-Dec-17 0:40 
GeneralRe: My vote of 5 Pin
OriginalGriff26-Dec-17 1:43
mveOriginalGriff26-Dec-17 1:43 
GeneralMy vote of 5 Pin
Heriberto Lugo24-Dec-17 15:55
Heriberto Lugo24-Dec-17 15:55 
GeneralRe: My vote of 5 Pin
OriginalGriff26-Dec-17 1:43
mveOriginalGriff26-Dec-17 1:43 
QuestionIt's amazing ... Pin
Richard MacCutchan23-Dec-17 2:41
mveRichard MacCutchan23-Dec-17 2:41 
AnswerRe: It's amazing ... Pin
OriginalGriff23-Dec-17 6:09
mveOriginalGriff23-Dec-17 6: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.