What better way to start a .NET blog than with a good old fashioned .NET article?
Ever notice how the property grid in the collection editor or in your own applications doesn't have an Events tab? You may think that adding the Events tab isn't a trivial thing to do, well you're wrong.
There is barely any documentation available on the web on how to add custom tabs to the property grid, let alone the Events tab. You might think the Events tab is a proprietary tab, available only in VS.NET. You're wrong again!
So here's the scoop: The Events tab is part of the .NET Framework and it's quite simple to add it to the property grid. Through my insistence, I was able to figure out how to add it, and here's how.
This is all you have to do:
grid.PropertyTabs.AddTabType(typeof(System.Windows.Forms.Design.EventsTab), PropertyTabScope.Global);
Where 'grid' is your property grid. Make sure you reference System.Design.dll in your project.
Now, you might notice that if you do just what I said above, you don't get any results. Don't panic, here's the catch. You have to assign a valid Site to the property grid. That is, you need to set the Site property of the grid to some designer site that you've implemented. If you're hosting your own designers, you should know how to retrieve this. If you don't set a Site, the tab won't show, period. Furthermore, you must assign a value to the Site before you add the tab. And that's basically it!
Now for the second part of this mini-article, adding the Events tab to not just any property grid, but the Collection Editor's property grid! Here's how to do it.
// In your custom collection editor class deriving from CollectionEditor, add this code:
protected override System.ComponentModel.Design.CollectionEditor.CollectionForm CreateCollectionForm()
{
System.ComponentModel.Design.CollectionEditor.CollectionForm frm =base.CreateCollectionForm ();
PropertyGrid grid =null;
//ask it "which one of your controls is a property grid?"
foreach(Control ctl in frm.Controls)
{
if(ctl is PropertyGrid)
{
grid = (PropertyGrid)ctl;
break;
}
}
if(grid !=null)
{
//an ingenious way to hook up to property grid and do whatever the hell we want!
//look ma', no reflection!
grid.BackColor = SystemColors.Control;
grid.LineColor = SystemColors.Control;
grid.ToolbarVisible =true;
grid.HelpVisible =true;
grid.Site = site;
grid.PropertyTabs.AddTabType(typeof(System.Windows.Forms.Design.EventsTab), PropertyTabScope.Global);
}
return frm;
}
And that's it! Now you have an Events tab in your collection editor. Perhaps I should post this on CodeProject.