Example #1
0
        private void HookupEvents(Control control)
        {
            string propertyBinding = control.Tag as string;

            if (propertyBinding != null)
            {
                TextBox textBox = control as TextBox;
                if (textBox != null)
                {
                    textBox.LostFocus       += this.HandleTextBoxLostFocus;
                    textBox.ModifiedChanged += this.HandleTextBoxModifiedChanged;
                }
                else
                {
                    CheckBox checkBox = control as CheckBox;
                    if (checkBox != null)
                    {
                        checkBox.CheckStateChanged += this.HandleControlValidated;
                    }
                    else
                    {
                        ComboBox comboBox = control as ComboBox;
                        if (comboBox != null)
                        {
                            comboBox.SelectedValueChanged += this.HandleControlValidated;
                        }
                        else
                        {
                            FoldersSelector selector = control as FoldersSelector;
                            if (selector != null)
                            {
                                selector.FolderValidating += this.HandleControlValidating;
                                selector.FoldersChanged   += this.HandleControlValidated;
                            }
                        }
                    }
                }
            }

            foreach (Control child in control.Controls)
            {
                this.HookupEvents(child);
            }
        }
Example #2
0
        /// <summary>
        /// Called when a control is being validated. Set e.Cancel to true to cause the validation to fail.
        /// </summary>
        /// <param name="sender">The control being validated.</param>
        /// <param name="e">Parameters for the event.</param>
        private void HandleControlValidating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Control control      = (Control)sender;
            string  propertyName = (string)control.Tag;
            string  value        = control.Text;

            TextBox textBox = control as TextBox;

            if (textBox != null)
            {
                if (!textBox.Modified)
                {
                    return;
                }

                value = this.ParentPropertyPage.Normalize(propertyName, value);
            }
            else
            {
                FoldersSelector foldesSelector = control as FoldersSelector;
                if (foldesSelector != null)
                {
                    value = foldesSelector.TextBox.Text;
                }
            }

            string currentValue = this.ParentPropertyPage.GetProperty(propertyName);

            if (!String.Equals(value, currentValue, StringComparison.Ordinal))
            {
                try
                {
                    PropertyValidator.ValidateProperty(propertyName, value);
                }
                catch (ProjectPropertyArgumentException ex)
                {
                    if (textBox != null)
                    {
                        // Don't retrigger Validation when losing focus due to the message box.
                        textBox.Modified = false;
                    }

                    XHelperMethods.ShowErrorMessageBox(this.ParentPropertyPage.Site, ex.Message);
                    e.Cancel = true;

                    if (textBox != null)
                    {
                        textBox.Text     = currentValue;
                        textBox.Modified = false;

                        // Clear the page dirty flag if it was only this textbox that was modified.
                        int isDirty;
                        if (this.ParentPropertyPage.ProjectMgr.IsDirty(out isDirty) == 0 && isDirty == 0)
                        {
                            this.ParentPropertyPage.IsDirty = false;
                        }
                    }
                }
            }
            else if (textBox != null && textBox.Modified)
            {
                // The text wasn't significantly changed, but might need to be adjusted to the trimmed value.
                textBox.Text     = value;
                textBox.Modified = false;

                // Clear the page dirty flag if it was only this textbox that was modified.
                int isDirty;
                if (this.ParentPropertyPage.ProjectMgr.IsDirty(out isDirty) == 0 && isDirty == 0)
                {
                    this.ParentPropertyPage.IsDirty = false;
                }
            }
        }