简体   繁体   English

如何从MDI子项禁用功能区菜单项

[英]How to disable a strip menu item from an MDI child

I have a parent form with a strip menu called topMenu . 我有一个父窗体带有一个名为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 . 我有一个名为“ SignIn”的子表单,当用户登录时,我想禁用topMenu.item.logIn并启用topMenu.item.Logout

How Can I disable the topMenu of the parent container from the child form? 如何从子窗体禁用父容器的topMenu

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();
    }
}

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.) (如果 UserInfo 是公开的,并且可以从“登录表单”之外引用,则只需使用它并跳过此步骤。)

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. 有更简单的方法可以做到这一点,例如仅实例化一个新的Login Form并使用ShowDialog()而不是设置MdiParent和停靠等等,但这应该可以与您现有的代码一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM