如何启用/禁用menustrip项目的侧面mdiparent表单 [英] How to enable /disable menustrip item out side mdiparent form

查看:88
本文介绍了如何启用/禁用menustrip项目的侧面mdiparent表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试但它给出了nullexception错误。

i想要在成功登录后启用homemenustrip。请帮助



我尝试过:



MDIParen.cs: -



private void adminToolStripMenuItem_Click (对象发送者,EventArgs e)// admin是menustrip,因为他们是登录和家庭条。

{

if(loginToolStripMenuItem.Enabled == true)

{

homeToolStripMenuItem.Enabled = false;



}

else

{

homeToolStripMenuItem.Enabled = true;



}

}



}

}



Login.cs: -



公共部分类登录:形式

{

public loginpg()

{

InitializeComponent();

}





private void loginbutton_Click(object sender,EventArgs e )

{

if(usertxt.Text ==arjun&& passwordtxt.Text ==12345)

{

homepg H =新homepg();

H.Show();

this.Hide();

MDIParent1 M = new MDIParent1();

M.Controls [loginToolStripMenuItem]。Enabled = false; / / Nullexception







}

}

解决方案

当你声明MDIParent1 M = new MDIParent1();在你的LogIn表单内的一个方法内:'MDIParent1的实例不存在于LogIn表单的范围之外。



我认为这是一个更好的策略,以确保除非登录有效,否则最终用户永远不会访问主表单MDIParentForm;在这种情况下,不会发生必须禁用menustrip的问题。以下是如何做到这一点:



假设:



1.一个名为'FormMDIParent的表格是登录成功后显示主要表格。



2.登录表单,包含两个TextBox,'tbxPassword和'tbxUserName。还有两个按钮,'btnSubmit和'btnCancel。登录表单'AcceptButton属性设置为'tbxSubmit,登录表单'CancelButton属性设置为'tbxCancel。



修改Program.cs像这样:



 使用系统; 
使用 System.Windows.Forms;

命名空间 YourMDIProject
{
static class 程序
{
/// < 摘要 >
/// 应用程序的主要入口点。
/// < / summary >
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );

if (( new FormLogIn())。ShowDialog()== DialogResult.OK)
{
Application.Run( new MDIParentForm());
}
}
}
}

因此应用程序以模态方式显示LogIn表单;当LogIn表单关闭时,如果DialogResult为'OK',则Application运行一个新的'MDIParentForm实例,它将成为主窗体。



现在使用系统查看'LogInForm:

 中的逻辑; 
使用 System.Windows.Forms;

命名空间 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;
}
其他
{
// < span class =code-comment>现在怎么办?取消?

// 允许用户再次重复#n次?

// 此处无,应用程序将终止
}
}

私有 bool LogInValidator(< span class =code-keyword> string password, string username)
{
// 您的验证函数返回'bool
// 到这里
// ????
}
}

还有另一种方法可以做到这一点,我更喜欢使用它,你可以在那里启动Appl使用'ApplicationContext实例,但我认为这个示例在这种情况下更简单,更合适。



您还可以为'FormClosing事件创建一个EventHandler通过访问该事件处理程序的事件参数中的'CloseReason枚举

this is what i tried but its giving nullexception error.
i want to enable homemenustrip after succesfull login.please help

What I have tried:

MDIParen.cs:-

private void adminToolStripMenuItem_Click(object sender, EventArgs e)//admin is menustrip under that their is login and home strips.
{
if (loginToolStripMenuItem.Enabled == true)
{
homeToolStripMenuItem.Enabled = false;

}
else
{
homeToolStripMenuItem.Enabled = true;

}
}

}
}

Login.cs:-

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


private void loginbutton_Click(object sender, EventArgs e)
{
if (usertxt.Text=="arjun" && passwordtxt.Text=="12345")
{
homepg H = new homepg();
H.Show();
this.Hide();
MDIParent1 M =new MDIParent1();
M.Controls["loginToolStripMenuItem"].Enabled = false;// Nullexception here



}
}

解决方案

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
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [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
            {
                // what now ? cancel ?
                // allow the user to repeat again for #n times ?

                // do "nothing" here and the Application will terminate
            }
        }

        private bool LogInValidator(string password, string username)
        {
            // your validation function that returns a 'bool
            // goes here
            // ????
    }
}

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.


这篇关于如何启用/禁用menustrip项目的侧面mdiparent表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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