Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Control

Status
Not open for further replies.

robertfah

Programmer
Mar 20, 2006
380
US
I've written an app that uses permissions stored in a DB for each control on a given form within the app. How can I go through each Control on a given form and find the coresponding control name (pulled from the DB) and make it Enabled/Disabled?

I know I have to loop through the Controls collection and do that recursively because the given control could have children controls, but I can't seem to get it to work. Here's my code:

Code:
//Set Permissions...
Permissions oPermissions = new Permissions();
DataSet dsPermissions = oPermissions.GetGroupPermissions(Forms.FormNames.frmMain, Users.Instance.GroupID);

for (int x = 0; x < dsPermissions.Tables[0].Rows.Count; x++)
    FindAllControls(dsPermissions.Tables[0].Rows[x], this);

this is the FindAllControls method...
Code:
private void FindAllControls(DataRow dr, Control ctrl)
{
    foreach (Control c in ctrl.Controls)
    {
        if (c.Name == dr["ControlName"].ToString())
            c.Enabled = Convert.ToBoolean(dr["Enabled"].ToString());
        else if (c.HasChildren)
            FindAllControls(dr, c);
    }
}
 
Remove the "else" statement in your recursive method. Otherwise it does not loop to a control which is contained within another control.
 
RyanEK,

Thanks for that....I was so involved that day that I overlooked that. Thanks again.

I found some interesting things about controls and whatnot. I have a Menu Strip msMenu that has sub menu items and those sub menu items may have sub menu items as well. So, when I used the above code, it never processed the sub menu items because they are of a different type (not Control). I ended up storing the type in the DB along with the control name and whether or not it should be enabled or not. So the new code uses 2 functions that are recursive within each other, like so:

Code:
private void TraverseMenuItems(DataRow dr, ToolStripMenuItem tsmi)
{
    //Process any Children that are of the type ToolStripMenuItem (purposely skipping 
    //ToolStripSeparator types).
    if (tsmi.GetType() == typeof (ToolStripMenuItem))
    {
        //Process menu items
        for (int x = 0; x < tsmi.DropDownItems.Count; x++)
        {
            //If the tsic[x] matches the datarow (menu item) then set it's Enabled property
            //to the value that's in the DB.
            if(tsmi.DropDownItems[x].Name == dr["ControlName"].ToString())
                tsmi.DropDownItems[x].Enabled = Convert.ToBoolean(dr["Enabled"]);
            
            //Process sub-menu items.
            TraverseSubMenuItems(dr, tsmi.DropDownItems);
        }
    }
}

private void TraverseSubMenuItems(DataRow dr, ToolStripItemCollection tsic)
{
    //Loop through each item in this Menu Item DropDownItems.
    for (int x = 0; x < tsic.Count; x++)
    {
        //If the tsic[x] matches the datarow (menu item) then set it's Enabled property
        //to the value that's in the DB.
        if (tsic[x].Name == dr["ControlName"].ToString())
            tsic[x].Enabled = Convert.ToBoolean(dr["Enabled"]);
        
        //Process any Children that are of the type ToolStripMenuItem (purposely skipping 
        //ToolStripSeparator types).
        if (tsic[x].GetType() == typeof(ToolStripMenuItem))
        {
            //Convert the ToolStripItemCollection tsic to a ToolStripMenuItem
            //and call the TraverseMenuItems recursive function to check for children.
            ToolStripMenuItem tsmi = (ToolStripMenuItem)tsic[x];
            TraverseMenuItems(dr, tsmi);
        }
    }
}

I had to use the typeof because Separators are not of the type ToolStripMenuItem, so there was a casting error if that statement isn't included. So all I do now is pass in the datarow and the name of the menu control to process and it works like a charm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top