예제 #1
0
        /// <summary>
        /// Applies the settings from the custom attributes to the control.
        /// </summary>
        /// <param name="ctrl">A control bound to property</param>
        /// <param name="attributes">Custom attributes for the property</param>
        /// <returns></returns>
        private Control ApplyAttributes(Control ctrl, object[] attributes)
        {
            ControlTag tag = (ControlTag)ctrl.Tag;
            NumericSettingsAttribute attrRange    = null;
            DisplaySettingsAttribute attrDisplay  = null;
            RequiredFieldAttribute   attrRequired = null;

            foreach (object attribute in attributes)
            {
                if (attribute is NumericSettingsAttribute)
                {
                    attrRange = (NumericSettingsAttribute)attribute;
                }
                else if (attribute is DisplaySettingsAttribute)
                {
                    attrDisplay = (DisplaySettingsAttribute)attribute;
                }
                else if (attribute is RequiredFieldAttribute)
                {
                    attrRequired = (RequiredFieldAttribute)attribute;
                }
            }

            // Attach LostFocus handler for input validation
            ctrl.LostFocus += ctrl_LostFocus;

            // Range Attribute
            if (attrRange != null)
            {
                // todo
            }

            // Display Attribute
            if (attrDisplay != null)
            {
                tag.CustomLabel = attrDisplay.Label;
                ctrl.Enabled    = !attrDisplay.ReadOnly;
                ctrl.Visible    = attrDisplay.Visible;
                if (attrDisplay.Width > 0)
                {
                    ctrl.Width = attrDisplay.Width;
                }
            }

            // Required Field Attribute
            if (attrRequired != null)
            {
                if (string.IsNullOrEmpty(attrRequired.Message))
                {
                    tag.ErrorMessage = "Required";
                }
                else
                {
                    tag.ErrorMessage = attrRequired.Message;
                }
            }
            return(ctrl);
        }
예제 #2
0
        private void InitializeControls(object item)
        {
            PropertyInfo[] properties = item.GetType().GetProperties();

            // Create layout table
            this.tableLayoutPanel1.RowCount = properties.Length;

            // For each property
            int rowNumber = 0;

            foreach (var property in properties)
            {
                Control ctrl = ControlFactory.CreateControl(item, property);
                if (ctrl != null)
                {
                    // Get custom attributes
                    object[] attributes = property.GetCustomAttributes(true);
                    ctrl = ApplyAttributes(ctrl, attributes);

                    // Disable the control if property read only
                    if (!property.CanWrite)
                    {
                        ctrl.Enabled = false;
                    }

                    // Set the tab index
                    //ctrl.TabIndex = controls.Count + 1;

                    // Build label
                    if (ctrl.Visible)
                    {
                        ControlTag tag   = (ControlTag)ctrl.Tag;
                        Label      label = ControlFactory.CreateLabel(property.Name);
                        if (!string.IsNullOrEmpty(tag.CustomLabel))
                        {
                            label.Text = tag.CustomLabel;
                        }
                        tableLayoutPanel1.Controls.Add(label, 0, rowNumber);
                        tableLayoutPanel1.Controls.Add(ctrl, 1, rowNumber);
                        controls.Add(ctrl);
                    }
                }
                rowNumber++;
            }

            // Resize the form
            this.Width  = tableLayoutPanel1.Width + 40;
            this.Height = tableLayoutPanel1.Height + 90;
        }
예제 #3
0
 /// <summary>
 /// Saves the controls value back to the data object.
 /// </summary>
 private void SaveControlValues()
 {
     // For each TextBox, Dropdown etc...
     foreach (Control c in controls)
     {
         ControlTag   tag      = (ControlTag)c.Tag;
         PropertyInfo property = DataItem.GetType().GetProperty(tag.PropertyName);
         Type         type     = property.PropertyType;
         if (c is TextBox)
         {
             TextBox textbox = (TextBox)c;
             if (type == typeof(string))
             {
                 property.SetValue(DataItem, textbox.Text, null);
             }
             else if (type == typeof(char))
             {
                 property.SetValue(DataItem, Convert.ToChar(textbox.Text), null);
             }
         }
         else if (c is NumericUpDown)
         {
             NumericUpDown numeric = (NumericUpDown)c;
             if (type == typeof(int))
             {
                 property.SetValue(DataItem, Convert.ToInt32(numeric.Value), null);
             }
             else if (type == typeof(decimal))
             {
                 property.SetValue(DataItem, Convert.ToDecimal(numeric.Value), null);
             }
         }
         else if (c is CheckBox)
         {
             CheckBox checkbox = c as CheckBox;
             property.SetValue(DataItem, checkbox.Checked, null);
         }
         else if (c is ComboBox)
         {
             ComboBox dropdown = c as ComboBox;
             property.SetValue(DataItem, Enum.Parse(tag.PropertyType, Convert.ToString(dropdown.SelectedItem)), null);
         }
         else if (c is FileBrowserButton)
         {
             FileBrowserButton button = c as FileBrowserButton;
             property.SetValue(DataItem, new FileInfo(button.FilePath.Text), null);
         }
     }
 }
예제 #4
0
        private bool ValidateControl(Control control)
        {
            bool isValid = true;

            // Validation currently limited to TextBoxes only
            TextBox txt = control as TextBox;

            if (txt != null)
            {
                // If the textbox is empty, show a warning
                ControlTag tag = (ControlTag)txt.Tag;
                if (tag.IsRequired && string.IsNullOrEmpty(txt.Text))
                {
                    errorProvider.SetError(txt, tag.ErrorMessage);
                    isValid = false;
                }
                else
                {
                    errorProvider.SetError(txt, string.Empty);
                }
            }
            return(isValid);
        }
예제 #5
0
        internal static Control CreateControl(object item, PropertyInfo property)
        {
            Control ctrl = null;
            Type    type = property.PropertyType;

            // The control depends on the property type
            if (type == typeof(string))
            {
                ctrl = new TextBox();
                TextBox textbox = ctrl as TextBox;
                textbox.Text   = (string)property.GetValue(item, null);
                textbox.Margin = new Padding(3, 3, 16, 0);
            }
            else if (type == typeof(char))
            {
                ctrl = new TextBox();
                TextBox textbox = ctrl as TextBox;
                textbox.MaxLength = 1;
                textbox.Width     = 20;
                textbox.Text      = Convert.ToString(property.GetValue(item, null));
                textbox.Margin    = new Padding(3, 3, 16, 0);
            }
            else if (type == typeof(int))
            {
                ctrl = new NumericUpDown();
                NumericUpDown numeric = ctrl as NumericUpDown;
                numeric.Value = Convert.ToDecimal(property.GetValue(item, null));
            }
            else if (type == typeof(decimal))
            {
                ctrl = new NumericUpDown();
                NumericUpDown numeric = ctrl as NumericUpDown;
                numeric.DecimalPlaces = 2;
                numeric.Value         = Convert.ToDecimal(property.GetValue(item, null));
            }
            else if (type == typeof(bool))
            {
                ctrl = new CheckBox();
                CheckBox checkbox = ctrl as CheckBox;
                checkbox.Checked = Convert.ToBoolean(property.GetValue(item, null));
            }
            else if (type.BaseType == typeof(Enum))
            {
                ctrl = new ComboBox();
                ComboBox dropdown = ctrl as ComboBox;
                dropdown.DropDownStyle = ComboBoxStyle.DropDownList;
                string[] names = Enum.GetNames(type);
                string   value = Convert.ToString(property.GetValue(item, null));
                foreach (var name in names)
                {
                    dropdown.Items.Add(name);
                    if (name == value)
                    {
                        dropdown.SelectedIndex = dropdown.Items.Count - 1;
                    }
                }
            }
            else if (type == typeof(DateTime))
            {
                ctrl = new DateTimePicker();
                DateTimePicker date      = ctrl as DateTimePicker;
                DateTime       dateValue = Convert.ToDateTime(property.GetValue(item, null));
                if (dateValue < date.MinDate)
                {
                    dateValue = date.MinDate;
                }
                if (dateValue > date.MaxDate)
                {
                    dateValue = date.MaxDate;
                }
                date.Value = dateValue;
            }
            else if (type == typeof(FileInfo))
            {
                ctrl = new FileBrowserButton();
                FileBrowserButton button = ctrl as FileBrowserButton;
                button.FilePath.Text = (string)property.GetValue(item, null);
            }


            if (ctrl != null)
            {
                ControlTag tag = new ControlTag();
                tag.PropertyName = property.Name;
                tag.PropertyType = property.PropertyType;
                ctrl.Tag         = tag;
            }
            return(ctrl);
        }