tags:

views:

292

answers:

5

Hi everybody.

Im traying to get all controls in a winform just disable them 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 reenable the controls again.

Thanks...

+5  A: 

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 :)

Ed Swangren
Thanks for the reply. I think it would be better to show it as a ShowDialog because if i use the foreach it disable the Login form too.
GutierrezDev
No, it won't disable the login form unless you explicitly add the form to your main form's Controls collection. Controls that your form creates are not automatically added to that collection.
Ed Swangren
That said, ShowDialog is the way to go here. This is exactly what it was made to do, and people will likely not understand why you are disabling controls instead of using ShowDialog when they look at your code.
Ed Swangren
A: 

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;
}
jasonh
A: 

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.

Glenn Condron
A: 

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();

        }
GutierrezDev
You need to call frmLogin.ShowDialog(); not Login.ShowDialog();.
Glenn Condron
Why do i need the frmLogin.showDialog if i'm creating an instance of it?
GutierrezDev
And the frmLogin doesn't have the ShowDialog method.
GutierrezDev
A: 

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.

Tim Jarvis
isnt the extension method redundant here? you could just issue Controls.Cast<Control>().ToList<Control>().ForEach(s => s.Enabled = false);and remove the unneccesary for each loop and still get the same result
Matt
this.Controls.Cast<Control>().ToList<Control>().ForEach(s => s.Enabled = false);just to make it more readable in the comments
Matt