Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
'This code work in VB.net

Dim X As Integer
For X = 1 To 10
Me.Controls("reg" & X).Enable = False

//when I try this in C# it don't work
int x = 0;
for (x = 1; x <= 10; x++)
{
this.Controls["reg" + x].enable=false;
}
Posted

There are a lot potential of problems in the code:
- enable property does not exist. Enabled does.
- the name of the controls may not match your name generation in the loop
- you start counting from 1 instead of 0
- what if there are more than 10 controls and so on.

Instead, try something like
C#
for (int counter = 0; counter < this.Controls.Count; counter++)
{
   this.Controls[counter].Enabled = false;
}


ADDED
If you want to disable only textboxes with the name having a certain number it's a bit more complicated.

First of all you may have many controls in the controls collection and it cannot be guaranteed that these are the 10 first controls when you loop through controls collection. Because of this you need to check all controls in the control collection, one way or another.

Also you need to check that the name matches you pattern again in one way or another.

Here's an alternative which loops through all textboxes having the name starting with Reg and checking the number:
C#
// Search for the number in the control name
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\d+");
// Loop through all controls which are text boxes and the name starts with "reg"
foreach (System.Windows.Forms.Control control in this.Controls.OfType<System.Windows.Forms.TextBox>().Where(x=> x.Name.StartsWith("reg"))) {
   // If the control name contains a number
   if (regex.IsMatch(control.Name)) {
      // Get the value from the control name
      controlNumber = int.Parse(regex.Match(control.Name).Value);
      // if the value is between desired range
      if (controlNumber >= 1 && controlNumber <= 10) {
         control.Enabled = false;
      }
   }
}
 
Share this answer
 
v3
Comments
GP-TECH 26-Jan-15 20:31pm    
Mika

Thanks for the code, but this code disable the entire form. I only want combo box and text boxes to disable at some point of time
i have a form with 11 text boxes and a combo box on form load event i want to disable the first 10 text boxes with the name reg1-reg10
Wendelius 26-Jan-15 23:51pm    
Solution updated
BillWoodruff 27-Jan-15 3:34am    
+5
Wendelius 5-Feb-15 0:30am    
Thank you :)
Maybe it's because you using capital first letter in control's name 'Reg...' but in code you're using small letter 'reg...'.

And you should check if control exists. Try this code:

C#
for (int x = 1; x <= 10; x++)
{
    var control = this.Controls["Reg" + x];
    if (control != null)
    {
        control.Enabled = false;
    }
}


Cheers!
 
Share this answer
 
foreach(Control c in this.Controls){
c.enabled=false;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900