private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDestinationField.Text))
            {
                MsgBox.ShowError(DashboardSharedStrings.EXPRESSION_ASSIGN_DESTINATION_FIELD_BLANK);
                this.DialogResult = DialogResult.None;
                return;
            }

            if (!editMode)
            {
                foreach (string s in dashboardHelper.GetFieldsAsList())
                {
                    if (txtDestinationField.Text.ToLowerInvariant().Equals(s.ToLowerInvariant()))
                    {
                        MsgBox.ShowError(DashboardSharedStrings.EXPRESSION_ASSIGN_DESTINATION_FIELD_ALREADY_EXISTS);
                        this.DialogResult = DialogResult.None;
                        return;
                    }
                }

                foreach (IDashboardRule rule in dashboardHelper.Rules)
                {
                    if (rule is DataAssignmentRule)
                    {
                        DataAssignmentRule assignmentRule = rule as DataAssignmentRule;
                        if (txtDestinationField.Text.ToLowerInvariant().Equals(assignmentRule.DestinationColumnName.ToLowerInvariant()))
                        {
                            MsgBox.ShowError(DashboardSharedStrings.EXPRESSION_ASSIGN_DESTINATION_FIELD_ALREADY_EXISTS_AS_RECODED);
                            this.DialogResult = DialogResult.None;
                            return;
                        }
                    }
                }
            }

            if (cbxDataType.Text.Equals("Text"))
            {
                AssignRule = new Rule_ExpressionAssign(this.dashboardHelper, "Assign " + txtDestinationField.Text + " the expression: " + txtExpression.Text, txtDestinationField.Text, "System.String", txtExpression.Text);
            }
            else if (cbxDataType.Text.Equals("Date/Time"))
            {
                AssignRule = new Rule_ExpressionAssign(this.dashboardHelper, "Assign " + txtDestinationField.Text + " the expression: " + txtExpression.Text, txtDestinationField.Text, "System.DateTime", txtExpression.Text);
            }
            else
            {
                AssignRule = new Rule_ExpressionAssign(this.dashboardHelper, "Assign " + txtDestinationField.Text + " the expression: " + txtExpression.Text, txtDestinationField.Text, txtExpression.Text);
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        /// <summary>
        /// Constructor used for editing an existing format rule
        /// </summary>
        /// <param name="dashboardHelper">The dashboard helper to attach</param>
        public ExpressionAssignDialog(DashboardHelper dashboardHelper, Rule_ExpressionAssign assignRule)
        {
            InEditMode           = true;
            this.dashboardHelper = dashboardHelper;
            this.AssignRule      = assignRule;
            InitializeComponent();
            FillSelectionComboboxes();

            this.txtDestinationField.Text    = AssignRule.DestinationColumnName;
            this.txtExpression.Text          = AssignRule.Expression;
            this.txtDestinationField.Enabled = false;

            if (assignRule.DestinationColumnType.Equals("System.String"))
            {
                cbxDataType.SelectedIndex = 1;
            }
            else
            {
                cbxDataType.SelectedIndex = 0;
            }
        }
        private void btnEditRule_Click(object sender, RoutedEventArgs e)
        {
            if (lbxRules.SelectedItems != null && lbxRules.SelectedItems.Count == 1)
            {
                Rule_Recode            recodeRule            = null;
                Rule_Format            formatRule            = null;
                Rule_ExpressionAssign  expressionAssignRule  = null;
                Rule_SimpleAssign      simpleAssignRule      = null;
                Rule_ConditionalAssign conditionalAssignRule = null;
                Rule_VariableGroup     variableGroupRule     = null;

                foreach (IDashboardRule rule in dashboardHelper.Rules)
                {
                    if (rule.FriendlyRule.Equals(lbxRules.SelectedItem.ToString()))
                    {
                        if (rule is Rule_Recode)
                        {
                            recodeRule = rule as Rule_Recode;
                            break;
                        }
                        else if (rule is Rule_Format)
                        {
                            formatRule = rule as Rule_Format;
                            break;
                        }
                        else if (rule is Rule_ExpressionAssign)
                        {
                            expressionAssignRule = rule as Rule_ExpressionAssign;
                            break;
                        }
                        else if (rule is Rule_SimpleAssign)
                        {
                            simpleAssignRule = rule as Rule_SimpleAssign;
                            break;
                        }
                        else if (rule is Rule_ConditionalAssign)
                        {
                            conditionalAssignRule = rule as Rule_ConditionalAssign;
                            break;
                        }
                        else if (rule is Rule_VariableGroup)
                        {
                            variableGroupRule = rule as Rule_VariableGroup;
                            break;
                        }
                    }
                }

                System.Windows.Forms.DialogResult result = System.Windows.Forms.DialogResult.None;

                if (recodeRule != null)
                {
                    EpiDashboard.Dialogs.RecodeDialog recodeDialog = new EpiDashboard.Dialogs.RecodeDialog(this.dashboardHelper, recodeRule);
                    result = recodeDialog.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        dashboardHelper.UpdateRule(recodeRule, recodeDialog.RecodeRule);
                        UpdateRules();
                        if (UserVariableChanged != null)
                        {
                            UserVariableChanged(this, new EventArgs());
                        }
                    }
                }
                else if (formatRule != null)
                {
                    EpiDashboard.Dialogs.FormatDialog formatDialog = new EpiDashboard.Dialogs.FormatDialog(this.dashboardHelper, formatRule);
                    result = formatDialog.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        dashboardHelper.UpdateRule(formatRule, formatDialog.FormatRule);
                        UpdateRules();
                        if (UserVariableChanged != null)
                        {
                            UserVariableChanged(this, new EventArgs());
                        }
                    }
                }
                else if (expressionAssignRule != null)
                {
                    EpiDashboard.Dialogs.ExpressionAssignDialog assignDialog = new EpiDashboard.Dialogs.ExpressionAssignDialog(this.dashboardHelper, expressionAssignRule);
                    result = assignDialog.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        dashboardHelper.UpdateRule(expressionAssignRule, assignDialog.AssignRule);
                        UpdateRules();
                        if (UserVariableChanged != null)
                        {
                            UserVariableChanged(this, new EventArgs());
                        }
                    }
                }
                else if (simpleAssignRule != null)
                {
                    EpiDashboard.Dialogs.SimpleAssignDialog assignDialog = new EpiDashboard.Dialogs.SimpleAssignDialog(this.dashboardHelper, simpleAssignRule);
                    result = assignDialog.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        dashboardHelper.UpdateRule(simpleAssignRule, assignDialog.AssignRule);
                        UpdateRules();
                        if (UserVariableChanged != null)
                        {
                            UserVariableChanged(this, new EventArgs());
                        }
                    }
                }
                else if (conditionalAssignRule != null)
                {
                    EpiDashboard.Dialogs.ConditionalAssignDialog assignDialog = new EpiDashboard.Dialogs.ConditionalAssignDialog(this.dashboardHelper, conditionalAssignRule);
                    result = assignDialog.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        dashboardHelper.UpdateRule(conditionalAssignRule, assignDialog.AssignRule);
                        UpdateRules();
                        if (UserVariableChanged != null)
                        {
                            UserVariableChanged(this, new EventArgs());
                        }
                    }
                }
                else if (variableGroupRule != null)
                {
                    EpiDashboard.Dialogs.CreateGroupDialog groupDialog = new EpiDashboard.Dialogs.CreateGroupDialog(this.dashboardHelper, variableGroupRule);
                    result = groupDialog.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        dashboardHelper.UpdateRule(variableGroupRule, groupDialog.Group);
                        UpdateRules();
                        if (UserVariableChanged != null)
                        {
                            UserVariableChanged(this, new EventArgs());
                        }
                    }
                }
            }
        }