Programmatically Disable All Checkboxes on a form

  • Thread starter Thread starter masterej
  • Start date Start date
M

masterej

Developers,

Is there any way to disable all checkboxes on a form? I have a form
with 160 checkboxes and I want to be able to disable all of them. Is
there a way I can do something like this:

for (int i = 0; i < 160; i++)
{
string checkbox = "checkBox" + i.toString();
(Cast)checkbox.enabled = false;
}


Thanks,

Eric
 
Developers,

Is there any way to disable all checkboxes on a form? I have a form
with 160 checkboxes and I want to be able to disable all of them. Is
there a way I can do something like this:

for (int i = 0; i < 160; i++)
{
string checkbox = "checkBox" + i.toString();
(Cast)checkbox.enabled = false;

}

Thanks,

Eric

You need to find the control objects and then set their enabled
property. You are very close:

for (int i = 0; i < 160; i++)
{
string checkbox = "checkBox" + i.toString();
((CheckBox)FindControl(checkbox)).enabled = false;

}

There is a lot of information that you have hardcoded in here that
would make it difficult to update in the future. It would perhaps be
easier to have a checkbox list and then iterate through all of it's
child controls and set their enabled state.
 
Use VJ's solution but the following check before disabling the
control:

if(crtl is CheckBox)
{
crtl.enabled =false;
}
 
Here's the method I use for this type of thing (using yours as an
example):

ActionAll<CheckBox>(this, delegate (CheckBox box) {box.Enabled =
false;});

Very useful; the <T> allows you to restrict it to a given category
(class) of Control, and the root allows you to limit the scope.

Marc

/// <summary>
/// Applies an action to all controls of type T in a tree
/// </summary>
public static void ActionAll<T>(Control root, Action<T>
action) where T : Control
{
if (root == null) throw new ArgumentNullException("root");
if (action == null) throw new
ArgumentNullException("action");
Queue<Control> controls = new Queue<Control>();
controls.Enqueue(root);
while (controls.Count > 0) // recurse
{
Control control = controls.Dequeue();
foreach (Control child in control.Controls)
{
controls.Enqueue(child);
}
T item = control as T;
if (item != null) // is a T
{
action(item);
}
}
}
 
Here's the method I use for this type of thing (using yours as an
example):

ActionAll<CheckBox>(this, delegate (CheckBox box) {box.Enabled =
false;});

Very useful; the <T> allows you to restrict it to a given category
(class) of Control, and the root allows you to limit the scope.

Marc

/// <summary>
/// Applies an action to all controls of type T in a tree
/// </summary>
public static void ActionAll<T>(Control root, Action<T>
action) where T : Control
{
if (root == null) throw new ArgumentNullException("root");
if (action == null) throw new
ArgumentNullException("action");
Queue<Control> controls = new Queue<Control>();
controls.Enqueue(root);
while (controls.Count > 0) // recurse
{
Control control = controls.Dequeue();
foreach (Control child in control.Controls)
{
controls.Enqueue(child);
}
T item = control as T;
if (item != null) // is a T
{
action(item);
}
}
}








- Show quoted text -

Thanks for the suggestions...I was able to find a way to do this using
Controls.Find to store all of my checkboxes in an array, then
programmatically disable them. Here's my solution:

for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 32; j++)
{
string checkbox = "checkBox" + j.ToString() + "s"
+ i.ToString();
ArrayList checkboxes = new ArrayList();
checkboxes.AddRange(this.Controls.Find(checkbox,
true));
foreach (Control item in checkboxes)
{
item.Enabled = false;
}

}
}

I have 5 rows of checkboxes, numbered 1-32. This function disables
them, and I can use this to selectively disable boxes at runtime.

Thanks again.

-Eric
 
Back
Top