hi there,

i have a form with text boxes and lables and datagrid view,

the user enters data in one text box in the form. wen the user press ctrl+c from the key board how can i make i to copy the value entered in the textbox. and when the user press ctrl+v how can i make the data to be pasted in the selected textbox.

how can i do this in C#.

thankxxxxxx

Recommended Answers

All 8 Replies

Textbox supports Ctrl+C and Ctrl+V by default. Do you mean to copy the text of the textbox without selecting them?

Textbox supports Ctrl+C and Ctrl+V by default. Do you mean to copy the text of the textbox without selecting them?

you said that it is by default in the C#, but it is not when i select a text box and press ctrl+C is dosen't get selected. so how can i do this.
do i have to write a code,

thankxxxx

Ctrl+C and Ctrl+V are supported by the TextBox control without any extra code. But you *do* need to select the contents first. Ctrl+C only copies the highlighted text. Edward often calls the SelectAll() method of the text box on the Enter event to facilitate users' expected behavior.

It also supports Ctrl+A to select all. It also by default supports the shell menu with Select all, copy, cut, and if applicable, paste as well.

It also supports Ctrl+A to select all. It also by default supports the shell menu with Select all, copy, cut, and if applicable, paste as well.

so do u mena that when i run my desktop application when i select a text of a textbox by pressing ctrl+a it will work

it won't work
that what i am saying
how do i get it work
thanxxxxxxxxx

Judith:

I have implemented this functionality by creating a routine like:

private void processKbdCtrlShortcuts(object sender, KeyEventArgs e)
        {
            TextBox t = (TextBox)sender;
            if (e.KeyData == (Keys.C | Keys.Control)) {
                t.Copy();
                e.Handled = true;
            } else if (e.KeyData == (Keys.X | Keys.Control)) {
                t.Cut();
                e.Handled = true;
            } else if (e.KeyData == (Keys.V | Keys.Control)) {
                t.Paste();
                e.Handled = true;
            } else if (e.KeyData == (Keys.A | Keys.Control)) {
                t.SelectAll();
                e.Handled = true;
            } else if (e.KeyData == (Keys.Z | Keys.Control)) {
                t.Undo();
                e.Handled = true;
            }
        }

and then calling it for the key-up event of each textbox control which requires the ability, e.g.:

private void txtWhatever_KeyUp(object sender, KeyEventArgs e)
        {
            processKbdCtrlShortcuts(sender, e);
        }

Judith:

I have implemented this functionality by creating a routine like:

private void processKbdCtrlShortcuts(object sender, KeyEventArgs e)
        {
            TextBox t = (TextBox)sender;
            if (e.KeyData == (Keys.C | Keys.Control)) {
                t.Copy();
                e.Handled = true;
            } else if (e.KeyData == (Keys.X | Keys.Control)) {
                t.Cut();
                e.Handled = true;
            } else if (e.KeyData == (Keys.V | Keys.Control)) {
                t.Paste();
                e.Handled = true;
            } else if (e.KeyData == (Keys.A | Keys.Control)) {
                t.SelectAll();
                e.Handled = true;
            } else if (e.KeyData == (Keys.Z | Keys.Control)) {
                t.Undo();
                e.Handled = true;
            }
        }

and then calling it for the key-up event of each textbox control which requires the ability, e.g.:

private void txtWhatever_KeyUp(object sender, KeyEventArgs e)
        {
            processKbdCtrlShortcuts(sender, e);
        }

ozone_tom:

hey,
when i press ctrl+a it dosen't select the text, but when i select the text from the mose and press ctrl+c it copies the text and when i press ctrl+v it pasts it to another text box
how can i select text from the key board,

and also what is ""txtWhatever_KeyUp"" is it a text name that u declared or is it with in the system

thankxxxx

> when i press ctrl+a it dosen't select the text
Some other keyboard events may be conflicting. Edward has not issues in a sample form.

> what is ""txtWhatever_KeyUp"" is it a text name that u declared or is it with in the system
Both. :) Visual Studio generates event handler names by using the name of the control and the name of the event separated by an underscore: <controlName>_<eventName>. You can call it whatever you want though, it is just another method.

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.