简体   繁体   English

如何在C#中显示窗体的所有隐藏控件?

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

In C# form window, say i have 5 controls. 在C#窗体窗口中,说我有5个控件。 I have just hide some of them. 我只是藏了一些。 When I run the program just the (Visible=true) controls should appear. 当我运行程序时,仅应显示(Visible = true)控件。 I want to click a button to appear or unhide all controls. 我想单击一个按钮以显示或取消隐藏所有控件。 How can i do that? 我怎样才能做到这一点?

For Winforms, try this: 对于Winforms,请尝试以下操作:

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. 此代码循环遍历窗体上的所有控件,并将Visible属性设置为true,以使每个控件可见。

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. 由于只有5个控件,因此我会坚持单独更改每个控件。

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) (请注意, Form.Controls是一个仅包含Object的无类型Collection,因此您必须公平地对其进行转换)

If you have only 5 controls, you could also show them individually, perhaps in a method like 如果您只有5个控件,则也可以分别显示它们,例如

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) . 这样做的好处是,您可以使用showHiddenControls(true)显示它们,然后使用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 : 然后在click事件上添加一个处理程序:

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

And then in the eventHandler : 然后在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. 最好的办法是创建一个使它出现并由Handler调用的函数。 And of course, if you have multiple object to make appear or disappear do the same for all in this function. 当然,如果您要使多个对象出现或消失,请对该函数执行所有操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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