public static void PopulateEntityProperty(Control control, PropertyInfo p, object entity, bool treatZeroAsNull) { bool isNullable = p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>); Type controlType = control.GetType(); string controlTypeName = controlType.FullName; string propertyTypeName = p.PropertyType.FullName; if (controlTypeName.Equals(typeof(TextBox).FullName)) { TextBox textBox = (TextBox)control; p.SetValue(entity, textBox.Text, null); } else if (controlTypeName.Equals(typeof(NumericTextBoxWindows).FullName)) { NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control; object value = DataHelperWindows.ChangeType(numericTextBox.Value, p.PropertyType); if (treatZeroAsNull && value != null && value.ToString() == "0") { value = null; } p.SetValue(entity, value, null); } else if (controlTypeName.Equals(typeof(CheckBox).FullName)) { CheckBox checkBox = (CheckBox)control; p.SetValue(entity, checkBox.Checked, null); } else if (controlTypeName.Equals(typeof(NumericUpDown).FullName)) { NumericUpDown numericUpDown = (NumericUpDown)control; object value = DataHelperWindows.ChangeType(numericUpDown.Value, p.PropertyType); if (treatZeroAsNull && value != null && value.ToString() == "0") { value = null; } p.SetValue(entity, value, null); } else if (controlTypeName.Equals(typeof(DateTimePicker).FullName)) { DateTimePicker dateTimePicker = (DateTimePicker)control; p.SetValue(entity, dateTimePicker.Value, null); } else if (controlTypeName.Equals(typeof(ComboBox).FullName)) { ComboBox comboBox = (ComboBox)control; Array enumValues = EnumHelperWindows.GetEnumValues(p.PropertyType); p.SetValue(entity, comboBox.SelectedItem, null); } else { throw new Exception(string.Format( "Unexpected controltype {0} to be used to populate property {1} on {2}.", controlTypeName, p.Name, entity.GetType().FullName)); } }
public static object GetControlValue(Control control, PropertyInfo p, bool treatZeroAsNull) { bool isNullable = p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>); Type controlType = control.GetType(); string controlTypeName = controlType.FullName; string propertyTypeName = p.PropertyType.FullName; if (controlTypeName.Equals(typeof(TextBox).FullName)) { TextBox textBox = (TextBox)control; return(textBox.Text); } else if (controlTypeName.Equals(typeof(NumericTextBoxWindows).FullName)) { NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control; object value = DataHelperWindows.ChangeType(numericTextBox.Value, p.PropertyType); if (treatZeroAsNull && value != null && value.ToString() == "0") { value = null; } return(value); } else if (controlTypeName.Equals(typeof(CheckBox).FullName)) { CheckBox checkBox = (CheckBox)control; return(checkBox.Checked); } else if (controlTypeName.Equals(typeof(NumericUpDown).FullName)) { NumericUpDown numericUpDown = (NumericUpDown)control; object value = DataHelperWindows.ChangeType(numericUpDown.Value, p.PropertyType); if (treatZeroAsNull && value != null && value.ToString() == "0") { value = null; } return(value); } else if (controlTypeName.Equals(typeof(DateTimePicker).FullName)) { DateTimePicker dateTimePicker = (DateTimePicker)control; return(dateTimePicker.Value); } else if (controlTypeName.Equals(typeof(ComboBox).FullName)) { ComboBox comboBox = (ComboBox)control; Array enumValues = EnumHelperWindows.GetEnumValues(p.PropertyType); return(comboBox.SelectedItem); } else { throw new Exception(string.Format( "Unexpected controltype {0} to be used to retrieve control value using property type {1}.", controlTypeName, p.Name)); } }
public static void PopulateControl(Control control, object value) { if (value == null) { return; } Type controlType = control.GetType(); string controlTypeName = controlType.FullName; if (controlTypeName.Equals(typeof(TextBox).FullName)) { TextBox textBox = (TextBox)control; textBox.Text = value.ToString(); } else if (controlTypeName.Equals(typeof(NumericTextBoxWindows).FullName)) { NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control; numericTextBox.Value = Convert.ToDouble(value); } else if (controlTypeName.Equals(typeof(CheckBox).FullName)) { CheckBox checkBox = (CheckBox)control; checkBox.Checked = Convert.ToBoolean(value); } else if (controlTypeName.Equals(typeof(NumericUpDown).FullName)) { NumericUpDown numericUpDown = (NumericUpDown)control; numericUpDown.Value = Convert.ToDecimal(value); } else if (controlTypeName.Equals(typeof(DateTimePicker).FullName)) { DateTimePicker dateTimePicker = (DateTimePicker)control; dateTimePicker.Value = Convert.ToDateTime(value); } else if (controlTypeName.Equals(typeof(ComboBox).FullName)) { ComboBox comboBox = (ComboBox)control; Array enumValues = EnumHelperWindows.GetEnumValues(value.GetType()); for (int i = 0; i < enumValues.Length; i++) //Populate the ComboBox with all the enum values. { object e = enumValues.GetValue(i); comboBox.Items.Add(e); } comboBox.SelectedItem = value; //Set the selected item based on the passed in value. } else { throw new Exception(string.Format( "Unexpected controltype {0} to populate with value {1}.", controlTypeName, value)); } }
public static Control GetInputControlForEntityProperty( string propertyName, Type entityType, DockStyle dockStyle, Color backColor) { Dictionary <string, Type> controlMappings = GetTypeNameToControlTypeMappings(); PropertyInfo p = entityType.GetProperty(propertyName); Type propertyType = null; if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = p.PropertyType.GetGenericArguments()[0]; } else { propertyType = p.PropertyType; } if (!controlMappings.ContainsKey(propertyType.FullName)) { throw new NullReferenceException(string.Format("No control mapped for property type 0.", propertyType.FullName)); } Type controlType = controlMappings[propertyType.FullName]; Control result = (Control)Activator.CreateInstance(controlType); result.Name = string.Format("{0}", p.Name); result.Tag = p.PropertyType; result.Dock = dockStyle; result.TabStop = true; if (!(result is TextBox) && !(result is NumericTextBoxWindows) && !(result is NumericUpDown) && !(result is DateTimePicker) && !(result is ComboBox)) { result.BackColor = backColor; } if (result is NumericUpDown) { ((NumericUpDown)result).Maximum = int.MaxValue; } if (result is NumericTextBoxWindows && (propertyType == typeof(Single)) || propertyType == typeof(Double) || propertyType == typeof(Decimal)) { NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)result; numericTextBox.AllowDecimal = true; } return(result); }
public static void ClearControls(Dictionary <string, Control> controls) { foreach (Control control in controls.Values) { if (control is Label) { continue; } if (!(control is Panel)) { throw new UserThrownException(string.Format("Expected control of type {0} and got control of type {1}.", typeof(Panel).FullName, control), LoggingLevel.Minimum); } Panel pnlInputControl = (Panel)control; Control inputControl = null; foreach (Control c in pnlInputControl.Controls) { if (!(c is LinkLabel)) { inputControl = c; break; } } if (inputControl is CheckBox) { CheckBox checkBox = (CheckBox)inputControl; checkBox.Checked = false; } else if (inputControl is NumericTextBoxWindows) { NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)inputControl; numericTextBox.Text = string.Empty; } else if (inputControl is TextBox) { TextBox textBox = (TextBox)inputControl; textBox.Text = string.Empty; } else if (inputControl is NumericUpDown) { NumericUpDown numericUpDown = (NumericUpDown)inputControl; numericUpDown.Value = 0; } else if (inputControl is DateTimePicker) { DateTimePicker dateTimePicker = (DateTimePicker)inputControl; dateTimePicker.Value = DateTime.Now; } else if (inputControl is ComboBox) { ComboBox comboBox = (ComboBox)inputControl; if (comboBox.Items.Count > 0) { //comboBox.Items.Clear(); comboBox.SelectedIndex = -1; } } else { //Do nothing } } }
public static Dictionary <string, Control> GetControlsForEntity( Type entityType, bool includeLabels, DockStyle dockStyle, Color backColor, List <string> hiddenProperties, List <string> unmanagedProperties, bool shapeColumnNames, List <LinkLabel> resetLinks, Color resetLinksBackColor) { if (resetLinks != null) { resetLinks.Clear(); } Dictionary <string, Control> controls = new Dictionary <string, Control>(); Dictionary <string, Type> controlMappings = GetTypeNameToControlTypeMappings(); PropertyInfo[] entityProperties = entityType.GetProperties(); for (int i = 0; i < entityProperties.Length; i++) { PropertyInfo p = entityProperties[i]; string propertyNameMatch = shapeColumnNames ? DataShaperWindows.ShapeCamelCaseString(p.Name) : p.Name; if (hiddenProperties.Contains(propertyNameMatch) || unmanagedProperties.Contains(propertyNameMatch)) { continue; } Type propertyType = null; if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = p.PropertyType.GetGenericArguments()[0]; } else { propertyType = p.PropertyType; } if (!controlMappings.ContainsKey(propertyType.FullName)) { continue; } Type controlType = controlMappings[propertyType.FullName]; Control control = (Control)Activator.CreateInstance(controlType); control.Name = string.Format("{0}", p.Name); control.Tag = p.PropertyType; control.Dock = DockStyle.Fill; control.TabStop = true; //control.TabIndex = i; //control.Height = 40; Panel pnlInputControl = new Panel() { Name = string.Format("pnl{0}", p.Name), Dock = dockStyle, Height = control.Height }; pnlInputControl.Controls.Add(control); if (resetLinks != null) { LinkLabel lnkReset = new LinkLabel() { Name = string.Format("lnk{0}", p.Name), Text = "Reset", Tag = control, Dock = DockStyle.Right, Width = 60, BackColor = resetLinksBackColor }; pnlInputControl.Controls.Add(lnkReset); resetLinks.Add(lnkReset); } if (!(control is TextBox) && !(control is NumericTextBoxWindows) && !(control is NumericUpDown) && !(control is DateTimePicker) && !(control is ComboBox)) { control.BackColor = backColor; } if (control is NumericUpDown) { ((NumericUpDown)control).Maximum = int.MaxValue; } if (control is NumericTextBoxWindows && (propertyType == typeof(Single)) || propertyType == typeof(Double) || propertyType == typeof(Decimal)) { NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control; numericTextBox.AllowDecimal = true; } if (includeLabels) { Label label = new Label() { Name = string.Format("lbl{0}", p.Name), Text = string.Format("{0}:", DataShaperWindows.ShapeCamelCaseString(p.Name)), Dock = dockStyle, BackColor = backColor, //Height = 40, }; controls.Add(label.Name, label); } controls.Add(pnlInputControl.Name, pnlInputControl); } return(DataHelperWindows.ReverseDictionaryOrder <string, Control>(controls)); }