I want to display a message box when any person clicks on the menu item which is not enabled I have tried the following coding but it is not displaying the message box.

Coding:

private void updateFineDetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (updateFineDetailsToolStripMenuItem.Enabled == true)
            {
                frmUpdateFineDetails objUpdateFineDetails = new frmUpdateFineDetails();
                objUpdateFineDetails.MdiParent = this;
                objUpdateFineDetails.Show();
            }
            else if (updateFineDetailsToolStripMenuItem.Enabled == false)
            {
                MessageBox.Show("Unauthorized Person");
            }
        }

By default I have set the enabled status to false and when the form loads I am checking whether the user is administrator, if the user is admin then this menu item will be enabled for all other user who logs into the application the above menu item has to be disabled.

Please note that the above coding does not generate any error, but it does not even display the messagebox as unauthorized person.

Can anybody help me out in performing this task?

Thanks in advance!

Attach/Detach event handler at runtime.
OR
Use Win API.

Disabled menu item doesn't generate Click event, that's why your code isn't executed.

I think in this situation it is better do define a boolean variable in the your Form class.
In constructor you can set it to True for admin and False for others, and do not disable the menu, just set its color to grey. In your updateFineDetailsToolStripMenuItem_Click handler you can change the if statement to this:

if (this.is_admin)
{
    //your admin code
}
else
{
    MessageBox.Show("Unauthorized Person");
}

All the above is quite OK.
Just a remark: when I disable my menu items, I consider my users smart enough to figure out by themselves, they don't have the authorty, or to guess that the program is in a state where it is meaningless to use this menu option. They can't anyway, it is disabled, that is the whole purpose of disabling something.
It feels strange to waste two mouseclicks(one menu, two OK button) on something you know beforehand you can't use it.

commented: excellent practices! +5

2ddanbe

I totally agree with you))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.