title: Using Custom PropertyTab in System.Windows.Forms.PropertyGrid Control
body: <p>I am trying to use the customisable property tab feature of the WinForms PropertyGrid control but it has me beaten. I am specifically trying to use in runtime a PropertyGrid to display object properties, as opposed to trying to create design-time support for a control, though they seem to be applications of similar technology.</p>
  
  <p>This question has me seriously confused. I have attempted to follow several tutorials on the topic and none come to any real solid conclusion.</p>
  
  <p><strong>Literature Review</strong></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/aa302334.aspx#vsnetpropbrow_topic05" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa302334.aspx#vsnetpropbrow_topic05</a></p>
  
  <p>This article informs the reader that adding property tabs is easily doable and that the code sample is in the included FunkyButton project, that, isn't included anywhere.</p>
  
  <p><a href="http://www.codeproject.com/KB/grid/PropertyGridExWinForms.aspx" rel="nofollow">http://www.codeproject.com/KB/grid/PropertyGridExWinForms.aspx</a></p>
  
  <p>This project suggests the only way to use custom property tabs is to extend the base property grid functionality by overriding for instance the GetService() private method. I am quite sure this is not required, and attempted to use its information to create a service provider. It did however enlighten me that the class of the SelectedObject in the property grid needs the class attribute PropertyTabAttribute to be assigned to an extended System.Windows.Forms.PropertyGridInternal.PropertiesTab, which I have created and used.</p>
  
  <p><a href="http://www.dacris.com/blog/2008/01/02/net-how-to-show-events-tab-in-a-property-grid/" rel="nofollow">http://www.dacris.com/blog/2008/01/02/net-how-to-show-events-tab-in-a-property-grid/</a></p>
  
  <p>This article indicates that the property grid itself requires the tab to be added using the PropertyGrid.PropertyTabs.AddTabType() method. It then reveals that the Site property of the property grid needs to be assigned and then smugly suggests I should already know how to do this: "...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."</p>
  
  <p><a href="http://www.syncfusion.com/FAQ/windowsforms/faq_c83c.aspx#q913q" rel="nofollow">http://www.syncfusion.com/FAQ/windowsforms/faq_c83c.aspx#q913q</a></p>
  
  <p>In starting to write this question I found a link to this short article on the topic with included code. This project is more focused it seems on design-time functionality however when I used an instance of their object in a completely vanilla PropertyGrid, it showed up both the Properties tab button and their Custom Tab object. I was able to switch between the associated views effortlessly. However the project seems to suggest now custom attributes are required to distinguish properties between the available views. This article now suggests I ignore the Site definitions, specialised PropertyGrids and service providers, and concentrate on the displayed object itself.</p>
  
  <p>Frustratingly, in modifying this project to closer reflect my application, I moved the operational property grid to a tab control's tab page, wherein it broke the ability for the property grid to show the default Properties tab, thus meaning I could switch to the custom property tab, but never back.</p>
  
  <p><strong>Conclusion</strong></p>
  
  <p>I am at a complete loss. I must be close, but of all the avenues I have tried to make this work, perhaps my brain is just fried. What is the key to making property tabs work?
  Is it:</p>
  
  <ul>
  <li>The specific class definition of the PropertyGrid.SelectedObject? </li>
  <li>Settings for the property grid itself?</li>
  <li>All about the custom tab definition?</li>
  </ul>
  
  <p>Did the last project described only work because the SelectedObject inherited from UserControl? Does the SelectedObject need to inherit from System.ComponentModel.Component?</p>
  
  <p>If someone could try to clear this all up and provide the most concise possible description of how to make it work, I would be most grateful.</p>
  
  <p>Many thanks.</p>