using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.Design; using System.Drawing.Design; using System.Reflection; using System.Text; using System.Windows.Forms; namespace AlarmClockControlLibrary { public class BehaviorDesignerActionList : DesignerActionList { public BehaviorDesignerActionList(IComponent component) : base(component) { } public override DesignerActionItemCollection GetSortedActionItems() { if( this.Component == null ) return null; // Create list to store designer action items DesignerActionItemCollection actionItems = new DesignerActionItemCollection(); // Fill list of designer action items actionItems.Add(new DesignerActionHeaderItem("Behavior")); actionItems.Add( new DesignerActionTextItem( "Properties that affect how the AlarmClockControl behaves.", "Behavior")); // Add BackupAlarm designer action property item actionItems.Add( new DesignerActionPropertyItem( "BackupAlarm", "Backup Alarm", this.GetAttributeString(this.AlarmClockControl, "BackupAlarm", "Category"), this.GetAttributeString(this.AlarmClockControl, "BackupAlarm", "Description"))); // Add PrimaryAlarm designer action property item actionItems.Add( new DesignerActionPropertyItem( "PrimaryAlarm", "Primary Alarm", this.GetAttributeString(this.AlarmClockControl, "PrimaryAlarm", "Category"), this.GetAttributeString(this.AlarmClockControl, "PrimaryAlarm", "Description"))); // Return list of designer action items return actionItems; } // BackupAlarm proxy property public DateTime BackupAlarm { get { return this.AlarmClockControl.BackupAlarm; } set { SetProperty("BackupAlarm", value); } } // PrimaryAlarm proxy property public DateTime PrimaryAlarm { get { return this.AlarmClockControl.PrimaryAlarm; } set { SetProperty("PrimaryAlarm", value); } } // Helper method to safely set a component’s property private void SetProperty(string propertyName, object value) { // Get property PropertyDescriptor property = TypeDescriptor.GetProperties(this.AlarmClockControl)[propertyName]; // Set property value property.SetValue(this.AlarmClockControl, value); } // Helper property to acquire a AlarmClockControl reference private AlarmClockControl AlarmClockControl { get { return (AlarmClockControl)this.Component; } } // Helper method that returns the category string from a CategoryAttribute // assigned to a property exposed by the specified objecs private string GetAttributeString(object source, string sourceProperty, string attributeProperty) { PropertyInfo sourcePropertyInfo = source.GetType().GetProperty(sourceProperty); T attribute = (T)sourcePropertyInfo.GetCustomAttributes(typeof(T), false)[0]; if( attribute == null ) return null; Type attributeType = attribute.GetType(); PropertyInfo attributePropertyInfo = attributeType.GetProperty(attributeProperty); return (string)attributePropertyInfo.GetValue(attribute, null); } } }