Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'Shorcutkeys' caused context menu to not show up #328

Closed
galvn opened this issue May 23, 2020 · 3 comments
Closed

Property 'Shorcutkeys' caused context menu to not show up #328

galvn opened this issue May 23, 2020 · 3 comments

Comments

@galvn
Copy link

galvn commented May 23, 2020

Version of SharpShell used: 2.7.2

Related type(s) of SharpShell-Server: : ShellContextMenu

Greetings.
Followed the Count Line project tutorial with a few tweaking and successfully built a dll as .net framework 4.0 class library using VS2019 on a win7 x64 PC.
Loaded it into ServerManager 2.7.2 and have all the info shown in the right panel. Then when I put it to the test in the test shell, the context menu failed to show up.
Eventually I found out that it is the 'Shortcutkeys' property of ToolStripMenuItem causing the issue.
Does any one have any ideas how this happened?

Here is the Sharpshell log roughly translated from Traditional Chinese:

2020-05-24 05:38:00.278Z - ServerManager - error: CopyFilesPath_UTF8: An exception occured building the context menu.
2020-05-24 05:38:00.280Z - ServerManager - error: System.ComponentModel.InvalidEnumArgumentException: Attribute 'value' (88) of Enum type 'Keys' is not valid.
Attribute name: value
   at System.Windows.Forms.ToolStripMenuItem.set_ShortcutKeys(Keys value)
   at CopyFilesPath_UTF8.CopyFilesPath_UTF8.CreateMenu() at K:\Repos\CopyFilesPath_UTF8\CopyFilesPath_UTF8\CopyFilesPath_UTF8.cs: 行 27
   at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Lazy`1.get_Value()
   at SharpShell.SharpContextMenu.SharpContextMenu.SharpShell.Interop.IContextMenu.QueryContextMenu(IntPtr hMenu, UInt32 indexMenu, Int32 idCmdFirst, Int32 idCmdLast, CMF uFlags) at C:\projects\sharpshell\SharpShell\SharpShell\SharpContextMenu\SharpContextMenu.cs: line 76

My CreateMenu() method code snippet as follows:

        protected override ContextMenuStrip CreateMenu()
        {
            //throw new NotImplementedException();
            var menu = new ContextMenuStrip();
            var item_CopyPath = new ToolStripMenuItem
            {
                Text = "SharpShell copy path (X)",
                //ShowShortcutKeys = true,
                ShortcutKeys = Keys.X,
                //ShortcutKeyDisplayString = "lkjsdflksjdfl"
            };
            item_CopyPath.Click += (sender, args) => MainProcess();
            return menu;
        }

Yes I also find out that ShortcutKeyDisplayString and ShowShortcutKeys cause the same problem as well but the reason seems different. The log shows that after querying Context Menu for items it just stoped right there.

@galvn galvn changed the title Attribute 'Shorcutkeys' caused context menu to not show up Property 'Shorcutkeys' caused context menu to not show up May 23, 2020
@dwmkerr
Copy link
Owner

dwmkerr commented Jun 10, 2020

Argghghghg save me from weird undocumented Microsoft features! The issue is the Systems.Windows.Forms code, specifically here:

https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ToolStripManager.cs,869a29ff62a2cace

Basically, when you set a shortcut key via the ShortcutKeys property, it makes a call to IsValidShortcut. And if it is not a valid shortcut, it throws an exception.

It turns out you cannot set the shortcut to something like Keys.Z, because there is no modifier so it has to be something like Keys.Alt | Keys.Z and then it won't crash.

This is terrible code here I've got to say; setting a property value should not call logic like this because it is totally unclear to the caller that there is actually a lot more going on (one would assume setting a property just sets a value, not go through a complex logic chain).

The fact that this isn't even documented is worse!

Anyway, I've noted this in the SharpShell docs here:

https://github.com/dwmkerr/sharpshell/blob/master/docs/context-menu.md#shortcut-keys

So hopefully others won't have this issue in the future!

Thanks for all of the details you added when raising the issue, it made it much easier for me to debug what was going on!

@galvn
Copy link
Author

galvn commented Jun 11, 2020

Got it. Thanks Dave!
Thankfully we still have a backup plan, which is adding a & symbol before the character one wants to assign to, directly in the string of Text property.

For example:

var item_CopyPath = new ToolStripMenuItem
            {
                Text = "SharpShell copy path (&X)"
            };

In the context menu it will be shown as:

SharpShell copy path (X)

And Keys.X become the desired hotkey.

@dwmkerr
Copy link
Owner

dwmkerr commented Jun 12, 2020

Great, glad it's working @galvn, I'll link the docs to this issue in case anyone else has the same challenge in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants