How to simulate the findform () function in the user control in the component (Component)

Source: Internet
Author: User
Using component programming is a highly respected technology. It has a visual interface programming mode and is not as bulky as usercontrol. However, anyone familiar with usercontrol knows that there is a findform () function in the usercontrol class that returns a reference to the form where usercontrol is located, this makes it much easier for us to control the form-although it may be a bit more appealing, sometimes we need this control capability.
However, component does not provide such a function. You can use other techniques to reference the form of component. For example, you can use application in component constructor. addmessagefilter (this), and then extract the handle of the message sent from the form to obtain the reference of the form. The disadvantage is that the form reference cannot be obtained at the design time; for example, you can add a styleform attribute to component, and then you can select one with the mouse in the designer. The disadvantage is that you must manually select one.
Today, it took half a day to finally design a solution to overcome the above two shortcomings, Code As follows: Using System;
Using System. componentmodel;
Using System. Collections. Generic;
Using System. diagnostics;
Using System. text;
Using System. componentmodel. design;
Using System. Windows. forms;
Namespace Findformsimulation
{
Public Partial Class Basestyle: Component
{
Public Basestyle ()
{
Initializecomponent ();
}

Public Basestyle (icontainer container)
{
Container. Add (This);

Initializecomponent ();
}
/**/ /// <Summary>
///Here, the key is to reload the base class site.
/// </Summary>
Public   Override Isite site
{
Get
{
Return Base. Site;
}
Set
{
If ( Base . Site ! = Value)
{
Base . Site = Value;
// Use the reflection mechanism to obtain the form you want to control at the design time.
Ireferenceservice referenceservice = (Ireferenceservice) This . Getservice ( Typeof (Ireferenceservice ));
If (Referenceservice ! =   Null )
{
/**/ ///The following sentence is used to obtain the form object of this component.
Object [] Parent = Referenceservice. getreferences ( Typeof (Form ));
Form container = Parent [ 0 ] As Form;
Styleform = Container;
/**/ ///Test the following method. We can know that parent. length is always 1.
// Styleform. Text = parent. length. tostring ();
}
}
}
}
Private Form styleform =   Null ;
[Description ( " The form to be controlled by this component " ), Defaultvalue ( Null )]
Public Form styleform
{
Get   {ReturnStyleform ;}
Set
{
If (Styleform ! = Value)
{
Styleform=Value;
}
}
}
}
}

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.