Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

i have multi text box with different name i want to change attribute .enable=true;

and use foreach or for
how can i ?

same
C#
foreach (Control tx in this.Controls)??????
           {
                Type c=tx.GetType();
                if (c == typeof(TextBox))
                   ????????????? i dont know

           }

for example txt1.enable=true;
Posted

In this case, I'd want to invest a little more time and effort to get something that can be re-used. And, I'd want something that could operate on any particular kind of control, and that could be run with and without recursive checking for controls within controls:
C#
private void SetControlsEnabledProperty(bool isEnabled, bool isRecursive, string typeToCheck, Control.ControlCollection theControls)
    {
        foreach (Control theControl in theControls)
        {
            // is there a better way to handle the
            // the type checking required here ?
            if (theControl.GetType().Name == typeToCheck)
            {
                theControl.Enabled = isEnabled;
            }
            else
            {
                if (isRecursive && theControl.HasChildren)
                {
                    SetControlsEnabledProperty(isEnabled, isRecursive, typeToCheck, theControl.Controls);
                }
            }
        }
    }
}
I might call this function like this to iterate all the Controls in a Form, and set every TextBox's Enabled property to true:
SetControlsEnabledProperty(true, true, "TextBox", this.Controls);
 
Share this answer
 
v3
Comments
[no name] 10-Oct-11 22:28pm    
tanks
this work but there is a one problem when i use (thecontrol.text="")all lables and textbox become clear but i want clear just textbox
how can i do?
BillWoodruff 11-Oct-11 1:50am    
Hi Mohammed, In editing the code I somehow got an extra statement in there: "theControl.Enabled = isEnabled;" which was outside the if/clause that tested if the type of the Control was the type to be Enabled. I've removed that statement, and I believe if you insert your theControl.Text = ""; statement inside the if/clause it will work properly now.

I've asked a question on the C# forum about the "cost" of using variables in the parameter list of a recursive procedure ... when those variables' value does not change ... this question came up in the course of looking at this code. Link: http://www.codeproject.com/Messages/4048177/How-cheap-is-recursive-use-of-variables.aspx
C#
foreach (Control control in this.Controls)
{
    TextBox textBox = control as TextBox;
    if (!object.ReferenceEquals(null, textBox))
        textBox.Enabled = true;
}
 
Share this answer
 
Comments
Luc Pattyn 10-Oct-11 21:55pm    
IMO if (textBox!=null) would be adequate, no need for object.ReferenceEquals()

:)
DaveyM69 11-Oct-11 12:59pm    
Hi Luc,
True, it's just a habit I've got into from being caught out occaisionally by objects that don't implement the == and != operators. If they do iplement them they will use object.ReferenceEquals() in their equality checks against null anyway so no overhead, more readable without perhaps ;)

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