Search code examples
c#winformspropertygridtabpagegeneric-collections

How to get PropertyGrid from TabPage Collection


I created newPropertyGrid and add that to newTabPage. I save all newTabPage to lstTankTabPages(generic collections). I want to raise an event when the data changes in the property grid to change the custom control on the main form. My question, How to get all newPropertyGrid from lstTankTabPages? Thank You.

    for (int i = 0; i < Modules.tankQuantity; i++)
    {
        string name = string.Format("Tank{0}", i + 1);

        // create newTabPage
        TabPage newTabPage = new TabPage(name);
        newTabPage.Name = string.Format("tabPage{0}", name);

        // create newPropertyGrid 
        PropertyGrid newPropertyGrid = new PropertyGrid();
        newPropertyGrid.Name = string.Format("propertyGrid{0}", name);
        newPropertyGrid.BrowsableAttributes = new AttributeCollection(new DisplayModeAttribute("Ullage,Gross,Netto,Water,Temperature,Density,Capacity,Connection,Appearance"));
        newPropertyGrid.SelectedObject = new TankInventoryCustom();

        // add newPropertyGrid to newTabPage
        newTabPage.Controls.Add(newPropertyGrid);

        // add newTabPage to tabControl
        this.tabControlTankInventory.TabPages.Add(newTabPage);

        // add newTabPage to collection
        Modules.lstTankTabPages.Add(newTabPage);
    }

Solution

  • Assuming that Modules.lstTankTabPages is a

    List<TabPage>
    

    then here is one way to get the PropertyGrids you have created in that loop

    foreach(TabPage page in Modules.lstTankTabPages) {
        PropertyGrid newPropertyGrid = (PropertyGrid)page.Controls[0];
        // do something with it
    }