简体   繁体   中英

How to show all hidden controls of a Form in C#?

In C# form window, say i have 5 controls. I have just hide some of them. When I run the program just the (Visible=true) controls should appear. I want to click a button to appear or unhide all controls. How can i do that?

For Winforms, try this:

foreach (Control c in Controls) {
    c.Visible = true;
}

This code loops through all controls on your form and sets the Visible property to true, to make each one visible.

You would need to change the visible propery in the code behind.

eg

button1.visible = true;

and do that for each hidden control, button or not.

Just to add I doubt you would want to do the loop through all controls that everyone else suggests. Changing the visibility on everything on your form is a waste of time and can lead to complications further down the track if you make only certain controls visible during different scenarios.

Since it is only 5 controls, I would stick with changing each one individually.

You could make it recursive, then if you have any panels/group boxes their children get made visible too.

public void MakeVisible(Control control)
{
    if(control.HasChildren)
    {
        foreach (Control child in control.Controls)
        {
            MakeVisible(child);
        }
    }
    control.Visible = true;
}

If you want to hide/show everything,

    foreach (Control cr in this.Controls)
        cr.Visible = false; // or true, if you want to show everything

Something like

foreach (var controlObj in form.Controls)
    ((Control)controlObj).Visible = True;

should do the trick. This simply shows all controls in the form. (Note that Form.Controls is an untyped Collection containing only Object s, so you have to cast them afair)

If you have only 5 controls, you could also show them individually, perhaps in a method like

void showHiddenControls(bool show) {
  control1.Visible = show;
  control2.Visible = show;
  // ...
}

This has the advantage that you can show them using showHiddenControls(true) and hide them again using showHiddenControls(false) .

The main idea would be to create objects like that :

Label toto = new Label():
(...) Do whatever you wan't to initialize your object
toto.Visible = false;

Button makeItAppear = new Button();
(..) Do whatever you wan't to initialize this button

And then add an handler on the click event :

makeItAppear.Click += new System.EventHandler(MakeItAppear);

And then in the eventHandler :

private void MakeItAppear(object sender, EventArgs e)
{
    this.toto.Visible = true;
}

The best would be to create a function which make this appear and which is called by the Handler. And of course, if you have multiple object to make appear or disappear do the same for all in this function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM