开发者

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开发者_如何学JAVA user is valid re-enable the controls again.


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


Simple Lambda Solution

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


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;
    }


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.


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;
}


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.


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

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜