views:

281

answers:

1

Does anybody know how to programmatically set the selected PropertyTab on a PropertyGrid in the .Net framework? The SelectedTab property is not settable, which is understandable, since the documentation indicates you should not be creating instances of PropertyTabs yourself. However, I cannot seem to find a corresponding method to call nor property to set on the PropertyGrid instance to change the tab from code, eg SelectTab(Type tabType) / int SelectedTabIndex { set; }. Any ideas?

+2  A: 

Poster Daniel almost had it. Here is what actually works, if you were to apply this to your own subclass of PropertyGrid:

    public int SelectedTabIndex 
    {
        set
        {
            Type pgType = typeof(PropertyGrid);
            BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;

            ToolStripButton[] buttons = (ToolStripButton[]) pgType.GetField("viewTabButtons", flags).GetValue(this);
            pgType.GetMethod("SelectViewTabButton", flags).Invoke(this, new object[] { buttons[value], true });
        }
    }

Like Daniel says, this is bad form and entirely unsupported, but it does work as long as you do not have to worry about cross-domain access permissions.

Not Sure
Slight improvement: Instead of calling SelectViewTabButton(ToolStripButton), calling OnViewTabButtonClick(ToolStripButton, EventArgs) is preferable.
Not Sure
That does look better :-]
Daniel LeCheminant