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.
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.