When you declare MDIParent1 M =new MDIParent1(); inside a method inside your LogIn Form: that instance of 'MDIParent1 does not exist outside the scope of the LogIn Form.
I think it's a better strategty to make sure the end-user never "gets to" the main form, the MDIParentForm, unless the log-in is valid; in that case, the issue of having to disable the menustrip does not occur. Here's how to do that:
Assuming:
1. a Form named 'FormMDIParent which is the "main form"to be shown after a successful log-in.
2. a log-in Form with two TextBoxes, 'tbxPassword, and 'tbxUserName. And two Buttons, 'btnSubmit, and 'btnCancel. The log-in Form 'AcceptButton Property is set to 'tbxSubmit, and the log-in Form 'CancelButton Property is set to 'tbxCancel.
Modify the Program.cs like this:
using System;
using System.Windows.Forms;
namespace YourMDIProject
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if ((new FormLogIn()).ShowDialog() == DialogResult.OK)
{
Application.Run(new MDIParentForm());
}
}
}
}
So the Application shows the LogIn Form modally; when the LogIn form is closed, if the DialogResult is 'OK, then the Application Runs a new instance of 'MDIParentForm, which becomes the "main form."
Now look at the logic in the 'LogInForm:
using System;
using System.Windows.Forms;
namespace YourMDIProject
{
public partial class FormLogIn : Form
{
public FormLogIn()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if (LogInValidator(tbxPassword.Text, tbxUserName.Text))
{
this.DialogResult = DialogResult.OK;
}
else
{
}
}
private bool LogInValidator(string password, string username)
{
}
}
There is another way to do this, which I prefer to use, where you start the Application with an 'ApplicationContext instance, but I think this example is simpler, and more appropriate, in this context.
You can also create an EventHandler for the 'FormClosing Event of the LogIn form, and in that code get more detailed information about exactly why the LogIn form is being closed by accessing the 'CloseReason enumeration in the event-arguments for that event-handler.