Use and customize of host of forms of Visual Studio 2005 in your applications .net
[30 mn of reading - published 6/6/2006 8:37:04 PM - Target : Confirmé]
|
   
|
Author
2 The DesignSurface class
The class System.ComponentModel.Design.DesignSurface goes allows us to create a basic host of forms. It is by this class that one will be able to add or customize the functionalities of the originator via services (IDesignerHost, ISelectionService, INameCreationService…) which we will study in the next chapters.
Let us examine somebody of these methods, properties and events:
- The property IsLoaded indicating if the originator to host be charged.
- The property View, it is seen on the host.
- The property ServiceContainer makes it possible to modify the services associated with the host.
- The method BeginLoad makes it possible to charge the control container or control root, as well as the host.
- The method GetService makes it possible to obtain the services associated with the host.
To start we will create a class "Designer" in the project which will inherit the DesignSurface class as follows:
|
public class Designer : System.ComponentModel.Design.DesignSurface {
//Constructor
public Designer(){}
} |
This class will allow us to add or to modify the services of DesignSurface bus according to the MSDN one can make only in the constructor of the DesignSurface class from where the need to create this class. For the moment one will leave this class just as it is, us will modify it with the wire of the chapters in order to add the various services presented.
We will modify our example by associating an event Load and FormClosing (to release the resources) the form of the application:
|
Designer designer = null;
// event of loading of the form of application (event Load)
private void Form1_Load(object sender, EventArgs e)
{
// Creation the host of the form
designer = new Designer();
// Assocation of the control container to the designer
designer.BeginLoad(typeof(Form));
// Throw a exception if the designer isn't loaded
if (!designer.IsLoaded)
throw new Exception("Le concepteur n'a pu être chargé");
// It is specified that view it of the designer extend on all the control which will host it
((Control)designer.View).Dock = DockStyle.Fill;
// Add the view of designer in the panel
panel1.Controls.Add((Control)designer.View);
}
// Release the ressources
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Delete the view of designer of panel
panel1.Controls.Remove((Control)designer.View);
// Release the designer to the form
designer.Dispose();
} |
The BeginLoad method of the DesignSurface class makes it possible to charge the control container or control root, it is with a statement the control relative which will accomodate all other controls which will be associated there. For example in Visual Studio 2005, at the time you to publish a form, the latter is the control container of the host. The BeginLoad method can take in parameter the type of the control container which can be of all kinds (for example a button, a panel, a userControl…). Here in the example one associated Form to the host. The View property of the DesignSurface class represents seen it of the host and to use it it is necessary to add it to a control which goes the reception. In the example, it is the panel which will play this role.
To compile and launch the project and here what you obtain:
The panel changed, one can distinguish there a form which will be the control container of the originator.
|