Key Handling Problem

  • Thread starter Anthony Peterson
  • Start date
A

Anthony Peterson

I have written an MDI windows application that handles key presses to
do things like make text bold, and in a previous version successfully
used cut, copy and paste with a RichTextBox. However, at some point in
time, just the Cut, Copy and Paste stopped working. I was trying to
set it up so I could implement the same operations in the datagrid
that is also on my child form page. Originally I thought I had just
failed to correctly implement key handling for a Datagrid but as it
turned out, my rich text box also no can not process cut, copy and
paste.

After trying some things, I've learned:

Cntrl-A works in my datagrid and my text box just fine. But I think in
the Datagrid it uses default behavior.

Cntrl-B, I and U work in my textbox using my key handler event
routines.

Cntrl-C, X and V do not work at all now.


I'm using a boolean in my child form class to print Cntrl-I from
acting like a tab, but also allow normal tab to work. So that too
effects my handlers.

I also have small helper methods that actually implement the bold,
ital, underline and the copying, cutting, pasting. I've verified
through debugging that the code path never even gets to the copy/cut/
paste methods..

My key handling code:

internal void Entry_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
notes.HandleControlCodes(e, this);
TabEnabled = false;
}
}

private void Entry_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control)
TabEnabled = true;
}

// Tab, Cntrl-I workaround
private void richTextBox1_KeyPress(object sender,
KeyPressEventArgs e)
{
if (TabEnabled)
return;

if (e.KeyChar == ((char)9))
e.Handled = true;
}

// In my notes class (which handles the richtextbox)
public void HandleControlCodes(KeyEventArgs e, Entry en)
{
if (e.KeyCode == Keys.A)
box.SelectAll();
else if (e.KeyCode == Keys.C)
box.Copy();
else if (e.KeyCode == Keys.X)
box.Cut();
else if (e.KeyCode == Keys.V)
box.Paste();
else if (e.KeyCode == Keys.B || e.KeyCode == Keys.I ||
e.KeyCode == Keys.U)
UpdateFontStyle(e.KeyCode == Keys.B, e.KeyCode ==
Keys.I, e.KeyCode == Keys.U);
else if (e.KeyCode == Keys.S)
en.saveToolStripMenuItem_Click(null, null);
}

I could use any insight, as I've tried to walk through and debug this,
but it is hard since I need to test the debugging response to Cntr-C,
X,V but I can't hold control down while tracing through the first
handling pass that occurs when I press just control. So I have only
been able to set a checkpoint on points like box.Copy that never got
reached.
 
A

Anthony Peterson

I have written an MDI windows application that handles key presses to
do things like make text bold, and in a previous version successfully
used cut, copy and paste with a RichTextBox. However, at some point in
time, just the Cut, Copy and Paste stopped working. I was trying to
set it up so I could implement the same operations in the datagrid
that is also on my child form page. Originally I thought I had just
failed to correctly implement key handling for a Datagrid but as its

I have found the solution, but wanted to post it, in case someone else
runs into this. The problem was created by the default behavior of
Visual Studio's MDI Parent template which can be accessed in the
Create New Item. There's nothing wrong with the template, but in my
case, the application I have been working on made an abrupt transition
from SDI to MDI. Previous to this change, cut copy and paste worked,
because the menu items for them had been manually created and I did
not add in shortcut keys immediately, but implemented them through my
key handler instead. My current child form was once a SDI form with a
menubar and all, and when I made it a child I made QCMaster, by the
MDI Parent template, which added shortcut keys to the menu items for
cut, copy, paste. I assumed, when I looked at them before, that they
would feed into my menu item click commands, but it seems somehow that
didn't happen, and the control-C, X and V were processed by the menu
items and never reached my key handler. I implemented handlers for the
cut, et all Tooltip items, but I forgot the menu items and as a result
nothing happened when I entered the shortcuts. I have removed the
shortcut keys from those three and functioning returned to normal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top