Articles in this section
Category / Section

How do I customize the behavior of a user interface tool?

2 mins read

How do I customize the behavior of a user interface tool?

User interface tools may be customized by either subclassing and overriding the behavior of a base tool, or by implementing a new Tool type that derives from the Syncfusion.Windows.Forms.Diagram.Tool base class, and is modeled after an existing Tool type.

The following code shows the outline of a new symbol insert tool that is modeled after the standard Syncfusion.Windows.Forms.Diagram.SymbolInsertTool. The custom insert tool differs from the base tool by displaying a message box instead of the BoundaryConstraintsException when the insertion point violates the boundary, and lets you re-insert the symbol at a new location.

C#

/// Custom InsertSymbolTool that displays a message when the symbol insertion point violates the
/// diagram's boundary constraints.
public class MyInsertSymbolTool : Tool, IMouseEventReceiver
{
public MyInsertSymbolTool() : base("MyInsertSymbolTool")
{
}
public MyInsertSymbolTool(string name) : base(name)
{
}
public MyInsertSymbolTool(string name, System.Type symbolType) : base(name)
{
this.symbolType = symbolType;
}
/// Called when the tool is activated.
protected override void OnActivate()
{
...
}
/// Called when the tool is deactivated.
protected override void OnDeactivate()
{
...
}
/// Called when a mouse down event occurs.
void IMouseEventReceiver.MouseDown(System.Windows.Forms.MouseEventArgs e)
{
...
}
/// Called when a mouse move event occurs.
void IMouseEventReceiver.MouseMove(System.Windows.Forms.MouseEventArgs e)
{
...
}
/// Called when a mouse up event occurs.
void IMouseEventReceiver.MouseUp(System.Windows.Forms.MouseEventArgs e)
{
...
}
}

To replace an existing Tool with the new Tool type, you will have to first use the Syncfusion.Windows.Forms.Diagram.Controller.UnRegisterTool(Tool tool) method to unregister the previously registered default Tool, and then use the Controller.RegisterTool(...) method to register your newly defined Tool.

C#

// Check whether the standard InsertSymbolTool is registered with the Controller, and
// unregister the tool it if present.
Tool[] regtools = this.diagramComponent.Controller.GetAllTools();
foreach(Tool regtool in regtools)
{
if(regtool.Name == "InsertSymbolTool")
{
this.diagramComponent.Controller.UnRegisterTool(regtool);
break;
}
}
// Register the custom insert symbol tool
this.diagramComponent.Controller.RegisterTool(new MyInsertSymbolTool());

 

VB

' Check whether the standard InsertSymbolTool is registered with the Controller, and
' unregister it if present.
Dim regtools() As Tool = Me.diagramComponent.Controller.GetAllTools()
Dim regtool As Tool
For Each regtool In regtools
If regtool.Name = "InsertSymbolTool" Then
Me.diagramComponent.Controller.UnRegisterTool(regtool)
End If
Next
' Register the custom insert symbol tool
Me.diagramComponent.Controller.RegisterTool(New MyInsertSymbolTool)

The attached file contains the full C# and VB source code for the MyInsertSymbolTool class. The definitions for the standard tools that ship with Essential Diagram can be found under the 'Essential Studio\...\Base\Diagram.Base\Src\Tools\' folder. Referring to these classes will give a better idea on to how to go about implementing your custom tools.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied