-1

I have This "HOME" as the main form ...and i have the admin login button which opens the admin login form...but the button should restrict the number of admin login form to only

Sergey Berezovskiy
  • 224,436
  • 37
  • 411
  • 441
  • Maybe a duplicate of http://stackoverflow.com/questions/3087841/how-can-i-make-a-single-instance-form-not-application – CodeCamper Jun 13 '13 at 10:19

4 Answers4

2

Either show login form with ShowDialog() -> this will block rest forms as long as login form is visible or keep track of opened forms and do nothing on button click when login form is opened.

First Example:

private void ButtonClick(object sender, EventArgs e)
{
    var frm = new LoginForm();
    frm.ShowDialog();
}

Second example:

private LoginForm form;

private void ButtonClick(object sender, EventArgs e)
{
    if (form != null)
    {
       if (form.Visible)
       {
           return;
       }

       form.Show();
    }
    else
    {
       form = new LoginForm();
       form.Show();
    }
}

Third example (with LINQ):

private void ButtonClick(object sender, EventArgs e)
{
    if (Application.OpenForms.Cast<Form>().Any(x => x.GetType() == typeof(LoginForm)))
    {
        return;
    }

    var frm = new LoginForm();
    frm.Show();
}
gzaxx
  • 16,801
  • 2
  • 33
  • 54
1

A simple solution is to set a boolean flag once you open up the form.

bool AdminFormOpen;

private void adminLoginBtn_click()
{
    if(!AdminFormOpen)
    {
        // Open the form.
        AdminFormOpen = true;
    }
}

Then, in the Admin Form, ensure that you reset this value via a mutator method. Something like:

protected void OnClosed(EventArgs e)
{
      parentForm.setAdminFormOpen(false);
}
christopher
  • 25,939
  • 5
  • 53
  • 88
1

You can use Application.OpenForms collection to check if login form is already opened instead of using boolean flag for this:

if (!Application.OpenForms.OfType<LoginForm>().Any())
{
    var loginForm = new LoginForm();
    loginForm.Show();
}

Or use Form.ShowDialog() to open login form as modal form.

Sergey Berezovskiy
  • 224,436
  • 37
  • 411
  • 441
0

Use a boolean variable to signal if the form is already.

Oscar
  • 13,056
  • 8
  • 43
  • 69