2

In my windows application i have a context menu with a grid the problem is that I want to disable the ToolStripMenuItem in context menu according to the user previlages.How can i do that. i have done like this but it is not working

private void contextMenuStrip_Machine_Opening(object sender, CancelEventArgs e)
{
    toolStripAuthorize.Enabled = INFOpermission.accessAuthorize;
} 

but it is not working

Masoud
  • 8,020
  • 12
  • 62
  • 123
Nighil
  • 4,099
  • 7
  • 30
  • 56
  • 1
    1. Show us more code. 2. When you put a breakpoint after the assignment does toolStripAuthorize have Enabled set to false or true? 3. Is INFOpermission.accessAuthorize set to false when expected? – dzendras Jan 14 '11 at 10:53

1 Answers1

7

You need to set toolStripAuthorize.Enabled to either true or false.

I have no idea what INFOpermission.accessAuthorize is because you didn't show the code that defines that (enum?), but if it's anything other than false, this isn't going to work out like you expect.

I can guarantee that setting the Enabled property of the ToolStripMenuItem that you want to disable to false in the Opening event handler will work. If it's not working for you, you're doing something else wrong, and you need to give us some more information to go on.

If you're stuck, see the sample code here: How to: Handle the ContextMenuStrip Opening Event


EDIT: Armed with new information provided in the comments, I've now isolated the source of the problem. You've assigned the ContextMenuStrip to the RowTemplate of a DataGridView control, and are therefore not able to modify items contained in that context menu in its Opening event handler method.

It turns out that this is a known bug that someone decided was "by design". You can see the original bug report here on Microsoft Connect. The explanation given is that whenever a new row is created based on the RowTemplate (which is how the RowTemplate works), the ContextMenuStrip that you've assigned gets cloned as well. That means the same context menu instance is not used for each row, and whatever properties that you try to set on the original menu items have no effect.

Fortunately, it also gives us a workaround. Like all events, the Opening event passes the actual instance of the ContextMenuStrip that is about to be opened as its sender parameter. This is the context menu whose items you need to modify in order for your alterations to be visible.

So what's the code? It looks like this:

private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
{
    ContextMenuStrip cmnu = (ContextMenuStrip)sender;
    cmnu.Items[1].Enabled = false;
}

Notice, though, that you'll have to reference the individual menu item that you want to modify by its index. This is just the zero-based position of the item in the menu that you want to modify. You can't use the toolStripAuthorize object like you were trying to do before because a new instance of it has been cloned for each new context menu instance.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • it is returning false and value is assining also but it the toolstripmenu is showing – Nighil Jan 14 '11 at 11:35
  • @NIGHIL: The *menu* is still going to open. If you want to cancel the menu opening, set `e.Cancel = true`. Setting the `Enabled` property of a particular item will cause it to appear grayed out (unselectable) in the menu. Which are you trying to do? – Cody Gray - on strike Jan 14 '11 at 11:38
  • @Code Gray: Menu will open but the disabled item is Enabled in the menu the value of INFOpermission.accessAuthorize is false – Nighil Jan 14 '11 at 11:48
  • if (INFOpermission.accessAuthorize == false)contextMenuStrip_Machine.Items.Remove(toolStripAuthorize); toolStripAuthorize is there it is not removed – Nighil Jan 14 '11 at 11:51
  • @NIGHIL: So, the menu item is actually named something different than you posted in your original code? Then just set *that* to disabled. Since I can't see your screen from where I'm sitting, it's difficult to guess at what all of your controls/variables are named. – Cody Gray - on strike Jan 14 '11 at 11:54
  • @Code Gray: i corrected it still not working if (INFOpermission.accessAuthorize == false) contextMenuStrip_Machine.Items.Remove(toolStripAuthorize); toolStripAuthorize is there it is not removed – Nighil Jan 14 '11 at 11:57
  • @NIGHIL: Then, either your computer is possessed with evil spirits, or there's some part of your code I can't see that's causing the problem. Just for fun, I tested this again on my computer by creating a new project, adding a `ContextMenuStrip` with a couple of items, and disabling one in the `Opening` event. Everything worked fine. – Cody Gray - on strike Jan 14 '11 at 12:01
  • @Code Gray: I am assigning context menu to datagrid -> rowtemplate-> contextmenustrip , will that cause any problem ? – Nighil Jan 14 '11 at 12:07
  • @NIGHIL: Yes, apparently. That's the problem because I can't get that to work on my machine either. More information is always the key. Let me debug it for a minute. – Cody Gray - on strike Jan 14 '11 at 12:09