Hi guys,

I writing this vb.net app using an mdi form.
I want to be able to use ctrl+z and ctrl+y in my forms' controls.
This is not working by default, allthough I read that it should.

What could be the problem here? I do not have any code in my project for the keydown function so this can't be it i guess

anyone any ideas?

Dani AI

Generated

As later found, MDI parents can steal common shortcuts before child controls see them. That explains why Ctrl+Z/Ctrl+Y behaved differently in your RichTextBox: the keypress never reached the control. ’s question about which controls is relevant — RichTextBox supports multi-level undo/redo, but only if the keystrokes are delivered to it.

Quick, practical options:

  • Unassign the parent/menu shortcuts. In the designer or at runtime set the offending menu item's ShortcutKeys to Keys.None (see the ToolStripMenuItem ShortcutKeys docs for details: ToolStripMenuItem.ShortcutKeys).
  • Handle the keys in the child form so they always reach the active editor. Override ProcessCmdKey in the child form and call the RichTextBox Undo/Redo when Ctrl+Z/Ctrl+Y are detected. Example:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
    Dim rt = TryCast(Me.ActiveControl, RichTextBox)
    If rt IsNot Nothing Then
        If keyData = (Keys.Control Or Keys.Z) AndAlso rt.CanUndo Then
            rt.Undo()
            Return True
        ElseIf keyData = (Keys.Control Or Keys.Y) AndAlso rt.CanRedo Then
            rt.Redo()
            Return True
        End If
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

Notes and troubleshooting: test with a minimal MDI parent that has no menus to confirm the behavior is menu-related. If you prefer not to override keys, set ShortcutKeys to None or redesign the parent menu so it does not define global shortcuts. References: ProcessCmdKey handling (ProcessCmdKey) and RichTextBox undo/redo methods (, RichTextBox.Redo).

Can u plz tell me which functinality u want to do using CTRL + A & on which control.....so dat i can help u further

I want to use these default key settings on (rich) text boxes
ctrl+z = undo
ctrl+y = redo

right clicking seems to work in a regular textbox, but only 1 undo is possible. I want to have multiple undo's in a (rich) text box.

in case somebody would be interested: after days and days of looking into this issue I have found the solution.
The form I was working with was an MDI form. VS puts some menu items by default, among which undo and redo. There are keyboard shortcuts asigned to these default menu items: this was the problem.
For some reason, the shortcuts do not work in child forms. Removing the menu items from the MDI form fixed the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.