How to disable a menu item had its sub memu items?

G

Guest

Hi, friends,

I am using C#.net 2005 to create a windows application.

It has menu items, such as File, etc.
Under File, there are more menu items, such as New Files, Working Files, etc.
Under New Files/Working Files, there are more sub menu items, respectively.

All those menu items are enabled at beginning.

It requires that:
If a user clicks on a sub menu of New Files, New Files menu and
all its sub menu items will be disabled;
If a user clicks on a sub menu of Working Files, Working Files
menu and all its sub menu items will be disabled.

In my source code, once a user clicks on a sub menu of New Files, I set
newFilesStripMenuItem.Enabled = false;
hoping that it will disable New Files menu and all its sub menu items
without writting 10 more lines to disable each its sub menu items one by one.

To my surprise, when I moved mouse pointer from Working Files menu (which
was still enabled and automatically displayed all its sub menu items while
moving mouse pointer) to New Files menu, all sub menu items of New Files menu
was displayed!

I was expecting all those sub menu items should NOT be displayed since New
Files menu was DISABLED. (Indeed, if I put mouse pointer on New Files menu
first without moving from Working Files menu, or if I clicked on New Files
menu directly, all those sub menu items could NOT be displayed)

Any ideas? Thanks a lot.
 
W

www.bypsoft.com

Hi Andrew,

I would suggest you to write your own MenuItem class which will handle items
on proper way. Here is a small example which checks always if OwnerItem is
enabled and if it is than it checks it's own state (enabled/disabled). If
OwnerItem is null than it also check it's own state, but if OwnerItem is
disabled than it is also disabled.

namespace TestMenuItem
{
//include support for designer.

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
public partial class TestMenuItem : ToolStripMenuItem
{

public TestMenuItem(): base(){}

public override bool Enabled
{
get
{
if (this.OwnerItem == null)
return CheckVisibility();
else if (this.OwnerItem.Enabled)
return CheckVisibility();
else
return false;

}
}
protected bool CheckVisibility()
{
// please here implement your logic here
}
}
}


Hope this helps.

rgds,
dlm,
www.bypsoft.com
cross-database comparison and synchronisation tools
DBTYP.NET - see differences, for free.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top