Sign in to follow this  

Recommended Posts

i do this in C#: toolStripItem.ShortcutKeys = Keys.H then i get an InvalidEnumException: "The value of argument 'value' (72) is invalid for Enum type 'Keys'." and yes, Keys.H has value 72.. euhh..wtf?

Share this post


Link to post
Share on other sites
What is the data type of toolStripItem.ShortcutKeys? Can you reference "Keys.H" someplace else in code (say, display a console message or message box with the value in it) without tripping an exception? Can you assign a literal 72 to toolStripItem.ShortcutKeys without tripping an exception?

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
What is the data type of toolStripItem.ShortcutKeys?


Well, 'Keys', just like the enum i'm assigning it to.
I can assign other Keys-enums to it just fine.
(not that i've tried them all..)


Quote:
Original post by ApochPiQ
Can you assign a literal 72 to toolStripItem.ShortcutKeys without tripping an exception?


No, in C# i can't assign a number to any Keys-enum without casting it to 'Keys' first.
And i read somewhere, that internally all enums on the .net platform are handled as numbers (makes sense, doesn't it) and the enum-names are looked up when needed.
So passing a (Keys)72 is identical to Keys.H as far as .net is concerned..


Quote:
Original post by ApochPiQ
Can you reference "Keys.H" someplace else in code (say, display a console message or message box with the value in it) without tripping an exception?


I can look at in the debugger, and it's definitely 72 (yes, decimal).
I use it in several places, it's just that at this ShortcutKeys it suddenly gives an InvalidEnumException..
Sure looks like a .NET bug to me..

Share this post


Link to post
Share on other sites
I'm not familiar enough with the semantics of when .Net throws exceptions to know where this may be coming from, but my best guess is that something isn't working the way you assume it is.

As for assigning a literal 72 - that's what I meant. What happens if you actually write the line of code as toolStripItem.ShortcutKeys = (Keys)(72); ?

Where's the declaration of toolStripItem.ShortcutKeys, and the declaration of the Keys enum? Something doesn't add up. I highly doubt .Net is broken here, but that's just my personal gut feeling.

Share this post


Link to post
Share on other sites
I did some fast Google research and found this. Apparently the ToolStripItem's ShortcutKeys is not a vanilla Keys enum, but is specially modified to filter certain keys. This seems to be intentional, by-design behavior.

MSDN confirms this suspicion; ShortcutKeys is implemented as a property, so it's thoroughly possible that they are throwing the exception manually in the setter.

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
I did some fast Google research and found this. Apparently the ToolStripItem's ShortcutKeys is not a vanilla Keys enum, but is specially modified to filter certain keys. This seems to be intentional, by-design behavior.

MSDN confirms this suspicion; ShortcutKeys is implemented as a property, so it's thoroughly possible that they are throwing the exception manually in the setter.


I knew it was a property, and it's pretty obvious that they're doing something different in there then just set a variable..

What i suspect is that they don't allow you to set a shortcut key without control/shift/alt.. probably because if you have a textbox, and you have a shortcut key like 'H' then suddenly you won't be able to type the letter H in the textbox anymore

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this