Implementing cut-and-paste on a form

  • Thread starter Thread starter michael sorens
  • Start date Start date
M

michael sorens

I have searched through the MSDN documentation but without success for
this very common task: implementing Ctrl-C, Ctrl-V, and Ctrl-X for any of
a number of TextBox controls on a form. Roughly it looks like I want to
set my form's KeyPreview to true, then I can check for the Control key via
something like "if ((Control.ModifierKeys & Keys.Control) =
Keys.Control)..." but then the actual key in e.KeyChar (where e is a
KeyPressEventArgs object) reports the ASCII for the modified character
(i.e. Ctrl-A == 1), so I would have to add an ASCII "A"-1 to be able to
detect "V", etc... All this just seems too low level for the likes of
Visual Studio 2005. Could someone provide enlightenment?
 
I have searched through the MSDN documentation but without success
for this very common task: implementing Ctrl-C, Ctrl-V, and Ctrl-X
for any of a number of TextBox controls on a form. Roughly it looks
like I want to set my form's KeyPreview to true, then I can check
for the Control key via something like "if ((Control.ModifierKeys &
Keys.Control) = Keys.Control)..." but then the actual key in
e.KeyChar (where e is a KeyPressEventArgs object) reports the ASCII
for the modified character (i.e. Ctrl-A == 1), so I would have to
add an ASCII "A"-1 to be able to detect "V", etc... All this just
seems too low level for the likes of Visual Studio 2005. Could
someone provide enlightenment?

This is already built in to TextBox controls, you don't need to do
anything unless you want to change the default action.
 
So it is--thanks for pointing that out. So let me make it a little more
challenging--how about cutting and pasting text into a DataGridView cell?
I know the basic step involved is
"Clipboard.SetDataObject(mvDGV.GetClipboardContent());" My question is
really how do I hook up the keystrokes (Ctrl-C, etc.)?
 
I think there are key press events, and there is some way to check to
see if the ctrl key is being held down.
 
Hi Michael,
Thank you for posting. From your post, my understanding on this issue is:
How to paste a text into a DataGridView cell and how to hook up the
keystrokes such as Ctrl-C in Windows Forms. If I'm off base, please feel
free to let me know.
When a DataGridView is editable, you can use Ctrl-V to paste a text into
the cells of it directly and it doesn't need any additional code. To make
a DataGridView, please set the ReadOnly property of the DataGridView to be
false.
As for how to hook up the keystrokes you can refer to the following. When
a key is pressed while a control has focus, three events will occur
sequentially in general. The three events are KeyDown, KeyPress and KeyUp.
The KeyDown event will occur first and the next is KeyPress event and KeyUp
event is the last one to occur. You can use KeyDown event to hook up the
keystrokes such as Ctrl-C and Ctrl-V. The following is a sample.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true && e.KeyCode == Keys.C)
{
// add your processing code, this is an example
MessageBox.Show("You have pressed ctrl-c!");
}
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true && e.KeyCode == Keys.V)
{
// add your processing code,this is an example
MessageBox.show("You have pressed ctrl-v!");
}
}

Hope it will be helpful to you.
Please let me know if you have any other concerns, or need anything else.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
You indicated that Ctrl-V, Ctrl-C, Ctrl-X should work in a DataGridView
that is not ReadOnly. But that is not correct. I confirmed that ReadOnly
is false, and I can edit the DataGridView by typing, but the cut-and-paste
keystrokes do nothing.
 
Hi Michael,

Thank you for your reply.
Only if a DataGridView is editable and a cell of it has input focus, you
can use cut-and-paste keystrokes to edit the cell.
Please make sure that the cell you want to edit has input focus.
Hope this will be helpful to you.
If you have other concerns or need anything else, please let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
After I read your reply a couple times, I realized the significant phrase:
*input* focus. I was just selecting the cell (is that called "cell
focus"?). Once I clicked again to get to "input focus" then I observed
that cut-and-paste do work, as you indicated. (In the meantime, I went
ahead and implemented a class to allow cut-and-paste with "cell focus".)
 
Hi Michael,

Yes, you are right. Thank you for your reply and the detail feedback on
how you were successful in resolving this issue.
If you have any other questions or concerns, please don't hesitate to
contact us. It is always our pleasure to be of assistance.

Have a nice day!


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
Back
Top