コード例 #1
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);
         }
     }
 }
コード例 #2
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);
        }