1

I am trying to enable admin privileges on my startup form. For this I've created an admin toolstrip login which would cause a password form to launch. I am however unable to update the startup form through the password form. I've seen a similar article over here [Why the controls not update, when call method from other form but this hasn't helped me solve my problem. My code is as below and what I have achieved so far is as below:

// Code for startup form...
public partial class StartupForm : Form {
    private void adminToolStripMenuItem_Click(object sender, EventArgs e) {
        FrmAdminPassword frmAdminPassword = new FrmAdminPassword();
        frmAdminPassword.Show();
        //this.Close();
        //AdminLogin();
    }

    public void AdminLogin() {
        loginToolStripMenuItem.Enabled = false;
        logoutToolStripMenuItem.Enabled = true;
        cmb1.Enabled = true;
        btn1.Enabled = true;
        tab1.Enabled = true;
        tab2.Enabled = true;
        tabControl1.TabPages.Add(tabAdminTasks);
        MessageBox.Show("Admin Logged In");
    }
}

// Code for password form
public partial class FrmAdminPassword : Form {
    private void btnLoginAdmin_Click(object sender, EventArgs e) {
        if (mskAdminPassword.Text == "password") {
            StartupForm frm = new StartupForm();
            frm.Show();
            frm.AdminLogin();
            this.Close();
        }
        else {
            MessageBox.Show("Not a valid password");
            this.Close();
        }

    }
}

If I implement it this way, what happens is that the original instance of the startup form is still present as a duplicate instance with all tabs and controls disabled and a new form is launched with all controls enabled.

What I actually want to achieve is:

  1. Click on the adminToolStripMenuItem.
  2. Launch the FrmAdminPassword.
  3. Enter the password and hit Login on FrmAdminPassword.
  4. After hitting Login, close the FrmAdminPassword and enable the controls on StartupForm.

Could someone please help on this? Thanks.

Community
  • 1
  • 1
svb
  • 85
  • 2
  • 10
  • Check out this SO Question here --> http://stackoverflow.com/questions/9152667/check-whether-current-user-is-a-member-of-an-active-directory-group I use this approach at work so users of different security groups can only see/use certain things in my applications – waltmagic May 13 '15 at 22:35

3 Answers3

2

Use ShowDialog() to show your login form. This will stop the execution of code in the startup form until the login form closes

private void adminToolStripMenuItem_Click(object sender, EventArgs e)
{
    // Putting the creation of the form inside a using block allows
    // the automatic closing and disposing of the form when the code
    // reaches the closing brace of the using block.
    using(FrmAdminPassword frmAdminPassword = new FrmAdminPassword())
    {
        // This opens the frmAdminPassword form in modal mode, no 
        // code is executed after the call until you close the 
        // form with DialogResult.OK, DialogResult.Cancel or whatever 
        if(DialogResult.OK == frmAdminPassword.ShowDialog())
        {
            MessageBox.Show("Login executed with success!");
        }
        else
        {
            // Mo password given or form cancelled
            // put here the logic to exit or disable things
        }
    }
}

Now in the Login form OK button click you could execute your logic to validate the password and to allow the closing of the form if the password match

public partial class FrmAdminPassword : Form
{
    private void btnLoginAdmin_Click(object sender, EventArgs e)
    {
        if (mskAdminPassword.Text == "password")
            this.DialogResult = DialogResult.OK;
        else
        {
            MessageBox.Show("Not a valid password");
            this.DialogResult = DialogResult.None;
        }
    }
}

To make this work you need to set the DialogResult property of the Login Form to something different from DialogResult.None. This Will force the Login form to automatically hide (not close) so you can read the DialogResult property on the initial instance of the startup form and decide what to do next. You should also provide some method to exit from the login form with DialogResult.Cancel and stop further processing because the user decided to not give a password.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks, this solved it. however, what should I have searched to have solved this on my own? – svb May 13 '15 at 22:43
  • There are a lot of info on this basic functionality of the windows forms. Probably you should search for Modal DialogBox or something about the form DialogResult property and the effect that setting this property has on the form lifecycle. Also search about the button DialogResult property that is automatically transferred to the form DialogResult property when you click that button. This will full automate the closing of the form with info for the calling client code – Steve May 13 '15 at 22:45
  • since your enclosing the showdialog in an IDisposable, wouldn't it automatically dispose the frmAdminPassword if the DialogResult is say None or cancelled? – svb May 13 '15 at 22:54
  • No DialogResult.None doesn't exits from the login form (ShowDialog()). Any other DialogResult will exit from ShowDialog(). But the form is just hidden not closed or disposed so you could read whatever you need from the instance before code meets the closing brace of the using statement. (this include the native form properties like DialogResult but also your own public property that you have set in the ok button click )event – Steve May 13 '15 at 23:04
1

Try this...

FrmAdminPassword

    private void btnLoginAdmin_Click(object sender, EventArgs e)
    {
        if (mskAdminPassword.Text == "password")
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        else
        {
            MessageBox.Show("Not a valid password");
            this.Close();
        }
     }

StartupForm

        private void adminToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAdminPassword frmAdminPassword = new FrmAdminPassword();
            using(frmAdminPassword)
            {
                if(frmAdminPassword.Show() == System.Windows.Forms.DialogResult.OK)
                {
                    AdminLogin();
                }
            }
        }
waltmagic
  • 631
  • 2
  • 9
  • 22
0

What is happening is that you have two separate copies of the form instantiated. At launch, the startup form is instantiated. During login, you are instantiating a totally new copy. Your login needs to reference the existing startup form.

Marc Johnston
  • 1,276
  • 1
  • 7
  • 16
  • Yes, how do I do that? So far all the examples I've seen are to initiate a new instance. In VB.Net you can just call form1.method1() in a form2 control. – svb May 13 '15 at 22:21
  • One method you can use is to use a global variable and during the construction, you can assign the "this" to that global variable. – Marc Johnston May 13 '15 at 22:26