User Control panels

larryse

Member
Joined
Dec 25, 2012
Messages
6
Programming Experience
1-3
I have an app with 6 user control panel. Can I put them in a list, idea is to put them in a List then when a menu item is selected the click passes that panel name in to a method and that method goes through the list and if the item equals the one passed show it and bring to front else hide.

I can create a list of Objects and pass the new panel as an object but how do I get it from obj to something I can show/hide?

Thanks,
Larry
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,563
Location
Sydney, Australia
Programming Experience
10+
Why create a list of objects when you know for a fact that they are all Controls? Create a list of Controls and then you already have something you can show/hide.

This sounds like it could be a bit dodgy, to be honest. If you really are passing the name of a control, rather than a reference to that control, then that's bad and you should fix it. Can you show us the relevant code, as you should have in the first place, so we can assess exactly what you're doing and exactly what the best path forward may be?
 

larryse

Member
Joined
Dec 25, 2012
Messages
6
Programming Experience
1-3
This is my general idea.
C#:
private void frmMyLibrary_Load(object sender, EventArgs e)
{
    controlList.Add(authorAdd1);
    controlList.Add(authorEdit1);
    controlList.Add(bookAdd1);
    controlList.Add(bookEdit1);
    controlList.Add(import1);
    controlList.Add(export1);
    SetControls(main1);
}

private void SetControls(object obj)
{
    foreach (object o in controlList)
    {
        if ( o.GetType() == obj.GetType() )
        {
            //show panel and bring to front
        }
        else
        {
            //hide panel
        }
    }
}

#region Menu Options
private void mnuAuthorAdd_Click(object sender, EventArgs e)
{
    SetControls(authorAdd1);
}
#endregion
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,563
Location
Sydney, Australia
Programming Experience
10+
There doesn't seem to be any need for that list at all. You're calling SetControls and passing it a control, so declare the parameter as type Control and then toggle the visibility of that control. It would be nice to give the method a name that actually indicates what it does, too.
C#:
private void ToggleVisibility(Control control)
{
    control.Visible = !control.Visible;
    
    if (control.Visible)
    {
        control.BringToFront();
    }
}
Now, when you call ToggleVisibility(authorAdd1) on the Click of a menu item, it will be hidden if it's already visible and displayed if it's not.
 
Top Bottom