Click here to Skip to main content
11,582,850 members (71,577 online)
Click here to Skip to main content

Undockable Custom Control in an MDI Application

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

Introduction

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.

Background

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; 
        } 
        else 
        { 
            canvasForm.MdiParent = this.ParentForm.Owner; 
        } 
        canvasForm.Show();
    } 
    
    
    //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; 
                undockedWnd.Show(); 
            } 
            else 
            //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(
            rect,
            Color.BlueViolet,
            Color.Black,
            45.0f,
            false);

e.Graphics.FillRectangle(br, rect);

//used for the sample text
StringFormat format = 
   (StringFormat)StringFormat.GenericTypographic.Clone();
Font fn = new Font("Tahoma", 9);
SizeF sf = e.Graphics.MeasureString ("Test String",fn,1024,format);
LinearGradientBrush br3 = 
  new LinearGradientBrush(new RectangleF(pictureBox1.Left,
                      pictureBox1.Top,sf.Width,sf.Height),
                      Color.Yellow,
                      Color.Red,
                      0,
                      false);

e.Graphics.DrawString(
   "Test String", 
   new Font ("Arial", 9, FontStyle.Bold), 
   br3, 
   new Rectangle (rect.X, rect.Y, rect.Width/4, rect.Height), 
   format);
...

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

History

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.

License

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

Share

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    Rant Rant    Admin Admin   

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

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