Hi, Gurjinder.
When I develop my design-time form, I use following code:
// There is a class to show EventTab in PropertyGrid
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);
}
public bool DrawFlat
{
get { return DrawFlatToolbar; }
set { DrawFlatToolbar = value; }
}
}
and add next code to your code
....
private MyPropertyGrid propertyGrid;
....
// Initialization PropertyGrid
this.propertyGrid = new MyPropertyGrid();
....
this.propertyGrid.SelectedObjectsChanged += new EventHandler(OnPropertyGridSelectedObjectChanged);
this.propertyGrid.SelectedGridItemChanged += new SelectedGridItemChangedEventHandler(OnSelectedGridItemChanged);
....
protected void OnPropertyGridSelectedObjectChanged(object sender, EventArgs e)
{
UpdatePropertyGridSite();
this.propertyGrid.ShowEvents(true);
}
private PropertyDescriptor lastProperty;
private object lastValue;
private void OnSelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
{
GridItem gridItem = e.NewSelection;
if (gridItem != null && (lastProperty = gridItem.PropertyDescriptor) != null)
{
Object selectedObj = this.propertyGrid.SelectedObject;
if (selectedObj != null)
{
try
{
lastValue = lastProperty.GetValue(selectedObj);
lastProperty.AddValueChanged(selectedObj, new EventHandler(OnPropertyChanged));
}
catch {}
}
}
}
....
protected void UpdatePropertyGridSite()
{
if(propertyGrid != null)
{
propertyGrid.Site = null;
IServiceProvider provider = GetPropertyGridServiceProvider();
if(provider != null)
{
propertyGrid.Site = new MySite(provider, propertyGrid as IComponent);
propertyGrid.PropertyTabs.AddTabType(typeof(System.Windows.Forms.Design.EventsTab));
}
}
}
protected IServiceProvider GetPropertyGridServiceProvider()
{
object selObject = null;
if (propertyGrid.SelectedObjects != null && propertyGrid.SelectedObjects.Length > 0)
selObject = propertyGrid.SelectedObjects[0];
else
selObject = propertyGrid.SelectedObject;
if (selObject is Component)
return (selObject as Component).Site;
return null;
}
I hope, I help you.
If you'll have some questions, you welcome.