и 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 я сейчас редактирую...
-----------------------------------------
тут может быть ваша реклама
Re: и cнова EventTab
От: nauro Украина  
Дата: 31.03.05 10:26
Оценка:
Если кто не видел вопроса в конце поста повторюсь.

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

-----------------------------------------
тут может быть ваша реклама
Re[2]: и cнова EventTab
От: Аноним  
Дата: 29.09.05 14:20
Оценка:
Здравствуйте, nauro, Вы писали:

N>

N>Вот, но во-первых, я немогу ничего редактировать в EventTab-е. Во вторых я немогу узнать какой event я сейчас редактирую...


ап
Re: и cнова EventTab
От: andreich78 Россия  
Дата: 30.09.05 07:29
Оценка:
Здравствуйте, nauro, Вы писали:

N>сделал я свой таб в PropertyGrid следующим образом.

N>есть 3 класа которые мне в этом помогли:

N>

N>


N>Вот, но во-первых, я немогу ничего редактировать в EventTab-е. Во вторых я немогу узнать какой event я сейчас редактирую...

где пытаешься?
run-time? design-time(IDE)?
Re: и cнова EventTab
От: Zegee Украина  
Дата: 22.11.05 15:49
Оценка:
Здравствуйте, nauro, Вы писали:

N>сделал я свой таб в PropertyGrid следующим образом.

N>есть 3 класа которые мне в этом помогли: ...

N>Вот, но во-первых, я немогу ничего редактировать в EventTab-е. Во вторых я немогу узнать какой event я сейчас редактирую...


Не знаю актуален ответ или нет. Надо переопределить PropertyDescriptor.Converter в классе EventPropertyDescriptor

например

public override TypeConverter Converter
{
get
{
if (converter == null)
{
converter = new EventConverter(evDesc);
}
return converter;
}
}

Пример самого конвертера (этот класс используется студией)

private class EventConverter : TypeConverter
{

private EventDescriptor evt;

public EventConverter(EventDescriptor evt)
{
this.evt = evt;
}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value != null)
{
if (!(value is string))
{
return base.ConvertFrom(context, culture, value);
}
if (((string) value).Length == 0)
{
return null;
}
}
return value;
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType != typeof(string))
{
return base.ConvertTo(context, culture, value, destinationType);
}
if (value != null)
{
return value;
}
return string.Empty;
}

public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
string[] textArray1 = null;
if (context != null)
{
IEventBindingService service1 = (IEventBindingService) context.GetService(typeof(IEventBindingService));
if (service1 != null)
{
ICollection collection1 = service1.GetCompatibleMethods(this.evt);
textArray1 = new string[collection1.Count];
int num1 = 0;
foreach (string text1 in collection1)
{
textArray1[num1++] = text1;
}
}
}
return new TypeConverter.StandardValuesCollection(textArray1);
}

public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;
}

public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}



}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.