I created two forms: a login form, and a main form that is displayed when I start debugging.
when loading the main form the login form also loaded.
Now my question is, I want to disable the main form when the login form is loaded.
if the connection is successful, the main form must be enabled, otherwise it should be disabled.
I tried this code :
MainFrm .cs :
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
c.Enabled = false;
Connectez ConnectezFrm = new Connectez { TopMost = true, Owner = this };
ConnectezFrm.Show();
}
Connectez.cs :
private MainFrm objMainfrm { get; set; }
public Connectez(MainFrm objfrm)
{
objMainfrm = objfrm;
InitializeComponent();
}
....
....
private void simpleButton1_Click(object sender, EventArgs e)
{
foreach (Control c in objMainfrm.Controls)
c.Enabled = true;
this.Close();
}
You don’t need to disable individual controls on a form to disable the form. You could use
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.enabled
However, what you really want to do in your case is just show a modal dialog. Use
Modal dialogs prevent interaction with their parent while they are active.
Also it looks like you tried to achieve this by passing a reference to your main form to the child form:
That is not necessary to get a modal dialog effect.
If you need to take some action if the connection fails, you can return a DialogResult from Connectez. Check that DialogResult like this: