Click here to Skip to main content
13,248,562 members (60,053 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
public void DisableControl(Control con)
{
foreach(Control c in con.Controls)
{
con.Enabled = false;

}
}

What I have tried:

method for disable only textbox in a form
Posted 14-Oct-17 20:59pm
Updated 14-Oct-17 21:08pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The code is the same, but you need to check "is this a textbox?" inside your loop:
foreach(Control c in con.Controls)
    {
    if (c is TextBox)
        {
        c.Enabled = false;
        }    
    }


Quote:
Can it Work when my textbox is in Panel

Depends on what you pass to your method: if it's the Form, then it won't - the Form.Controls collection contains the panel, and the Panel.Controls collection contains the TextBox so it won't get found.
If it's the Panel you pass then it will.

If you want to disable a textbox "somewhere in a control on the the form", then you need to call your method recursively:
public void DisableControl(Control con)
    {
    foreach(Control c in con.Controls)
        {
        DisableControl(c);
        if (c is TextBox)
            {
            c.Enabled = false;
            }    
        }
    }
That will work if you pass it the form, and disable all textboxes on the form.
  Permalink  
v2
Comments
Member 13224949 15-Oct-17 2:26am
   
Can it Work when my textbox is in Panel
OriginalGriff 15-Oct-17 2:58am
   
Answer updated.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 15 Oct 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100