Example #1
0
 public CalculationDynamicForm(CalculationSettings sender, int type, DynamicConstant editItem)
 {
     _sender = sender;
     _type   = type;
     if (_type == 2)
     {
         _editModel = editItem;
     }
     InitializeComponent();
     LoadLists();
     tb_name.DataBindings.Add("Text", _editModel, "Name");
     tb_express.DataBindings.Add("Text", _editModel, "Expression");
 }
Example #2
0
        private void CalculateDynamic(DynamicConstant dynamic)
        {
            Dictionary <string, string> replace = new Dictionary <string, string>();
            string constantPattern = @"constant\[\d+\]";

            foreach (Match match in Regex.Matches(dynamic.Expression, constantPattern))
            {
                string pattern    = @"\d+";
                var    constantId = Convert.ToInt32(Regex.Match(match.Value, pattern).ToString());

                var constantValue = _usedConstatns.FirstOrDefault(x => x.ID == constantId);
                if (constantValue != null)
                {
                    if (!replace.ContainsKey(match.Value))
                    {
                        replace.Add(match.Value, constantValue.Value.ToString());
                    }
                }
            }

            string sumPattern = @"sum\[\d+\]";

            foreach (Match match in Regex.Matches(dynamic.Expression, sumPattern))
            {
                string pattern = @"\d+";

                var itemId   = Convert.ToInt32(Regex.Match(match.Value, pattern).ToString());
                var sumValue = ItemSumList.FirstOrDefault(x => x.Item.ID == itemId);
                if (sumValue != null)
                {
                    if (!replace.ContainsKey(match.Value))
                    {
                        replace.Add(match.Value, sumValue.Value.ToString());
                    }
                }
            }



            string dynamicPattern = @"dynamic\[\d+\]";

            foreach (Match match in Regex.Matches(dynamic.Expression, dynamicPattern))
            {
                string pattern = @"\d+";

                var dynamicId    = Convert.ToInt32(Regex.Match(match.Value, pattern).ToString());
                var dynamicValue = CalculatedDynamics.FirstOrDefault(x => x.Dynamic.ID == dynamicId);
                if (dynamicValue != null)
                {
                    if (!replace.ContainsKey(match.Value))
                    {
                        replace.Add(match.Value, dynamicValue.Value.ToString());
                    }
                }
            }

            var ret = dynamic.Expression;

            foreach (var i in replace)
            {
                ret = ret.Replace(i.Key, i.Value);
            }

            var value = CalculateValue(ret);

            CalculatedDynamics.Add(new CalculatedDynamic
            {
                Dynamic = dynamic, Value = value
            });
        }