Click here to Skip to main content
13,042,421 members (111,612 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 24 Jun 2005

Undockable Custom Control in an MDI Application

, 24 Jun 2005
Rate this:
Please Sign up or sign in to vote.
A custom made control that can be docked and undocked in an MDI application.


In this second article of mine I would like to share with you how I made a custom control undockable. Well, before I start I want to mention that I'm not a professional so the code itself is not the best-practice.


The user should be aware of the Windows Forms and controls.

Let's dive into the code!

This is not a tutorial, so I will not define all the steps thoroughly. Just the main points will be covered here; anyway I have provided plenty of comments in the source code for reference.

Well, I made the control undockable in the following way:

  1. Create a Windows application project and make it MDI.
  2. Add a Windows Form (named "Canvas") for adding a child window to the MDI Parent.
  3. Create a user control (named "Toolbar") and add the following code to the buttons:
    private void button1_Click(object sender, System.EventArgs e) 
    //a new child window is created and 
    //assigned to the MDIParent...
        Canvas canvasForm = new Canvas(); 
        //checkes if the control is docked or not...and gets
        if (this.ParentForm.IsMdiContainer == true)  
        //the MDIContainer correctly according to the situation 
            canvasForm.MdiParent = this.ParentForm; 
            canvasForm.MdiParent = this.ParentForm.Owner; 
    //docks or undocks the control
    private void button2_Click(object sender, System.EventArgs e) 
        //gets the correct control (docked control 
        //or undocked control)
        if (this.ParentForm.Text.Length != 0)  
            this.Visible = false; //docked control is made hidden here... 
            if (undockedWnd == null) 
                //creates the undockedcontrol...and the parent
                undockedWnd = new UndockedControl ();  
                //form is assigned as its owner 
                undockedWnd.Owner = this.ParentForm; 
            //if already created only the "visible" flag is changed to true... 
                undockedWnd.Visible = true; 
        else //undocked control 
            //the undocked control is made hidden here...
            this.ParentForm.Visible = false;  
            //captures the main parentform...
            DrawingGUI prntForm = (DrawingGUI) this.ParentForm.Owner;  
            prntForm.toolBarControl1.Visible = true; 
  4. Add this user control to the main application (MDI application). After adding the control, go and change its accessibility level from private to public. I made this change in order to reach at it later on from the undocked control.
  5. Add a Windows Form (named UndockedControl). Add the "Toolbar" to this form. I also added a ComboBox to the form for the user to change its transparency level.
  6. In the coding of the "Canvas", I have included some code for the Move event of the form in order to rearrange the layout when the scrollbars are displayed. They are not perfect and need much more improvement.

Points of interest

Well for those who are interested in the canvas background that is the gradient color, I'd like to give the following code: (I got it from Using the LinearGradientBrush.)

Add this code in the Paint event of "Canvas" form.

Rectangle rect = new Rectangle (pictureBox1.Location, pictureBox1.Size);
LinearGradientBrush br=new LinearGradientBrush(

e.Graphics.FillRectangle(br, rect);

//used for the sample text
StringFormat format = 
Font fn = new Font("Tahoma", 9);
SizeF sf = e.Graphics.MeasureString ("Test String",fn,1024,format);
LinearGradientBrush br3 = 
  new LinearGradientBrush(new RectangleF(pictureBox1.Left,

   "Test String", 
   new Font ("Arial", 9, FontStyle.Bold), 
   new Rectangle (rect.X, rect.Y, rect.Width/4, rect.Height), 

I hope this article helps someone out there...if anything goes wrong or there is a better way of coding (I'm sure there is...) please let me know...


I have added some code for a flicker-free interface. I have used the .NET way to prevent flickering. I tested it on a WinXP SP2 with .NET 1.1 installed.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Evren Daglioglu
Software Developer
Turkey Turkey
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionOo ? Pin
juras99-Nov-08 10:20
memberjuras99-Nov-08 10:20 
GeneralBug on canvas maximize Pin
Indy20052-Apr-07 18:40
memberIndy20052-Apr-07 18:40 
GeneralFlicker Pin
fwsouthern25-Jun-05 5:53
memberfwsouthern25-Jun-05 5:53 
GeneralRe: Flicker Pin
Evren Daglioglu26-Jun-05 21:04
memberEvren Daglioglu26-Jun-05 21:04 
GeneralRe: Flicker Pin
fwsouthern27-Jun-05 3:07
memberfwsouthern27-Jun-05 3:07 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 24 Jun 2005
Article Copyright 2005 by Evren Daglioglu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid