Nevron .NET Vision
Diagram for .NET / User's Guide / Document Object Model / Models / Shapes / WinForm Control Host Shape
In This Topic
    WinForm Control Host Shape
    In This Topic

    The NWinFormControlHostShape helps you embed any WinForm control inside a drawing document. The following code example shows how to create a shape that embeds a button:

    C#
    Copy Code
    // create a button
    Button button = new Button();
    NWinFormControlHostShape buttonShape = new NWinFormControlHostShape(button);
    document.ActiveLayer.AddChild(buttonShape);
    
    VB.NET
    Copy Code
    ' create a button
    Dim button As New Button()
    Dim buttonShape As New NWinFormControlHostShape(button)
    document.ActiveLayer.AddChild(buttonShape)
    

    WinForm controls cannot export to any vector format, so when a WinForm control is embedded in a drawing it is displayed as a raster image, which is generated by the control (e.g. the WinForm control is asked to paint itself in an off-screen bitmap). This is suitable for form builder applications where a simple preview of the control is needed.

    A simple image preview is not enough for more advanced interactivity scenarios. The NWinFormControlHostShape exploits the inplace editing features of shapes and provides a special inplace editing control, which hosts the control that was provided to the WinForm control constructor. This makes it possible for the control to be displayed in a WinForm diagramming application, just as any other control. The hosted control will be activated in place, when you call the StartInplaceEditing method of the view:

    C#
    Copy Code
    // start inplace editing 
    view.StartInplaceEditing(buttonShape);
    
    VB.NET
    Copy Code
    ' start inplace editing 
    view.StartInplaceEditing(buttonShape)
    

    By default inplace editing automatically ends when the inplace edit control looses focus. In the context of WinForm control hosting this may not always be desirable (for example you may want the button to be active all the time regardless of whether it is focused or not). To prevent deactivation on lost focus - set the DeactiveOnLostFocus property of the shape to false.

     Serialization

    WinForm controls do not natively support serialization in general. It is however required that certain drawing documents save and load the settings of the WinForm controls embedded in them. For example: if you assign the Text property of a button and save and load the drawing you would expect this Text property to be persisted.

    To implement WinForm controls serialization we have used the designer serialization support that WinForm controls gained ad-hoc. This feature is used in the Visual Studio form designer code serialization (with arguable results). As a consequence you can expect that drawing documents will persist all WinForm control properties that are marked with the DesignerSerializationVisibility - Visible or Content settings.

     Additional Limitations

    The limitations of the WinForm control host shape are associated with the inherent limitations of WinForm controls, which are:

    • Rotation - by default the WinForm control host shape rotation is disabled, since there is no way you can rotate the inplace editing control.
    • Zooming - it is not a good idea to use zooming with drawings that embed WinForm controls, because WinForm controls cannot zoom their content.
    • Vector Export - in all vector image formats embedded WinForm controls will be rendered as images, since WinForm controls cannot render vector contents.
     Related Examples
    Windows Forms: Document Object Model - Shapes - WinForm Controls Hosting