Click here to Skip to main content
15,886,734 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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



}
}
Posted
Updated 17-Jun-16 22:33pm
Comments
Sergey Alexandrovich Kryukov 18-Jun-16 2:56am    
Your problem is not UI, but understanding the very elementary basics of programming. You need to fix it first.
For example, what does it mean, "if (loginToolStripMenuItem.Enabled == true)"? The type of .Enabled is already Boolean, why comparing it with "true". This is the same as "if (loginToolStripMenuItem.Enabled)...". And so on...
Anyway, the assignment to .Enable property is the solution.
—SA
Member 12590531 19-Jun-16 0:01am    
Do U even know C#.Dont think u know.
Sergey Alexandrovich Kryukov 19-Jun-16 1:43am    
You got me.
—SA

1 solution

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:

C#
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:
C#
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.
 
Share this answer
 
v4
Comments
Member 12590531 19-Jun-16 1:58am    
Hey Now I got It.It was Difficult for me to understand as I Started C# since One Week.
Thanks For You Solution.It Helped Alot.Thank You So Much.
BillWoodruff 19-Jun-16 2:19am    
You're welcome. Sometimes the best way around an issue is to "step around it." In my experience (as a teacher) many people who have some experience with WinForms have never really looked at the code in the Program.cs file and considered how it "works."

Member 12590531 19-Jun-16 3:14am    
Hey BillWoodruff Your solution is best but i got assignment to disable manustrip from login page where firstly mdiparent Form will open and then ill choose to login and after that login will be disabled and home which is another manustip is enabled. he given hint that some "contains" function will be used.please help me.
BillWoodruff 19-Jun-16 8:39am    
Think how impressed your teacher will be when you tell him/her the very valid reasons for not showing the MDIParentForm first ! cheers, Bill
BillWoodruff 19-Jun-16 10:25am    
What you describe here should not be that hard to implement: make the 'MDIParentForm your main form (do not modify the Program.cs file), in the Load Event of the 'MDIParentForm show the 'LogInForm using ShowDialog(this);

Set the 'MainMenuStrip Property of the 'MDIParentForm to your MenuStrip, set the MenuStrip's Visible and Enabled properties to false.

When the LogInForm is closed, interpret the DialogResult returned and do the right thing to set the MenuStrip's Enabled, Visible, properties to true. Or, show another menu based on the DialogResult.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900