DockPanel Use Tips

Source: Internet
Author: User

The basic use of DockPanel I will not say, online a lot, I want to say is in the use of DockPanel need to pay attention to a few small problems

The first one:

Anyone who has ever used a dockpanel may have encountered an error such as:

Invalid Content:activecontent must be one of the visible contents, or null if there is no visible Content.

The meaning of translation is roughly: invalid content: If there is no visible content, activecontent must be visible content or empty.

What is the specific reason, we can discuss each other. Let me say a few things about this problem

The This keyword in code represents the form where DockPanel is Form1

1), when the DockPanel Documentstyle is not dockingmdi, the following code will appear this problem

Frm_a Frma = null;
To determine if a subform already exists in DockPanel
foreach (dockcontent frm in this.dockPanel1.Contents)
{
if (frm is frm_a)
{
frm.     Activate (); Activate child form
Return
}
}

Frma = new Frm_a ();
Frma.mdiparent = this;
Frma.show (THIS.DOCKPANEL1);

Solution: See if you set the DockPanel Documnetstyle as Dockingmdi. You can also try several other ways (Dockingwindow,dockingsdi,systemmdi)

2), set the DockPanel Documentstyle is not dockingmdi, if you want to set the form Frm_b to the left floating form, you need to set the form Frm_b Dockareas for and only for Dockleft, If you want to implement other features to set up other property information yourself, now look at the following code

Frm_b FRMB = null;
To determine if a subform already exists in DockPanel
foreach (dockcontent frm in this.dockPanel1.Contents)
{
if (frm is Frm_b)
{
frm.     Activate (); Activate child form
Return
}
}

FRMB = new Frm_b ();
Frmb.mdiparent = this;
Frmb.show (This.dockpanel1,dockstate.dockleft);

Note that if you add a red-annotated code to your code, the program will also report the error when it is running.

Solution: Annotate the Red code.

Reason: (Personal understanding) frmb.show (this.dockpanel1,dockstate.dockleft); This code is actually set FRMB docked only to the left of DockPanel, at this time the FRMB is not part of the MDI child form, So once you add the red code, the program will complain.

The second one:

Drag, dock, and fix a subform (shown in DockPanel)

Drag: If you want to make your subform can drag arbitrarily, then you set the Dockareas property of the subform, keep the default value, do not modify.

Docking: First you need to set the location of the Dockareas, you can dock in the left, right, inferior, or through the program code control, refer to the above code.

Fixed: Just set the dockareas of your form as document.

The third one:

The contents and the judgment of the child form

Many times you need to determine how many subforms or contents exist in DockPanel, refer to the following code:

foreach (Form in this.) Mdichildren)

{

When this is judged, the docked form is not counted

}

and

foreach (dockcontent frm in this.dockPanel1.Contents)
{

After this setting, all the inherited and dockcontent forms will be counted.

}

Fourth one:

Find the main form, animate the subform

Reference Chart:

Implementation of the function: here we need to implement, right click on a form, through the right menu to display form B.

The object of the main form
Form1 Form1;

private void Showb_click (object senders, EventArgs e)
{

Getfrmmain (); Use this function to get Form1

foreach (Form frm in Form1. Mdichildren)
{
if (frm is Frm_b)
{
frm. Activate ();
Return
}
}

Frm_b FRMB = new Frm_b (this);
Frmb.mdiparent = Form1;
Frmb.show (FORM1.DOCKPANEL1);

}

private void Getfrmmain ()

{

if (this. Parent.Parent.Parent.Parent!= null)
{
Form1 = (Form1) this. Parent.Parent.Parent.Parent;
}
Else
{
Form1 = (Form1) this. Parent.Parent.Parent;
}

}

Now in form A, this keyword is not the main form of code, so here we need to get the main form object

This is required when form a is docked. Parent.Parent.Parent.Parent (four)

When not docked, only three of this is required. Parent.Parent.Parent

Debug code discovery: When docked

This. Parent is {WeifenLuo.WinFormsUI.Docking.DockPane}

This. Parent.parent is {WeifenLuo.WinFormsUI.Docking.DockWindow, BorderStyle:System.Windows.Forms.BorderStyle.None}

This. Parent.Parent.Parent is {WeifenLuo.WinFormsUI.Docking.DockPanel, BorderStyle:System.Windows.Forms.BorderStyle.None }

This. Parent.Parent.Parent is {testevenhandler.form1, TEXT:FORM1} is the main form we are looking for Form1

When not docked:

This. Parent is {WeifenLuo.WinFormsUI.Docking.DockPane}

This. Parent.parent is {weifenluo.winformsui.docking.dockpanel+autohidewindowcontrol, BorderStyle: System.Windows.Forms.BorderStyle.None}

This. Parent.Parent.Parent is {testevenhandler.form1, TEXT:FORM1} is the main form we are looking for Form1

Four small problems, is not a skill, I have encountered in the development, the reason may explain is not very clear, forget everyone to explore each other and common progress.

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.