468,107 Members | 1,451 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

Programmatically Disable All Checkboxes on a form

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

Mar 22 '07 #1
5 10844
On Mar 22, 11:20 am, maste...@gmail.com wrote:
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.

Mar 22 '07 #2
VJ
foreach(Control crtl in this.Controls)
{
crtl.enabled =false;
}

I think this will work..

VJ

<ma******@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
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

Mar 22 '07 #3
Use VJ's solution but the following check before disabling the
control:

if(crtl is CheckBox)
{
crtl.enabled =false;
}

On 22 Mar, 15:29, "VJ" <nonewsaddr...@yahoo.comwrote:
foreach(Control crtl in this.Controls)
{
crtl.enabled =false;

}

I think this will work..

VJ

<maste...@gmail.comwrote in message

news:11**********************@n76g2000hsh.googlegr oups.com...
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

Mar 22 '07 #4
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 <Tallows 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<Controlcontrols = 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);
}
}
}

<ma******@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
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

Mar 22 '07 #5
On Mar 22, 11:58 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
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 <Tallows 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<Controlcontrols = 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);
}
}
}

<maste...@gmail.comwrote in message

news:11**********************@n76g2000hsh.googlegr oups.com...
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- Hide quoted text -

- 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

Mar 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Shabam | last post: by
3 posts views Thread by Yuelin | last post: by
1 post views Thread by prisk | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.