Getting control in winform to disable them

I am trying to disable all controls in the form of win in the Load event.

I have a form (MDI) that loads a login form. I want to disable the controls specified in the login form so that the user can only enter their username and password, and then, if the user is valid, turn on the controls again.

+3
source share
7 answers

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

Or, if you really want to disable each control, use the collection of form controls:

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

, , " ", , . , :

this.Enabled = false;

, , :)

+15

-

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

linq, .....

, "BatchExecute" IEnumerable 1 .

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

....

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

.

+2

, ,
.

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

, ShowDialog - , , , :

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

, , . , ShowDialog, , , .

, - , , . . , .

0

ShowDialog : , , . showDialog.

:

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

Source: https://habr.com/ru/post/1721932/


All Articles