00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.ComponentModel;
00014 using System.Drawing;
00015 using System.Reflection;
00016 using System.Windows.Forms;
00017
00018 namespace Mcs.Epm.MicrosoftProject.mpFx.WinForms
00019 {
00020
00021
00022
00023 public partial class PropertyEditorForm : Form
00024 {
00025 #region Public Delegates
00026
00027 public delegate void OnItemEditEventHandler(object sender, DataGridViewRow row, out object value);
00028
00029 #endregion
00030
00031 #region Public Events
00032
00033 public event OnItemEditEventHandler OnItemEdit;
00034
00035 #endregion
00036
00037 #region Constants
00038
00039 public const int PROPERTY_NAME_COLUMN_INDEX = 0;
00040 public const int PROPERTY_DESCRIPTION_COLUMN_INDEX = 0;
00041 public const int PROPERTY_VALUE_COLUMN_INDEX = 2;
00042
00043 #endregion
00044
00045 #region Instance Data
00046
00047 private readonly object _ObjectWithProperties;
00048
00049 private readonly PropertyInfo[] _PropertyInfo;
00050
00051 #endregion
00052
00053 #region Constructor
00054
00055 public PropertyEditorForm(object objectWithProperties)
00056 {
00057 InitializeComponent();
00058
00059 _ObjectWithProperties = objectWithProperties;
00060
00061 Type type = _ObjectWithProperties.GetType();
00062
00063 _PropertyInfo = type.GetProperties();
00064
00065 foreach (PropertyInfo property in _PropertyInfo)
00066 {
00067 object[] attributes = property.GetCustomAttributes(false);
00068
00069 string description = string.Empty;
00070
00071 foreach (object attribute in attributes)
00072 {
00073 DescriptionAttribute descriptionAttribute = attribute as DescriptionAttribute;
00074
00075 if (descriptionAttribute != null)
00076 {
00077 description = descriptionAttribute.Description;
00078 }
00079 }
00080
00081 object returnValue = property.GetValue(_ObjectWithProperties, null) ?? string.Empty;
00082
00083 propertiesGridView.Rows.Add(new object[] { property.Name, description, returnValue.ToString() });
00084
00085 if (!property.CanWrite)
00086 {
00087 propertiesGridView.Rows[propertiesGridView.Rows.Count - 1].ReadOnly = true;
00088 propertiesGridView.Rows[propertiesGridView.Rows.Count - 1].DefaultCellStyle.BackColor = Color.SlateGray;
00089 }
00090 }
00091 }
00092
00093 #endregion
00094
00095 #region Public Properties
00096
00097 public object Object
00098 {
00099 get { return _ObjectWithProperties; }
00100 }
00101
00102 #endregion
00103
00104 #region Private Methods
00105
00106 private void DoOnItemEdit(object sender, DataGridViewRow row)
00107 {
00108 if (OnItemEdit != null)
00109 {
00110 object value;
00111
00112 OnItemEdit(sender, row, out value);
00113
00114 if (value != null)
00115 {
00116 propertiesGridView.CancelEdit();
00117 row.Cells[PROPERTY_VALUE_COLUMN_INDEX].Value = value;
00118 }
00119 }
00120 }
00121
00122 #endregion
00123
00124 #region Event Handlers
00125
00126 private void okButton_Click(object sender, EventArgs e)
00127 {
00128 foreach (DataGridViewRow row in propertiesGridView.Rows)
00129 {
00130 string propertyName = (string) row.Cells[PROPERTY_NAME_COLUMN_INDEX].Value;
00131
00132 foreach (PropertyInfo propertyInfo in _PropertyInfo)
00133 {
00134 if (propertyInfo.Name == propertyName && propertyInfo.CanWrite)
00135 {
00136 object value = TypeDescriptor.GetConverter(propertyInfo.PropertyType).ConvertFrom(row.Cells[PROPERTY_VALUE_COLUMN_INDEX].Value);
00137 propertyInfo.SetValue(_ObjectWithProperties, value, null);
00138 break;
00139 }
00140 }
00141 }
00142 }
00143
00144 private void propertiesGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
00145 {
00146 DoOnItemEdit(sender, propertiesGridView.Rows[e.RowIndex]);
00147 }
00148
00149 #endregion
00150 }
00151 }