Using DockPanel and C # to make forms floating and docking (VS2010)

Source: Internet
Author: User

using DockPanel and C # to make forms floating and docking (VS2010) Click the function window and then drag the mouse Form2 effect diagram as follows:

1. Download the DockPanel. on SF can be down to the latest version of the DLL and demo. Unzip the file to get the following image file:

2, build the main form (parent form): Frmmain. (1) New project: Floatingform
(2) Copy Dockpanel.config and WeifenLuo.WinFormsUI.Docking.dll to the Floatingform\floatingform\bin\debug file of the current project.


(3) Add the reference WeifenLuo.WinFormsUI.Docking first.
Then click the Toolbox to add the DockPanel control to the toolbox.

(4) Add the main form frmmain and set the IsMdiContainer =true of the main form;
(5) Add the DockPanel control to the main form: DockPanel1, and set the Documentstyle:dockPanel.DocumentStyle = Documentstyle in the DockPanel. Dockingmdi;

the Frmmain interface is as follows: (If you add a DockPanel control to a frmmain error, go to the Attention Item view)

The background code is as follows:

Using System;

Using System.Collections.Generic;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Linq;

Using System.Text;

Using System.Windows.Forms;

Using WeifenLuo.WinFormsUI.Docking;

Using System.IO;

Namespace Floatingform

{

public partial class Frmmain:form

{

Public Frmmain ()

{

InitializeComponent ();

}

private string M_dockpath = String. Empty;

private void Form1_Load (object sender, EventArgs e)

{

This. Dockpanel1.documentstyle = Documentstyle.dockingmdi;

This.m_dockpath=path.combine (Path.getdirectoryname (Application.executablepath), "DockPanel.config");

This. Initdockpanel ();

This. STATUSBAR.ITEMS.ADD ("Ready"); This is the status bar, you can not, do not affect.

}

#region Initialize DockPanel by configuration file

private void Initdockpanel ()

{

Try

{

Dynamically load a floating form based on a profile

This. Dockpanel1.loadfromxml (This.m_dockpath, Delegate (stringpersiststring)

{

Feature form

if (persiststring ==typeof (frmfunction). ToString ())

{

Returnfrmfunction.getinstance ();

}

Forms outside the main frame are not displayed

return null;

});

}

catch (Exception)

{

Load a subform by system default rule if the configuration file does not exist or if there is a problem with the configuration file

Frmfunction.getinstance (). Show (this. DOCKPANEL1);

}

}

#endregion

private void Frmmain_formclosing (object sender, FormClosingEventArgs e)

{

Try

{

For the next time you open the program, the floating form is displayed in the same location as it was closed.

Dockpanel1.saveasxml (This.m_dockpath);

}

catch (Exception ex)

{

MessageBox.Show ("Save DockPanel Config file failed," + ex.) message);

Return

}

}

private void Dockpanel1toolstripmenuitem_click (object sender, Eventargse)

{

Frmfunction Frmfun = Frmfunction.getinstance ();

Frmfun.show (this. DockPanel1, appconfig.ms_frmfunction);

This. Statusbar.items[0]. Text = Frmfun.text;

}

}

}

3. Build the form that needs floating display: Frmfunction. (1) Add a form to the current project: frmfunction; (note: Floating Forms and label forms need to inherit from Dockcontent);
(2) In order to ensure that after the closure of a floating form, and then open to display in the original location, to the floating form processing, handling the form of the Dockstatechanged event, the label form dock position change, recorded to the public class;

the Frmfunction interface is as follows: (the form to be floated)

The background code is as follows:

Using System;

Using System.Collections.Generic;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Linq;

Using System.Text;

usingSystem.Windows.Forms;

usingWeifenLuo.WinFormsUI.Docking;

Namespace Floatingform

{

public partial class Frmfunction:dockcontent

{

private static frmfunctioninstance;

 

Public Frmfunction ()

{

InitializeComponent ();

}

Public static Frmfunction getinstance ()

        {

if (Instance = null)

            {

Instance = new Frmfunction ();

            }

return Instance;

        }

To ensure that a floating form is turned off and displayed in its original location, to handle the floating form, handle the Dockstatechanged event of the form, change the dock position of the label form, and record it to the public class

private void Frmfunction_dockstatechanged (object sender, EventArgs e)

{

//Closed (Dockstate for unknown) do not save Dockstate

if (Instance!= null)

            {

if (this. Dockstate ==dockstate.unknown | | This. Dockstate = = Dockstate.hidden)

               {

return ;

                }

appconfig.ms_frmfunction =this. Dockstate;

            }

}

private void Frmfunction_formclosing (object sender, Formclosingeventargse)

{

Instance = null; Otherwise the next time to open the Times is wrong, prompt "Cannot access freed objects"

}

}

4. Add Class AppConfig (public Class) to the current project. the code is as follows:

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using WeifenLuo.WinFormsUI.Docking;

Namespace Floatingform

{

Class AppConfig

{

Public static Dockstate ms_frmfunction =dockstate.dockleft; function form, dock on left side

}

}

5. Run successfully to achieve basic functions (docking in the middle of the effect map)

Attention Matters

question (1) Description:

VS2010 Add the WeifenLuo.WinFormsUI.Docking.DockPanel.dll file, add the DockPanel control times error from the toolbar, hint "type universe cannot resolve assembly: system.design , version=2.0.0.0, Culture=neutral, publickeytoken=b03f5f7f11d50a3a. 】

Workaround:

Open the Floatingform property of the project, select application, modify the target framework (all configurations) option, and select the. NET Framework 4 in the Drop-down box option.


I personally look at others after the blog, in their own hands after the personal summary, mainly wrote a number of major steps and precautions, if there are flaws, welcome advice. Thank you. (share happiness)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.