我如何在C#中启用和禁用Menustrip? [英] How I Do Menustrip Enable And Disable In C#?

查看:322
本文介绍了我如何在C#中启用和禁用Menustrip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式。



1表格是mdiparent。

1表格是登录表格



登录表单位于mdi父级内部。





mdiparent有menustrip,它有...



菜单叫,

1.登录

2.报告



当你点击登录时它将显示用户名和密码

如果正确

应用程序中的整个菜单将启用给所有用户。



目前报告菜单被禁用,我想在登录正确时启用它。



只需查看以下代码即可。

i have two forms.

1 form is mdiparent.
1 form is login form

login form is inside of mdi parent.


mdiparent has menustrip and it has..

menus called,
1. login
2. Reports

when you clicked login it will show username and password
if its correct
whole menu's in application will be enable to all users.

currently reports menu is disabled and i want to enable that if login is correct.

just go through my following codes.

Form1 main = new Form1();

            main.Controls["Reprt1"].Enabled = true;

            main.Controls["Reprt2"].Enabled = true;

            main.Controls["Reprt3"].Enabled = true;

            main.Controls["Reprt4"].Enabled = false;







错误是这样的?

对象引用未设置为对象的实例。




Error is this?
Object reference not set to an instance of an object.

推荐答案

嗯。

你不要认为它在 现有的 表单实例上启用和禁用menustrip会更有用吗?你知道,用户可以 看到 吗?



目前,你正在创建一个新表格实例并尝试更改它的menustrip控件,假设它们直接附加到Controls集合。首先,赢得了工作,因为你所做的任何改变都会影响新实例,而不是正在显示的版本,其次实际的报表控件包含在实际的MenuStrip对象实例中,而不是直接在表单上表面。



相反,请使用MDIParent表单中的代码(因此影响当前实例)并直接使用控制名称:

Um.
You don;t think it would be more useful to enable and disable the menustrip on the existing form instance? You know, the one the user can see?

At the moment, you are creating a new form instance and trying to change it's menustrip controls, assuming that they are directly attached to the Controls collection. First off, that won;t work, because anything you did change there would affect the new instance, and not the version that is being displayed, and secondly the actual report controls are contained within the actual MenuStrip object instance, not directly on the form surface.

Instead, do the code in the MDIParent form itself (so you affect the current instance) and use teh control name directly:
Reprt1.Enabled - true;

其中Reprt1是控件的名称,如下所示设计师视图属性窗格。



如果您仍然需要在登录表单显示时执行此操作,请使用事件来通知登录已完成的主表单:< a href =http://www.codeproject.com/Tips/548131/Transferring-information-between-two-forms-Part>在两个表格之间传递信息,第2部分:孩子到父母 [ ^ ]

Where "Reprt1" is the Name of the control as shown in the designer view Properties Pane.

If you need to do this while the login form is still displayed, then use an event to signal the main form that login has completed: Transferring information between two forms, Part 2: Child to Parent[^]


这篇关于我如何在C#中启用和禁用Menustrip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆