Reputation: 85
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:
adminToolStripMenuItem
.FrmAdminPassword
.FrmAdminPassword
.FrmAdminPassword
and enable the controls on StartupForm
.Could someone please help on this? Thanks.
Upvotes: 1
Views: 105
Reputation: 216243
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.
Upvotes: 2
Reputation: 642
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();
}
}
}
Upvotes: 1
Reputation: 1286
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.
Upvotes: 0