Wednesday, April 23, 2008

Events tab in property grid

have you ever looked at the property grid in VS IDE and wondered how to get the
events tab on?

Well , the good news is that the framework provides an EventTab that can be added to the propertytabs collection of the propertygrid..

Wait... thats not it ...

To be able to see thevents tab , you need to assign the propertygrid's site to your custom site.

Why??

Because propertygrid internally uses the site's getservice to retrieve the eventsbindingservice.
So you need to create your own custom site.

I can hear you say , Alright , but it looks like something more is required...

Yes, you need to provide the EventsBindingService by implementing IEventsBindingService.

Now, how do you connect the eventsbindingservice class to your site?

You have to add the service to the service container.


Below is the code to implement the propertygrid
For further study , u might be interested in propertygridinternal.viewevent code
( i use reflector ).

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Xml;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;

namespace NewPanel {

public partial class PropertyForm : Form
{

MyDesignSurface designSurface;

public PropertyForm()

{

InitializeComponent();
designSurface = new MyDesignSurface();
IServiceContainer serviceContainer = (IServiceContainer)designSurface.GetService(typeof(IServiceContainer));
serviceContainer.AddService(typeof(IEventBindingService), new MyEventBindingService(designSurface));

}

// a new object has been selected

public void SetNewObject(Object obj)
{

IDesignerHost designerHost = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));

this.extPropertyGrid.SelectedObject = obj;

if (designerHost != null)
{
extPropertyGrid.Site = (new IDEContainer(designerHost)).CreateSite(extPropertyGrid);

extPropertyGrid.PropertyTabs.AddTabType(typeof(System.Windows.Forms.Design.EventsTab), PropertyTabScope.Document); extPropertyGrid.ShowEvents(true);

}

else
{

extPropertyGrid.Site = null;
}

}

}

#region MyDesignerSurface

class MyDesignSurface : DesignSurface, IServiceProvider
{

#region IServiceProvider Members

object IServiceProvider.GetService(Type serviceType)
{ return this.GetService(serviceType); }
#endregion

}
#endregion

#region Site

class IDEContainer : Container
{

class IDESite : ISite
{
private string name = "";
private IComponent component;
private IDEContainer container;

public IDESite(IComponent sitedComponent, IDEContainer site, string aName) { component = sitedComponent;
container = site;
name = aName;

}

public IComponent Component { get { return component; } }
public IContainer Container { get { return container; } }
public bool DesignMode { get { return false; } }
public string Name { get { return name; } set { name = value; } } public object GetService(Type serviceType) { return container.GetService(serviceType); }

}

public IDEContainer(IServiceProvider sp) { serviceProvider = sp; }

protected override object GetService(Type serviceType)
{
object service = base.GetService(serviceType);
if (service == null)
{
service = serviceProvider.GetService(serviceType);
}
return service;
}

public ISite CreateSite(IComponent component)
{
return CreateSite(component, "UNKNOWN_SITE");
}

protected override ISite CreateSite(IComponent component, string name)
{
return new IDESite(component, this, name);

}

private IServiceProvider serviceProvider;

}

#endregion

#region EventBindingService public class MyEventBindingService : System.ComponentModel.Design.EventBindingService

{

public MyEventBindingService(IServiceProvider myhost) : base(myhost) { }

#region IEventBindingService Members

protected override string CreateUniqueMethodName(IComponent component, EventDescriptor e)
{
throw new Exception("The method or operation is not implemented.");
}

protected override System.Collections.ICollection GetCompatibleMethods(System.ComponentModel.EventDescriptor e)
{
throw new Exception("The method or operation is not implemented.");
}

protected override bool ShowCode(System.ComponentModel.IComponent component, System.ComponentModel.EventDescriptor e, string methodName)
{
throw new Exception("The method or operation is not implemented.");

}

protected override bool ShowCode(int lineNumber)
{
throw new Exception("The method or operation is not implemented.");
}

protected override bool ShowCode()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
} #endregion
}

No comments: