PropertyGrid Adding EventsTab
I am having problems adding the EventsTab to the propertygrid. I have not been able to find much information on how this is done and documentation is very rare.
My code adds the EventsTab to the propertygrid but it never gets displayed. I am setting the Site of the propertygrid to that of my designer as I have seen that you should do this but still It doesn't display the EventsTab.
Any help would be much appreciated.
Thanks
Thank you very much for your help, Giftednewt and Quirk. The truth is that I don't understand this process very well (I don't known where I have to add the OnSelectionChanged event to filter the events, for example), but anyway, I have a project with a Form that includes a designer (a Form and a Button) and other Form with one PropertyGrid that shows the properties and events of both controls. I'm sure that the project has many bugs, but it could be a good start point for somebody who, like me, doesn't have this process very clear.Below, is the source code of the unit, if someone is interested or can help him.
If there is a person who knows how this mechanism works... like Giftednewt.... and has a bit of time, I'm pleased that he can comments the source code and clean the code from bugs, adding or deleting whatever he want.
Thank you again and here is the code.
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace WindowsFormsDesigner
{
#region MainForm
class MainForm : Form
{
#region Windows Designer
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
PropertiesForm propertiesForm;
MyDesignSurface designSurface;
public MainForm()
{
this.SuspendLayout();
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(500, 500);
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "Windows Forms Designer";
this.ResumeLayout(false);
designSurface = new MyDesignSurface();
propertiesForm = new PropertiesForm(designSurface);
propertiesForm.Left = this.Width + this.Left;
propertiesForm.Height = this.Height;
propertiesForm.Show();
IServiceContainer serviceContainer = (IServiceContainer)designSurface.GetService(typeof(IServiceContainer));
serviceContainer.AddService(typeof(IEventBindingService), new EventBindingService(designSurface));
ISelectionService selectionService = (ISelectionService)designSurface.GetService(typeof(ISelectionService));
selectionService.SelectionChanged += new EventHandler(OnSelectionChanged);
designSurface.BeginLoad(typeof(Form));
Control c = designSurface.View as Control;
c.Parent = this;
c.Dock = DockStyle.Fill;
IDesignerHost designerHost = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
System.Drawing.Design.IToolboxUser itu = (System.Drawing.Design.IToolboxUser)designerHost.GetDesigner(designerHost.RootComponent);
itu.ToolPicked(new System.Drawing.Design.ToolboxItem(typeof(Button)));
}
private void OnSelectionChanged(object sender, System.EventArgs e)
{
ISelectionService s = (ISelectionService)designSurface.GetService(typeof(ISelectionService));
IDesignerHost designerHost = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
object[] selection;
if (s.SelectionCount == 0)
propertiesForm.SetObjectToPropertyGrid(null);
else
{
selection = new object[s.SelectionCount];
s.GetSelectedComponents().CopyTo(selection, 0);
propertiesForm.SetObjectToPropertyGrid(selection);
}
}
}
#endregion
#region MyDesignerSurface
class MyDesignSurface : DesignSurface, IServiceProvider
{
#region IServiceProvider Members
object IServiceProvider.GetService(Type serviceType)
{
return this.GetService(serviceType);
}
#endregion
}
#endregion
#region PropertiesForm
class PropertiesForm : Form
{
private MyDesignSurface designSurface;
private System.ComponentModel.IContainer components = null;
private MyPropertyGrid pg = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
public PropertiesForm(MyDesignSurface designSurface)
{
this.designSurface = designSurface;
this.SuspendLayout();
this.TopMost = true;
this.StartPosition = FormStartPosition.Manual;
this.Size = new Size(258, 700);
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
this.ShowInTaskbar = false;
pg = new MyPropertyGrid();
pg.Parent = this;
pg.Dock = DockStyle.Fill;
this.ResumeLayout(false);
}
internal void SetObjectToPropertyGrid(object[] c)
{
IDesignerHost designerHost = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
if (c == null)
pg.SelectedObject = null;
else
pg.SelectedObjects = c;
if (designerHost != null)
{
pg.Site = (new IDEContainer(designerHost)).CreateSite(pg);
pg.PropertyTabs.AddTabType(typeof(System.Windows.Forms.Design.EventsTab), PropertyTabScope.Document);
pg.ShowEvents(true);
}
else
{
pg.Site = null;
}
}
}
#endregion
#region MyPropertyGrid
internal class MyPropertyGrid : System.Windows.Forms.PropertyGrid
{
private System.ComponentModel.Container components = null;
public MyPropertyGrid()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Component Designer generated code
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
public void ShowEvents(bool show)
{
ShowEventsButton(show);
}
}
#endregion
#region Site
// this is taken from SharpDevelop. It's a copy&paste
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)
{
ISite site = base.CreateSite(component, name);
if (site == null)
{
}
return new IDESite(component, this, name);
}
private IServiceProvider serviceProvider;
}
#endregion
#region EventBindingService
public class EventBindingService : System.ComponentModel.Design.EventBindingService
{
public EventBindingService(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)
{
List<object> l = new List<object>();
return l;
}
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
}