Articles in this section
Category / Section

How to add a couple of properties in the symbol model preferable in the designer?

2 mins read

The Essential WinForms Diagram SymbolModel class implements the Diagram.IPropertyContainer interface and to add properties that will be confined to programmatic access you can use the IPropertyContainer.SetPropertyValue(String propertyname, object propertyvalue) method to add the required properties anytime after a SymbolModel instance is created by the SymbolDesigner. The SymbolDesigner creates a new SymbolModel in response to the 'Add Symbol' command from within the 'MainForm.SymbolAdd_Click()' event handler found in the SymbolDesigner project.

// From the SymbolDesigner’s MainForm.SymbolAdd_Click() handler

C#

private void SymbolAdd_Click(object sender, System.EventArgs e)
  {
   if(curPalette != null)
   {
    SymbolModel symbolMdl = curPalette.AddSymbol("New Symbol");
    symbolMdl.SetPropertyValue("Custom Property", "Test Value");
curPalette.AppendChild(symbolMdl);
    SymbolDocument formSymbolDoc = new SymbolDocument(symbolMdl);
    //...
   }
  }
 

 

VB

Private Sub SymbolAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   If Not curPalette Is Nothing Then
    Dim symbolMdl As SymbolModel = curPalette.AddSymbol("New Symbol")
    symbolMdl.SetPropertyValue("Custom Property", "Test Value")
curPalette.AppendChild(symbolMdl)
    Dim formSymbolDoc As SymbolDocument = New SymbolDocument(symbolMdl)
    '...
   End If
End Sub
 

This property will be persisted along with the SymbolModel and you can subsequently use the SymbolModel’s IPropertyContainer.GetPropertyValue(String propertyname) method to retrieve this property from the SymbolModel when creating the Symbol.

However to add properties to the SymbolModel that can be edited through the SymbolDesigner's property editor, you will have to first implement a subclass of the Diagram.SymbolModel type that defines the extra properties that you require. This custom SymbolModel should be implemented as a serializable class that includes the 'Serializable' attribute and implements the requisite serialization constructor and GetObjectData(SerializationInfo, StreamingContext) method. Now within the SymbolDesigner’s 'Add Symbol' event handler, in place of the default SymbolModel.AddSymbol() method, create an instance of your custom SymbolModel class, and append this custom symbol model instance to the SymbolPalette using the SymbolPalette.AppendChild(INode mysymbolmodel) method. Also initialize the SymbolDocument that you will be creating with this new instance of the SymbolModel. The following code shows the revised handler,

C#

private void SymbolAdd_Click(object sender, System.EventArgs e)
  {
   Cursor prevCursor = Cursor.Current;
   Cursor.Current = Cursors.WaitCursor;
   SymbolPalette curPalette = symbolPaletteGroupBar.CurrentPalette;
   if (curPalette != null)
   {
    CustomSymbolModel custsymbolmdl = new CustomSymbolModel();
curPalette.AppendChild(custsymbolmdl);
    SymbolDocument formSymbolDoc = new SymbolDocument(custsymbolmdl);
    formSymbolDoc.MdiParent = this;
    this.propertyEditor.Diagram = formSymbolDoc.Diagram;
    formSymbolDoc.Show();
   }
   Cursor.Current = prevCursor;
  }
 

 

VB

Private Sub SymbolAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   Dim prevCursor As Cursor = Cursor.Current
   Cursor.Current = Cursors.WaitCursor
   Dim curPalette As SymbolPalette = symbolPaletteGroupBar.CurrentPalette
   If Not curPalette Is Nothing Then
    Dim custsymbolmdl As CustomSymbolModel = New CustomSymbolModel()
curPalette.AppendChild(custsymbolmdl)
    Dim formSymbolDoc As SymbolDocument = New SymbolDocument(custsymbolmdl)
    formSymbolDoc.MdiParent = Me
    Me.propertyEditor.Diagram = formSymbolDoc.Diagram
    formSymbolDoc.Show()
   End If
   Cursor.Current = prevCursor
End Sub
 

The new properties for the SymbolModel will be available from the symbol model properties window, and may be accessed later on when using the SymbolModel to create a Symbol.

Please note that the subclassed SymbolModel type will have to be defined in a separate class library assembly, and both the SymbolDesigner and your application should link to this dll.


Conclusion

I hope you enjoyed learning about how to add a couple of properties in the symbol model preferable in the designer.

You can refer to our WinForms Diagram feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Diagram documentation to understand how to present and manipulate data. 

For current customers, you can check out our WinForms 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 WinForms Diagram and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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