简体   繁体   中英

how to get mdi parent control from mdi child form

there is mdi parent form that contain the menustrip initially the menu strip is disabled after the user successfully login from child login form.. after the successful login i need to enabled the mdi parent menu strip

what i have tried so far is something like this but it is not working.

if (username == validUsername && password == validPassword)
    {
             this.mdicontainer.menustrip.enabled = false;
    }

what is way to access the mdiparent control from the child form..

Changing properties of main form from child form is a kind of bad manner for me. How about creating a login dialog form and using it just for requesting login and password? The default login form scenario is quite simple:

  1. You have main form (MainFrm) and you create login form (LoginDlg) with login textbox, password textbox and "Ok" and "Cancel" buttons
  2. At the beginning (for example at MainFrm_Shown) you create new instance of LoginDlg and call it's "ShowDialog()" method
  3. If user clicks "Ok", you receive login name and password in MainFrm from LoginDlg
  4. You analyze login and password somehow in MainFrm
  5. If login and password are correct, you enable your menustrip or whatever
  6. Otherwise you show error message and show DialogFrm again

Here are some helpful links on creating login forms:

Suppose you have a MDI form called with your own custom property called and a menu strip control called : 的MDI表单, 包含您自己的名为的自定义属性和一个名为的菜单控件:

        // Property variable
        private bool _MenuStripEnabled = true;

        // Custom property
        public bool MenuStripEnabled
        {
            get { 
               return _MenuStripEnabled; 
            }
            set { 
               _MenuStripEnabled = value; 
               this.MainMenuStripControl.Enabled = value;
            }
        }

Then to call it from a child form, you just do this:

        (this.MdiParent as MDIMaster).MenuStripEnabled = false;

The part reassigns the generic MdiParent as your specific form type, allowing you access to your custom property. 部分将通用MdiParent重新指定为您的特定表单类型,允许您访问自定义属性。

;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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