Question

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);
    }
Was it helpful?

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
}

OTHER TIPS

You can use "OfType" to get all the PropertyGrid items

foreach (TabPage page in Modules.lstTankTabPages)
{
    PropertyGrid newPropertyGrid = page.Controls.OfType<PropertyGrid>().FirstOrDefault()
    if (newPropertyGrid != null)
    {
        // do something with it
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top