GutierrezDev
GutierrezDev

Reputation: 1076

Getting controls in a winform to disable them

I'm trying to get all controls in a winform disabled at the Load event.

I have a form (MDI) which loads a Login Form. I want to disable the controls behind the Login Form to only let the user enter his username and password, and then if the user is valid re-enable the controls again.

Upvotes: 4

Views: 16027

Answers (7)

Jayant Varshney
Jayant Varshney

Reputation: 1825

Simple Lambda Solution

form.Controls.Cast<Control>()
             .ToList()
             .ForEach(x=>x.Enabled = false);

Upvotes: 4

Kim Ki Won
Kim Ki Won

Reputation: 1855

Container like Panel control that contains other controls
then I used queue and recursive function get all controls.

for (Control control in GetAllControls(this.Controls))
{
    control.Enabled = false;
}


public List<Control> GetAllControls(Control.ControlCollection containerControls, params Control[] excludeControlList)
{
        List<Control> controlList = new List<Control>();
        Queue<Control.ControlCollection> queue = new Queue<Control.ControlCollection>();

        queue.Enqueue(containerControls);

        while (queue.Count > 0)
        {
            Control.ControlCollection controls = queue.Dequeue();

            if (controls == null || controls.Count == 0)
                continue;

            foreach (Control control in controls)
            {
                if (excludeControlList != null)
                {
                    if (excludeControlList.SingleOrDefault(expControl => (control == expControl)) != null)
                        continue;
                }

                controlList.Add(control);
                queue.Enqueue(control.Controls);
            }
        }

        return controlList;
    }

Upvotes: 3

Tim Jarvis
Tim Jarvis

Reputation: 18825

Just for some fun with linq, because you can.....

What you could do is create a "BatchExecute" extension method for IEnumerable and update all your controls in 1 hit.

  public static class BatchExecuteExtension
  {
    public static void BatchExecute<T>(this IEnumerable<T> list, Action<T> action)
    {
      foreach (T obj in list)
      {
        action(obj);
      }
    }
  }

Then in your code....

this.Controls.Cast<Control>().BatchExecute( c => c.enabled = false);

Cool.

Upvotes: 2

GutierrezDev
GutierrezDev

Reputation: 1076

Trying the ShowDialog show this exception: Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.

What im doing is this:

private void frmControlPanel_Load(object sender, EventArgs e)

{
            WindowState = FormWindowState.Maximized;

           ShowLogin();
           //User = "GutierrezDev"; // Get user name.
           //tssObject02.Text = User;
}

 private void ShowLogin()
        {
            Login = new frmLogin
                        {
                            MdiParent = this,
                            Text = "Login",
                            MaximizeBox = false,
                            MinimizeBox = false,
                            FormBorderStyle = FormBorderStyle.FixedDialog,
                            StartPosition = FormStartPosition.CenterScreen

                        };
            Login.ShowDialog();

        }

Upvotes: 0

Glenn
Glenn

Reputation: 1167

As Ed said, showing the form as a modal dialog will do what you want. Be sure to check the dialog result returned from ShowDialog in case they cancel it instead of clicking login.

But if you really want to disable all the controls on the form then you should be able to just disable the form itself, or some other parent control like a panel that has all controls in it. That will disable all child controls. This will also allow the child controls to go back to their previous state when the parent control is enabled again.

Upvotes: 0

jasonh
jasonh

Reputation: 30303

I agree that ShowDialog is the way to go, but to answer the original question, you can do this if you want to disable all controls:

foreach (Control c in this.Controls)
{
    c.Enabled = false;
}

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124770

Just show the login form as a modal dialog, i.e., frm.ShowDialog( ).

Or, if you really want to disable each control, use the Form's Controls collection:

void ChangeEnabled( bool enabled )
{
    foreach ( Control c in this.Controls )
    {
        c.Enabled = enabled;    
    }
}

I suggest doing it this way instead of simply setting the Form's Enabled propery because if you disable the form itself you also disable the tool bar buttons. If that is ok with you then just set the form to disabled:

this.Enabled = false;

However, if you are going to do this you may as well just show the login prompt as a modal dialog :)

Upvotes: 17

Related Questions