Reputation: 146
How to hide "all" textboxes together when button is clicked? Is there any short method to avoid hiding them one by one?
gamma_textBox.Visible = false;
Upvotes: 0
Views: 3009
Reputation: 460138
You could use this generic, recursive method:
private void ProcessAllControls<T>(Control rootControl, Action<T> action)where T:Control
{
foreach (T childControl in rootControl.Controls.OfType<T>())
{
action(childControl);
}
foreach (Control childControl in rootControl.Controls)
{
ProcessAllControls<T>(childControl, action);
}
}
It works this way:
ProcessAllControls<TextBox>(this, txt=> txt.Visible = false);
This method searches recursively all child controls of a given container control for controls of a specified type. Then it applies an action(in this case it changes Visibility)
.
If you want it for any kind of control, use the non-generic overload:
public static void ProcessAllControls(Control rootControl, Action<Control> action)
{
foreach (Control childControl in rootControl.Controls)
{
ProcessAllControls(childControl, action);
action(childControl);
}
}
Upvotes: 3
Reputation: 98750
If you using Winforms, you can do it like;
for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls[i].GetType().ToString() == "System.Windows.Forms.TextBox")
this.Controls[i].Visible = false;
}
Upvotes: 2
Reputation: 116498
foreach(var tb in this.Controls.OfType<TextBox>()) tb.Visible = false;
But note this will not look inside any containers. You can do so recursively though by enumerating each child's Controls
collection. An example of this would be:
public void HideChildTextBoxes(Control parent)
{
foreach(Control c in parent.Controls)
{
HideChildTextBoxes(c);
if(c is TextBox) c.Visible = false;
}
}
Upvotes: 4
Reputation: 51
You can put all textbox into a LIST(Or array),then use a for iteration to make them invisiable.
Upvotes: 0
Reputation: 2622
Paste them into panel(or other container), and set visibility of that panel.
Upvotes: 1