Click here to Skip to main content
6,629,377 members and growing! (21,365 online)
Email Password   helpLost your password?
Languages » C# » Windows Forms     Intermediate License: The Code Project Open License (CPOL)

Have a Great DesignTime Experience with a Powerful DesignSurface (Extended) Class

By Paolo Foti

Use design facilities (TabOrder, UndoEngine, SnapLines / Grid alignment) to design WinForms
C#, Windows, .NET 2.0VS2008, Dev
Posted:14 Mar 2008
Views:18,233
Bookmarked:53 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 5.63 Rating: 4.49 out of 5

1

2
1 vote, 5.6%
3
2 votes, 11.1%
4
15 votes, 83.3%
5
designsurfaceext_tinyformdesigner_sourcecode

Abstract

The article describes a DesignSurface inherited class which adds some design facilities (TabOrder, UndoEngine, SnapLines / Grid alignment) to the .NET 2.0 class. This class is hosted inside a DLL Assembly and ready to use inside any .NET solution.

Introduction

The .NET 2.0 Framework introduces the DesignSurface class which implements what the user perceives as a designer. This class is an improvement over the .NET 1.0 one, but it is still missing some design facilities which are very useful for the user who wants to design Forms. I'm talking about: TabOrder, UndoEngine and SnapLines/Grid alignment. All these features can be added with little effort (except maybe the TabOrder), nevertheless I want to make life a little easier for anyone who wants to make her/his own "designer".

Background

You should be familiar with basic C# programming and with some concepts used by the .NET designer (especially if you dive into the source code).

How to Use DesignSurfaceExt

In short:

using DesignSurfaceExt;

...


Form frm = new Form();
    IDesignSurfaceExt surface = new DesignSurfaceExt.DesignSurfaceExt();
    surface.CreateRootComponent( typeof( Form ), new Size( 400, 400 ) );
    surface.CreateControl( typeof( Button ), new Size( 100, 40 ), new Point( 10, 10 ) );
    TextBox t1 = (TextBox) surface.CreateControl( typeof( TextBox ), 
        new Size( 300, 20 ), new Point( 10, 80 ) );
    t1.Text = "Hello World by DesignSurfaceExt";
    surface.GetView().Parent = frm;
frm.ShowDialog();

The above code snippet simply creates a new DesignSurfaceExt instance. Use it via its interface to create the root component and any .NET control you desire, finally host the designsurface into a control, for example a Form; that all folks!

Tiny Form Designer, a Simple Demo Project to Show You the Power of the DesignSurfaceExt

Obviously you can do anything you want as I show you in the DemoConsole Project where I created a Tiny Form Designer! With this little designer, you can switch through four design surfaces hosted by four TabPages: one lets you align the controls using SnapLines facility; one using the "old" Grid; one using the Grid without snapping to the grid itself; and the last doesn't offer any alignment facility at all. Move the controls which have been programmatically deployed; cut and copy or cut and paste from one surface to another; undo and redo your actions and change the TabIndex of the controls with TabOrder facility.

The Interface

The really useful features are exposed through interface IDesignSurfaceExt but, if you prefer, you can ignore it and use the DesignSurfaceExt instance itself.

public interface IDesignSurfaceExt {

    //- perform Cut/Copy/Paste/Delete commands
    void DoAction( string command );

    //- de/activate the TabOrder facility
    void SwitchTabOrder();

    //- select the controls alignment mode
    void UseSnapLines();
    void UseGrid ( System.Drawing.Size gridSize );
    void UseGridWithoutSnapping ( System.Drawing.Size gridSize );
    void UseNoGuides();

    //- method useful to create control without the ToolBox facility
    IComponent CreateRootComponent ( Type controlType, Size controlSize );
    Control CreateControl ( Type controlType, Size controlSize, Point controlLocation );

    //- Get the UndoEngineExtended object
    UndoEngineExt GetUndoEngineExt();

    //- Get the IDesignerHost of the .NET 2.0 DesignSurface
    IDesignerHost GetIDesignerHost();

    //- the View of the .NET 2.0 DesignSurface is just a Control
    //- you can manipulate this Control just like any other WinForms Control
    //- (you can dock it and add it to another Control just to display it)
    //- Get the View
    Control GetView();

}//end_interface

The SnapLines/Grid Alignment Guides

The use of the Snaplines to align the controls hosted by the DesignSurface is achieved by setting the right Property:

surface.UseSnapLines();

If you prefer to use the Grid to align the controls hosted by the DesignSurface, you must specify the size of the grid when calling the right Property:

surface.UseGrid ( new System.Drawing.Size ( 16, 16 ) );

You can also align the control with no guides at all:

surface.UseGridWithoutSnapping ( new System.Drawing.Size ( 32, 32 ) );
surface.UseNoGuides(); 

The TabOrder

And what about the Taborder? Simply call the interface method to activate and deactivate the facility:

surface.SwitchTabOrder();

The UndoEngine

Now the UndoEngine! Get it via the Property: GetUndoEngineExt() and use it calling the Undo/Redo actions:

surface.GetUndoEngineExt().Undo();
surface.GetUndoEngineExt().Redo();

Cut/Copy/Paste/Delete the Controls

Finally use the cut/copy/paste and delete as usual:

surface.DoAction( "Cut" );
surface.DoAction( "Copy" );
surface.DoAction( "Paste" );
surface.DoAction( "Del" );

Conclusion

DesignTime is something really different from RunTime but the .NET Framework lets us approach it in a relatively simple manner. Enjoy this, IMHO, useful class and let me know if you find it useful!

History

  • 14th March, 2008 - First release

License

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

About the Author

Paolo Foti


Member
I started coding in 1984 on a ZX Spectrum. From the Sinclair Basic I jump straight to C++ (which I use since 1992). I wrote code in assembly 386, ANSI C++ (I love it) and VisualBasic 4.0/6.0 (urgh!!!), and now I write code in C# 3.0!

I was born in 1968 in Italy and I live in the north west of my country (near Venice). Nowadays I work as a Project Leader/Senior Developer for an Italian software house.

Computer Science interests: I like coding 3D-GameEngine and OpenGL applications.

Sport and hobbies: Martial Arts, Trekking, travels, reading and writing novels.

Occupation: Software Developer (Senior)
Location: Italy Italy

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
Generalabout asp.net web form designer Pinmemberwyj806663:15 15 Jun '09  
GeneralEditare una form esistente Pinmemberallenbradley1:36 28 Apr '09  
GeneralDisplaying control size and position at the bottom-right corner (like Visual Studio does) PinmemberMember 29138843:17 10 Apr '09  
GeneralDoes your implementation solve the memory leak in the base DesignSurface? Pinmembertribbles12:45 17 Feb '09  
AnswerRe: Does your implementation solve the memory leak in the base DesignSurface? PinmemberPaolo Foti5:20 25 Mar '09  
Generalwithout TabOrderHooker Pinmemberaport@bupt22:27 10 Dec '08  
QuestionSmart Tags Pinmemberdanreber11:05 3 Oct '08  
QuestionRe: Smart Tags [modified] Pinmemberdanreber11:10 3 Oct '08  
AnswerRe: Smart Tags Pinmembervenkeey14:52 9 Oct '08  
GeneralSerialization PinmemberMichaelgor10:52 28 Sep '08  
AnswerRe: Serialization PinmemberPaolo Foti0:40 15 Oct '08  
GeneralRe: Serialization PinmemberFadik23:59 14 Jul '09  
GeneralFantastic code Pinmembergshea5:06 5 Sep '08  
GeneralRe: Fantastic code PinmemberPaolo Foti4:06 22 Sep '08  
GeneralHow to handle ComboBox Tasks pane Pinmemberdkalyan22:23 1 Sep '08  
QuestionRe: How to handle ComboBox Tasks pane PinmemberPaolo Foti4:03 22 Sep '08  
GeneralContext Menu on Surface Pinmemberdkalyan0:11 22 May '08  
AnswerRe: Context Menu on Surface PinmemberPaolo Foti7:06 29 May '08  
GeneralRe: Context Menu on Surface Pinmemberdkalyan22:18 1 Jun '08  
GeneralDraging Controls From one panel to surface Pinmemberdkalyan20:30 14 May '08  
AnswerRe: Draging Controls From one panel to surface PinmemberPaolo Foti2:54 16 May '08  
GeneralRe: Draging Controls From one panel to surface Pinmemberdkalyan3:35 19 May '08  
GeneralRe: Draging Controls From one panel to surface PinmemberPaolo Foti6:27 19 May '08  
GeneralRe: Draging Controls From one panel to surface Pinmemberdkalyan22:33 21 May '08  
GeneralNice, but what about DesignSurfaceManager? PinmemberMember 27922680:33 9 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Mar 2008
Editor: Deeksha Shenoy
Copyright 2008 by Paolo Foti
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project