How do I create a custom symbol?
The following code sample demonstrates how you can create a custom symbol and use it in Essential Diagram:
1. Create the custom symbol:
[C#]
// Custom Symbol (MySymbol.cs)
public class MySymbol : Symbol {
private Syncfusion.Windows.Forms.Diagram.Rectangle outerRect = null;
private Ellipse innerEllipse = null;public MySymbol() {
//////////////////////////////////////////////////////////////////
// Add child nodes to the symbol programmatically
//////////////////////////////////////////////////////////////////// Add an outer rectangle
this.outerRect = new Syncfusion.Windows.Forms.Diagram.Rectangle(0, 0, 120, 80);
this.outerRect.Name = "Rectangle";
this.outerRect.FillStyle.Color = Color.Khaki;
this.AppendChild(outerRect);// Add an inner ellipse
this.innerEllipse = new Ellipse(10, 10, 100, 60);
this.innerEllipse.Name = "Ellipse";
this.AppendChild(innerEllipse);// Add Label
Label lbl = this.AddLabel("My Symbol", BoxPosition.Center);
lbl.BackgroundStyle.Color = Color.Transparent;
}
}
[VB.NET]
' Custom Symbol (MySymbol.vb)
Public Class MySymbol
Inherits SymbolPrivate outerRect As Syncfusion.Windows.Forms.Diagram.Rectangle = Nothing
Private innerEllipse As Ellipse = NothingPublic Sub New()
' Add child nodes to the symbol programmatically
' Add an outer rectangle
Me.outerRect = New Syncfusion.Windows.Forms.Diagram.Rectangle(0, 0, 120, 80)
Me.outerRect.Name = "Rectangle"
Me.outerRect.FillStyle.Color = Color.Khaki
Me.AppendChild(outerRect)' Add an inner ellipse
Me.innerEllipse = New Ellipse(10, 10, 100, 60)
Me.innerEllipse.Name = "Ellipse"
Me.AppendChild(innerEllipse)' Add Label
Dim lbl As Label = Me.AddLabel("My Symbol", BoxPosition.Center)
lbl.BackgroundStyle.Color = Color.Transparent
End Sub ' New
End Class ' MySymbol
2. Using the symbol in the form:
[C#]
//Register InsertTool for MySymbol this.diagram1.Controller.RegisterTool(new InsertSymbolTool("InsertMySymbol", typeof(MySymbol))); //Activate InsertTool for MySymbol this.diagram1.ActivateTool("InsertMySymbol");
[VB.NET]
'Register InsertTool for MySymbol Me.diagram1.Controller.RegisterTool(New InsertSymbolTool("InsertMySymbol", GetType(MySymbol))) 'Activate InsertTool for MySymbol Me.diagram1.ActivateTool("InsertMySymbol")
Conclusion
I hope you enjoyed learning about how to create a custom symbol.
You can refer to the WinForms Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!