MenuStrip shortcuts

Hi,
I have a normal form with a menustrip docked at the top and also a richtextbox. The problem is whenever the richtextbox is in focus, some of the menustrip shortcuts don't work (Ctrl+R for example). The richttextbox seems to be "stealing" certain key events so the menustrip doesn't process them. Could someone please tell me how to correct this. Thanks,

Alex



Answer this question

MenuStrip shortcuts

  • Doug Maynard

    Hi, thanks for your reply. I've tried this but it doesn't actually solve anything :(. Maybe this is a bug or is there a different solution
  • Norman Hadfield

    Hi again,

    I seem to have figured out the problem - your example does work just as you explained, so thanks for doing that, but now i realise the problem only occurs when the richtextbox is *readonly* (property ReadOnly set to True). The ShortcutsEnabled property is also a bug I think, because in the description of the property it specifies that the shortcuts are for "the control", not the whole form. It should therefore only disable the control's shortcuts and leave the menustrip's ones alone. Anyway, wondering what you make of all this...
    Thanks for the help.

    Alex


  • Jewels

    weird...it still doesn't work for me ... :/ do you think you could send me an example project of what you are doing... I would very much appreciate it. you can send to myrpg@hotmail.com if you don't mind. thanks

    EDIT: just noticed that Ctrl+C *does* work for me - it just seems to be Ctrl+R.


  • Chris Hays

    I'm seeing the same results with CTRL+R - heres the repro:

    Open a new project, add a RichTextBox and set the Text property to "abcdefg".

    Run the project. Press CTRL+R and the text will be shifted to the right-hand side; this is expected since CTRL+R is the standard shortcut to right-justify text. (you can verify this in Word as well) 

    Stop the project and add a MenuStrip control.

    Add a top-level menu item and set the Text to "&Edit"

    Add a subitem and set its Text to "Right Align"; set the ShortcutKeys property to "CTRL+R"

    Double-click the Right Align submenu and add the following code to the RightAlignToolSTripMenuItem_Click event:

    MsgBox("You pressed CTRL+R")

    Run the project again, and press CTRL+R. The message box is displayed, and the text in the RichTextBox remains left-aligned.

    Stop the app again, and set the ShortcutsEnabled property of the RichTextBox to False.

    Run the app again, and press CTRL+R - nothing happens. Now press ALT+E to activate the Edit menu, then press CTRL+R - the message box is displayed.

     

    At first I thought this might be a bug, but the more I look at it the less convinced I am. If I add another control to the form and shift the focus to that control, the menu shortcut does work. Although it isn't specifically documented, it appears that if ShortcutsEnabled is set to False it disables all shortcut keys on the form while the RichTextBox has focus. This makes sense, because the stated purpose of the ShortcutsEnabled property is to allow you to override the standard shortcuts and implement your own.

    Bottom line - don't duplicate the standard shortcut keys - period.

    - Steve Hoag



  • Osiris

    Alright, so if I override it in the form but still call base.ProcessCommandKey, shouldn't the base method return true if it was handled by it   It isn't though, so that means the culprit is still elsewhere

    Thanks,
    Nick

  • lifangm

    Alex -

    You can set the ShortcutsEnabled property of the RichTextBox to False - RichTextBox has a group of shortcut keys defined by default, including CTRL+R. Note however that this will disable all shortcut keys for the RichTextBox, including Copy and Paste.

    Steve Hoag

    Visual Basic Express



  • AJB123

    I'm seeing the opposite of what you are reporting. If I add a menu subitem and assign it a common ShortKey (i.e. CTRL+C) and then add code to display a MessageBox, it overrides the command from the RichTextBox even when the RichTextBox has focus - the Copy function for the RichTextBox no longer works.

    If I set the EnableShortcuts property to False, the shortcut keys no longer work for either the menu or the RichTextBox. This does seem like it could be a bug.

    The solution is to keep shortcuts enabled for the RichTextBox, and don't duplicate any of the standard shortcut keys unless you are using them to do the exact same thing. For example, you could assign CTRL+C to a Copy menu subitem, but you should never assign that key combination for anything else.

    Does this make sense

    Steve Hoag



  • ajit_patra

    Hi Nick,

    Take a look at the ProcessCmdKey method: http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx

    Steve Hoag

    Visual Basic Express



  • randomwanderer

    Hmm, I'd like to implement shortcut keys such as ctrl-b, ctrl-i, ctrl-u, which I figured wouldn't be too big a deal because those are fairly common shortcuts for bold, italic, and underline.  Apparently ctrl-i is already used though, and this is giving me some trouble.  I've created a class which inherits RichTextBox, but I'm not quite sure what should be overridden with my shortcut (does RichTextBox have an event other than KeyPress/KeyDown/KeyUp that handles these ).

    Thanks for any help.
    Nick Spacek

  • paul002000

    hi - thanks for investigating this matter, after your explanations i can see how these things wouldn't be bugs. it shouldn't really be a problem to use a different shortcut.
    the problem that you did mention as a bug i also think is not problematic, since it provides an easy way to override the Ctrl+R (right-justification) for a textbox/richtextbox (inheriting from textboxbase, as you said). this may be useful somehow anyway, i'm glad this problem's been clarified.

    alex


  • SDerix

    Interesting - setting ReadOnly does indeed keep the menu shortcut from working, and this does seem odd. I also tested this with a TextBox control with the same results - not surprising considering that they both inherit from TextBoxBase.

    The more I think about it though, it probably isn't a bug. Because CTRL+R is defined as a standard shortcut for TextBoxBase, it's reasonable to expect that pressing CTRL+R wouldn't work when the control has focus, since the control is read-only. If you change the menu shortcut to a non-standard shortcut such as ALT+R it works, even when a read-only TextBox has focus.

    As for the ShortcutsEnabled issue, the same is true - even though you disabled shortcuts, CTRL-R is a standard shortcut so you can't override it. I suspect that if you really needed to use CTRL-R as a custom menu shortcut, you could create an inherited RichTextBox and override the standard shortcut keys defined in TextBoxBase - although I haven't tried this.

    What does seem like a bug is the fact that the menu shortcut overrides the TextBox standard CTRL+R shortcut when the TextBox has focus, ShortcutsEnabled is True and ReadOnly is False. I would expect the TextBox shortcut to take precedence since it has the focus.

    Again, as long as you don't try to use the standard shortcut keys, it won't be a problem.

    - Steve Hoag

     



  • gb__

    Ah, through trial and error I had stumbled upon that. :)  The only problem is that in the meantime, I discovered that ctrl-i (which inserts a tab) isn't included in those ShortcutKeys -- it still works even when ShortcutKeys == false!  Any suggestions on how to deal with that

    Thanks,
    Nick

  • Rene Garcia - [MSFT]

    Sounds like Ctrl+i is getting handled at the form level - maybe try overriding it there

  • MenuStrip shortcuts