и cнова EventTab
От: nauro Украина  
Дата: 31.03.05 08:14
Оценка:
сделал я свой таб в PropertyGrid следующим образом.
есть 3 класа которые мне в этом помогли:

public class EventPropertyDescriptor : PropertyDescriptor 
    {
        #region Fields
        EventDescriptor  FEventDescriptor;
        string FValue = string.Empty;
        #endregion
        public EventPropertyDescriptor( EventDescriptor  eventDescriptor): base(eventDescriptor)
        {
            FEventDescriptor = eventDescriptor;
            
        }
        
        #region  Override PropertyDescriptor
        public override object GetValue(object component)
        {
            //TODO : GetValue(object component)
            
            return "";//"hello world";
        }
        public override bool CanResetValue(object component)
        {
            //TODO : CanResetValue(object component)
            return false;
        }
        public override bool ShouldSerializeValue(object component)
        {
            //TODO : ShouldSerializeValue(object component)
            return true;
        }
        public override Type PropertyType
        {
            get
            {
                return FEventDescriptor.EventType;
            }
        }

        public override Type ComponentType
        {
            get
            {                
                return FEventDescriptor.ComponentType;
            }
        }

        public override void SetValue(object component, object value)
        {
            FValue = (string)value;
        }
        public override void ResetValue(object component)
        {
            
        }
        public override bool IsReadOnly
        {
            get
            {
                //TODO : IsReadOnly
                return false;
            }
        }
        #endregion
    }
}
    public class EventTab : PropertyTab
    {
        [Browsable(true)]
        public EventTab()
        {            
        }
        // Returns the properties of the specified component extended with 
        // a CategoryAttribute reflecting the name of the type of the property.
        public override System.ComponentModel.PropertyDescriptorCollection GetProperties(object component, System.Attribute[] attributes)
        {
            EventDescriptorCollection events;
            if( attributes == null )
                events = TypeDescriptor.GetEvents(component) ;            
            else
                events = TypeDescriptor.GetEvents(component, attributes);    

         
            
            PropertyDescriptor[] propArray = new PropertyDescriptor[events.Count];            
            for(int i=0; i<events.Count; i++)           
            {               
                
                // Create a new PropertyDescriptor from the old one, with 
                // a CategoryAttribute matching the name of the type.
                PropertyDescriptor pd =     new EventPropertyDescriptor(events[i]);
                
                propArray[i] = pd;
            }
            return new PropertyDescriptorCollection( propArray );
        }

        public override System.ComponentModel.PropertyDescriptorCollection GetProperties(object component)
        {                     
            return this.GetProperties(component, null);
        }

        // Provides the name for the property tab.
        public override string TabName
        {
            get
            {
                return "Events";
            }
        }
        // Provides an image for the property tab.
        public override System.Drawing.Bitmap Bitmap
        {
            get
            {
                Assembly thisAssembly = Assembly.GetExecutingAssembly();
                string nameStream =  thisAssembly.GetName().Name+".event.bmp";
                System.IO.Stream stream = thisAssembly.GetManifestResourceStream(nameStream);
                Bitmap bmp = new Bitmap(stream);
                return bmp;
            }
        }
    }
}
[PropertyTabAttribute(typeof(EventTab), PropertyTabScope.Component)]
    public class WrapperComponent : System.ComponentModel.Component ,ICustomTypeDescriptor
    {
        private System.ComponentModel.Component FComponent;
        public StringCollection eventsMethod;
        public System.ComponentModel.Component Component
        {
            get { return FComponent; }
        }

        
        
        public WrapperComponent(System.ComponentModel.Component component)
        {
            FComponent = component;
            eventsMethod = new StringCollection();
        }
        #region ICustomTypeDescriptor Members

        public TypeConverter GetConverter()
        {    
            return TypeDescriptor.GetConverter(FComponent);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(FComponent,attributes);;
        }

        EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()
        {
            return TypeDescriptor.GetEvents(FComponent);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(FComponent);;
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return FComponent;
        }

        public AttributeCollection GetAttributes()
        {
            Attribute [] arr = new Attribute[1];
            arr[0]= new PropertyTabAttribute(typeof(EventTab), PropertyTabScope.Component);
            AttributeCollection coll = new AttributeCollection(arr);
            return coll;
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(FComponent,attributes);;
        }

        PropertyDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetProperties()
        {
            return TypeDescriptor.GetProperties(FComponent);
        }

        public object GetEditor(Type editorBaseType)
        {            
            return TypeDescriptor.GetEditor(FComponent,editorBaseType);;
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(FComponent);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(FComponent);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(FComponent);;
        }

        #endregion
    }

/******************а вот пример использования*/
propertyGrid.SelectedObject = new WrapperComponent(selectedComponent);


Вот, но во-первых, я немогу ничего редактировать в EventTab-е. Во вторых я немогу узнать какой event я сейчас редактирую...
-----------------------------------------
тут может быть ваша реклама
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.