0
votes

I have a parent form with a strip menu called topMenu.

I have a child form called "SignIn" and when a user is logged in then I want to disable the topMenu.item.logIn and enable topMenu.item.Logout.

How Can I disable the topMenu of the parent container from the child form?

When a user clicks in the strip menu item "Sign In" the following code is executed.

private void signInToolStripMenuItem_Click(object sender, EventArgs e)
{
    var newMDIChild = new SignIn();

    // Set the Parent Form of the Child window.
    newMDIChild.MdiParent = this;

    newMDIChild.Dock = DockStyle.Fill;
    // Display the new form.
    newMDIChild.Show();

}

after a user type the username and the password the following code is executed

public partial class SignIn : Form
{
    public SignIn()
    {
        InitializeComponent();
    }

    private void btn_signin_Click(object sender, EventArgs e)
    {
        UserInfo.Autherized = true;

        // here I want to disable the sign in menu item
        // and enable the sign out menu item which is located on the parent form
        this.Close();
    }
}
1
the login is a standard form once the login is successful then I want to disable the menu item. Do you mean I can reload the parent form and handle the condition in the load method?Jaylen
from the top strip menu.Jaylen
what code? it is just an item in the menu when a user click it it opens a child form.Jaylen

1 Answers

2
votes

I'd much rather the parent Form get the data it needs from the child Form, rather than the child Form knowing too much about the parent and modifying controls on it.

Add a property to your Login Form that returns whether the user is authenticated. (If UserInfo is public and can be referenced from outside the Login Form, then just use that and skip this step.)

public bool IsUserAuthenticated
{
    get { return UserInfo.Autherized; }
}

Then read that value and take the appropriate action when the Login Form is closed. (This subscribes to the event that executes when the Login Form is closed, and tells it to run some code.)

private void signInToolStripMenuItem_Click(object sender, EventArgs e)
{
    var si = new SignIn();
    si.MdiParent = this;
    si.Dock = DockStyle.Fill;
    si.FormClosed += delegate
                     {
                         if (si.IsUserAuthenticated)
                         {
                             yourLoginItem.Enabled = false;
                             yourLogoutItem.Enabled = true;
                         };
                     }
    si.Show();
}

There are easier ways to do this, like just instantiating a new Login Form and using ShowDialog() instead of setting an MdiParent and docking and all that, but this should work with your existing code.