473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 11244
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: HolaGoogle | last post by:
Hi all, Can you please tell me what's wrong with my code??? i do have this database in wich i have to field.One is a "yes/no" field and another one is "number" field. To display the yes/no field...
2
by: HolaGoogle | last post by:
Can you please tell me the right way to do this?? it's realy important! thanks in advance... Hi all, Can you please tell me what's wrong with my code??? i do have this database in wich i have...
1
by: Shabam | last post by:
Is there a way to present a form to a user by default with all forms disabled, then when user clicks a link, the fields become enabled?
12
by: Forti2ude | last post by:
Hello, I have a simple form... <form> <select name="foo" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select>
3
by: Yuelin | last post by:
Hi there How can I disable a form being resized? i can't find a property in the property window to do this. Thanks yuelin
2
by: Michael Creager | last post by:
I am using VB NET 2002. How can I programmatically create a new windows form using the value of a string variable as the name of the new form? Public FrmName As String = "MDIChildFrm1" Public...
9
by: Marc | last post by:
Okay, here's the problem - I have 3 radio buttons, and 11 check boxes (which are disabled by default). I have the javascript below which when the last radio button is clicked, enables the...
0
by: blackmagicinc | last post by:
hi, "Show Window Content while Dragging" is the big flaw in windows XP How to programmatically disable "Show Window Content while Dragging" from VB6 ? Thanks in advance...
1
by: prisk | last post by:
Hi, I have a form that has checkboxes and lists generated in php with the names like this <input name=chk<?$i$j?onClick="EnableLst(name of checkbox)"> <select name =lst<?$i$j?disabled> in a loop...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.