How do I grey out an option in a context menu

Hi,

Does it make sense to grey out an option, or is it more standard to simply not provide the option in the first place?

Also, when I'd like a menu displayed how can I impose conditions on whether an item will be in the menu or not?

Thanks,
Bob
ba272Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jonorossiCommented:
Right click in the textbox below you will see you have certain options disabled, however right clicking your recycle bin wont have a delete menu item. So it really depends on what you are using it for as to whether to hide or disable the item.

Both the .NET 2.0 MenuStrip and the .NET 1.1 Menu have an Enabled and Visible property for menu items.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ba272Author Commented:
Thanks for straightening me out.  You look at these things all day every day and still ask.

As far as using ContextMenus in C# 1.0, how do I disable a menu item?
jonorossiCommented:
When the user changes selection of something on your form or even just before you display the context menu (if done manually) you can do something like this because each menu item is a seperate control:

contextMenu1_Delete.Enabled = false;

Let me know if you need something different.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

ba272Author Commented:
How do I intercept this call in the event that I use the ContextMenu property of ListView, which handles the right mouse click?
ba272Author Commented:
or sould I do it the manual way?  and handle the rigt mouse-click myself?
jonorossiCommented:
You can change the enabled property with the SelectedChange (can't remember proper name) of the ListView, or you could do it manually. Both ways are just as easy. If you don't want the context menu to show when they right click the blank white space you will have to do it manually.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
You can leave the ContextMenuStrip() property blank and set it at run-time in the MouseDown() event of your ListView.

Something like...

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ListViewItem lvi = listView1.GetItemAt(e.X, e.Y);
                if (lvi != null)
                {
                    // set the ContextMenuStrip property
                    listView1.ContextMenuStrip = contextMenuStrip1;

                    // modify the ContextMenuStrip based on "lvi" here...
                    deleteToolStripMenuItem.Enabled = false;
                }
                else
                {
                    listView1.ContextMenuStrip = null;
                }
            }      
        }
Ravi SinghSenior Software EngineerCommented:
Have you tried handling the ContextMenu's Popup event? Its fired before the context menu is actually displayed, so you can do the relevant validation in that...

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.