Skip to content

ControlHostService Component

Gary edited this page Aug 27, 2014 · 1 revision

Table of Contents

The ControlHostService component is responsible for exposing client controls in the application's main form. The control host service allows the client to specify a preferred location for the main form, and calls the client back when the control gets or loses focus, or is closed by the user. This component also provides a dock in which to hold controls, such as menus and tool strips. The docking control allows moving windows around on the dock. The window configuration can be saved, using the components WindowLayoutService and WindowLayoutServiceCommands.

Uses and Interfaces

ControlHostService implements the IControlHostService interface. IControlHostService offers methods similar to ICommandService. IControlHostService offers methods to register, unregister, and show controls, instead of commands.

ControlHostService also implements IControlRegistry, which tracks active controls, noting when controls are activated or deactivated. For more information, see Using Controls in ATF.

The ControlHostService component, or one like it, is useful to any application that has a GUI and is used by nearly all the ATF samples. Because it is a component, applications simply need to add ControlHostService to the MEF TypeCatalog. Applications also need to add the CommandService and SettingsService components, because ControlHostService imports ICommandService and ISettingsService.

For WinForms, ControlHostService also imports IContextMenuCommandProvider, which is used for getting a list of commands to present on a context menu. IContextMenuCommandProvider is imported using an [ImportMany] attribute, and you don't have to provide such an import for ControlHostService to work. This import is provided in case there are any controls in the application that have context menus.

For more information on satisfying components' import needs, see Using ATF Components.

There are versions of ControlHostService and IControlHostService for both WinForms and WPF.

For more information, see Using Controls in ATF.

Registering a Control

Here is a simple example of creating and registering a control from the PaletteService component. The call to RegisterControl() uses the ControlInfo class, which describes the control in the UI. This class is analogous to the CommandInfo class for commands, and it provides information on the control, such as a text description ("Palette").

m_control = new UserControl();
m_control.Dock = DockStyle.Fill;
m_control.SuspendLayout();
m_control.Name = "Palette".Localize();
m_control.Text = "Palette".Localize();
m_control.Controls.Add(m_searchInput);
m_control.Controls.Add(TreeControl);
m_control.Layout += controls_Layout;
m_control.ResumeLayout();

m_controlHostService.RegisterControl(
    m_control,
    new ControlInfo(
        "Palette".Localize(),
        "Creates new instances".Localize(),
        StandardControlGroup.Left),
    this);

To learn more, see Registering Controls.

Topics in this section

Clone this wiki locally