I
Ian Ringrose
I wished to show verbs in a PropertyGrid that is hosted in my own WinForms
application. The normal way to show verbs is to provide a IDesigner
implementation, however this only works when the PropertyGrid is hosted
inside of MSDEV. This is my solution, comments please.
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
namespace Main
{
/// <summary>
/// The class enables an object, to display 'verbs' in the property grid.
/// It should NOT be used for any object that will be edited with the
/// property grid that is in MSDEV. It is only usefull for property grids
that
/// are hosted in a customer WinForms app.
///
/// Override the Verbs::get method in sub classes to return the set of
verbs
/// for that class.
/// </summary>
[Serializable]
public class VerbHelper : IComponent, ISite, IMenuCommandService
{
public VerbHelper()
{
}
#region IComponent Members
public event System.EventHandler Disposed;
[Browsable(false)]
public ISite Site
{
get
{
return this;
}
set
{
throw new Exception("I hope this is never called!");
}
}
#endregion
#region ISite Members
[Browsable(false)]
public IComponent Component
{
get
{
return this;
}
}
[NonSerialized]
Container mContainer = null;
[Browsable(false)]
public IContainer Container
{
get
{
if (mContainer == null)
mContainer = new Container();
return mContainer;
}
}
[Browsable(false)]
public bool DesignMode
{
get
{
return false;
}
}
[Browsable(false)]
string ISite.Name
{
get
{
return "My Name";
}
set
{
}
}
#endregion
#region IServiceProvider Members
public object GetService(Type serviceType)
{
if (serviceType == typeof(IMenuCommandService))
return this;
return null;
}
#endregion
#region IDisposable Members
public void Dispose()
{
if (Disposed != null)
Disposed(this, EventArgs.Empty);
}
#endregion
#region IMenuCommandService Members
public void AddCommand(MenuCommand command)
{
}
public void RemoveVerb(DesignerVerb verb)
{
}
public void RemoveCommand(MenuCommand command)
{
}
public MenuCommand FindCommand(CommandID commandID)
{
return null;
}
public bool GlobalInvoke(CommandID commandID)
{
return false;
}
public void ShowContextMenu(CommandID menuID, int x, int y)
{
}
public void AddVerb(DesignerVerb verb)
{
}
[Browsable(false)]
virtual public DesignerVerbCollection Verbs
{
get
{
//override this in a sub class to return your verbs.
return new DesignerVerbCollection();
}
}
#endregion
}
}
application. The normal way to show verbs is to provide a IDesigner
implementation, however this only works when the PropertyGrid is hosted
inside of MSDEV. This is my solution, comments please.
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
namespace Main
{
/// <summary>
/// The class enables an object, to display 'verbs' in the property grid.
/// It should NOT be used for any object that will be edited with the
/// property grid that is in MSDEV. It is only usefull for property grids
that
/// are hosted in a customer WinForms app.
///
/// Override the Verbs::get method in sub classes to return the set of
verbs
/// for that class.
/// </summary>
[Serializable]
public class VerbHelper : IComponent, ISite, IMenuCommandService
{
public VerbHelper()
{
}
#region IComponent Members
public event System.EventHandler Disposed;
[Browsable(false)]
public ISite Site
{
get
{
return this;
}
set
{
throw new Exception("I hope this is never called!");
}
}
#endregion
#region ISite Members
[Browsable(false)]
public IComponent Component
{
get
{
return this;
}
}
[NonSerialized]
Container mContainer = null;
[Browsable(false)]
public IContainer Container
{
get
{
if (mContainer == null)
mContainer = new Container();
return mContainer;
}
}
[Browsable(false)]
public bool DesignMode
{
get
{
return false;
}
}
[Browsable(false)]
string ISite.Name
{
get
{
return "My Name";
}
set
{
}
}
#endregion
#region IServiceProvider Members
public object GetService(Type serviceType)
{
if (serviceType == typeof(IMenuCommandService))
return this;
return null;
}
#endregion
#region IDisposable Members
public void Dispose()
{
if (Disposed != null)
Disposed(this, EventArgs.Empty);
}
#endregion
#region IMenuCommandService Members
public void AddCommand(MenuCommand command)
{
}
public void RemoveVerb(DesignerVerb verb)
{
}
public void RemoveCommand(MenuCommand command)
{
}
public MenuCommand FindCommand(CommandID commandID)
{
return null;
}
public bool GlobalInvoke(CommandID commandID)
{
return false;
}
public void ShowContextMenu(CommandID menuID, int x, int y)
{
}
public void AddVerb(DesignerVerb verb)
{
}
[Browsable(false)]
virtual public DesignerVerbCollection Verbs
{
get
{
//override this in a sub class to return your verbs.
return new DesignerVerbCollection();
}
}
#endregion
}
}