Working with Expression Editor / Create Custom Function
Create Custom Function

Expression Editor provides various built-in functions to create expressions for your applications. Furthermore, it allows you to define custom functions according to your application’s requirement. The AddFunction method of C1ExpressionEditor class allows you to add custom functions to Expression Editor. This custom function gets added to the ExpressionEditor engine and is accessible at runtime by C1ExpreesionEditor for performing calculations.

The following image shows a custom function used in the ExpressionEditor control.

The following code demonstrates how a custom function is created and added to the Expression Editor panel:

Public NotInheritable Partial Class MainPage
    Inherits Page

    Public Sub New()
        Me.InitializeComponent()
        ExpressionPanel.ExpressionEditor = ExpressionEditor
        Dim factItem As ExpressionItem = New ExpressionItem("Factorial", "Factorial()",
        "Returns the product of an integer and all the integers below it.",
        ItemType.MathFuncs)
        factItem.Arguments.Add(New Argument("number", GetType(System.Double)))
        ExpressionEditor.AddFunction(factItem, AddressOf Factorial, 1, 1)
    End Sub

    Private Function Factorial(ByVal list As List(Of Object)) As Object
        Dim items As List(Of Object) = New List(Of Object)()
        items = items.Concat(list).ToList()
        Dim i, number As Integer, fact As Integer = 1
        number = Convert.ToInt32(items(0))

        For i = 1 To number
            fact = fact * i
        Next

        Return fact
    End Function

    Private Sub ExpressionEditor_ExpressionChanged(ByVal sender As Object, ByVal e As EventArgs)
        If Not ExpressionEditor.IsValid Then
            Result.Text = ""
        Else
            Result.Text = ExpressionEditor.Evaluate()?.ToString()
        End If
    End Sub
End Class