Solution to Windows form nesting

Source: Internet
Author: User
Form nesting

Experienced Visual Basic developers know the multi-Document Interface (MDI) ApplicationProgramIt can contain child forms, which are managed by the MDI parent form. But how do you implement nested forms without the MDI capability? For example, an MDI subform may also need to contain another form.

Sometimes this function can be implemented using a user control, but if you really need to nest a form into another form, there are multiple ways to implement it. The form is derived from the control class, which means it can be put into the control set of another form, using the following logic:

Dim F as new frmembed2 ()
Me. Controls. Add (f)
F. Show ()

But unfortunately, this sectionCodeThis will cause a runtime exception (see figure 1 ).

 

Figure 1. runtime error when trying to add a form to the control set of another form

To avoid this exception, the toplevel attribute of the form must be set to false (see the following code ).

Dim F as new frmembed2 () F. toplevel = false me. Controls. Add (f) F. Show ()

Subsidy description (C #):
When developing a Windows program using Visual Studio 2008, you need to display a new form in the container control. If an exception occurs in the following code, you cannot add the top-level control to the control:
Myform = new myform ();
Container. Controls. Add (myform );
I checked the relevant information online, and the solution is very simple. Because form is a top-level control by default, an exception occurs when adding a form to a container control.
You only need to set the form to a non-top-level control before adding the control code.
Myform = new myform ();
Myform. toplevel = false;
Container. Controls. Add (myform );

Figure 2 shows that one form implemented using the above logic is embedded into another form. The embedded form has a title bar (its color is an inactive system color). Therefore, the embedded form can be dragged around the container form. In Figure 2, the form is dragged from its start position (upper left corner) to the lower right corner.

 

Figure 2. There is an embedded form in the container form. The embedded form can be dragged in the container form.

Generally, the position of an embedded form is set before it is displayed. You only need to set the left and top attributes of the embedded form. The position of the embedded form is relative to that of the container form.

Unlike the MDI child form, the embedded form can overwrite the controls on the container form. Figure 3 shows their differences.

Figure 3. embedded forms (left) can overwrite controls on container forms. The right side of the MDI child form cannot overwrite the controls on the MDI parent form.

In the MDI example on the right, there is no way to hide the button behind the child form. However, the button on the left is overwritten by the embedded form.

When the form is embedded for the first time, it will be displayed after the existing controls on the container form. When it is clicked, it goes to the front end and stays there. This will disturb the user, but it can be prevented by inserting the following code:

F. bringtofront ()

Embedded forms can contain other embedded forms without actual restrictions. Figure 4 shows an embedded form that contains an embedded form.

Figure 4. An embedded form containing an embedded form

Related Article

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.