ComponentOne Expression Editor for WPF
Working with Expression Editor / Create Custom Function
In This Topic
    Create Custom Function
    In This Topic

    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 Partial Class MainWindow
        Inherits Window
    
        Public Sub New()
            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()
                Errors.Text = ""
            End If
        End Sub
    End Class
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ExpressionPanel.ExpressionEditor = ExpressionEditor;
            ExpressionItem factItem = new ExpressionItem("Factorial", "Factorial()",
            "Returns the product of an integer and all the integers below it.",
            ItemType.MathFuncs);
            factItem.Arguments.Add(new Argument("number", typeof(System.Double)));
            ExpressionEditor.AddFunction(factItem, Factorial, 1, 1);
        }
    
        private object Factorial(List<object> list)
        {
            List<object> items = new List<object>();
            items = items.Concat(list).ToList();
    
            int i, fact = 1, number;
            number = Convert.ToInt32(items[0]);
            for (i = 1; i <= number; i++)
            {
                fact = fact * i;
            }
    
            return fact;
        }
    
        private void ExpressionEditor_ExpressionChanged(object sender, EventArgs e)
        {
            if (!ExpressionEditor.IsValid)
            {
                Result.Text = "";
            }
            else
            {
                Result.Text = ExpressionEditor.Evaluate()?.ToString();
                Errors.Text = "";
            }
        }
    }