Hosting user control in SOLIDWORKS Task Pane using SwEx.AddIn framework

Edit ArticleEdit Article

Any System.Windows.Forms.UserControl can be hosted in the Task Pane by calling the ISwAddInEx.CreateTaskPane method.

MyControlHost ctrl;
var taskPaneView = CreateTaskPane<MyControlHost>(out ctrl);

Both COM-visible and not COM-visible controls are supported

public partial class MyControlHost : UserControl
{
    public IssuesControlHost()
    {
        InitializeComponent();
    }
}
...
[ComVisible(true)]
public partial class MyComVisibleControlHost : UserControl
{
    public IssuesControlHost()
    {
        InitializeComponent();
    }
}

It is recommended to use COM-visible controls when hosting Windows Presentation Foundation (WCF) control in System.Windows.Forms.Integration.ElementHost as keypresses might not be handled properly in com-invisible controls.

Defining Commands

It is possible to define task pane commands to be added as buttons. It is required to declare the enumeration with commands and provides the commands handler.

public enum TaskPaneCommands_e
{
    Command1
}

...
TaskPaneControl ctrl;
var taskPaneView = CreateTaskPane<TaskPaneControl, TaskPaneCommands_e>(OnTaskPaneCommandClick, out ctrl);
...

private void OnTaskPaneCommandClick(TaskPaneCommands_e cmd)
{
    switch (cmd)
    {
        case TaskPaneCommands_e.Command1:
            //TODO: handle command
            break;
    }
}

Commands can be attributed with TitleAttribute and IconAttribute or TaskPaneIconAttribute for specifying the tooltip and icon respectively.

Standard icon can be set by using the TaskPaneStandardButtonAttribute attribute where the values defined in swTaskPaneBitmapsOptions_e enumeration

Please see the image below for the diagram of elements of Task Pane.

Task Pane control
Task Pane control

  1. WinForms User Control hosted in the Task Pane
  2. Task Pane button with the custom icon
  3. Task Pane button with default icon
  4. Task Pane button with standard swTaskPaneBitmapsOptions_Back icon
  5. Task Pane button with standard swTaskPaneBitmapsOptions_Next icon
  6. Task Pane button with standard swTaskPaneBitmapsOptions_Ok icon
  7. Task Pane button with standard swTaskPaneBitmapsOptions_Help icon
  8. Task Pane button with standard swTaskPaneBitmapsOptions_Options icon
  9. Task Pane button with standard swTaskPaneBitmapsOptions_Close icon
  10. Tooltip for Task Pane button
  11. Custom icon for Task Pane Tab
  12. Default icon for Task Pane Tab
  13. Tooltip for Task Pane Tab

Product of Xarial Product of Xarial