Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textbox and I want to select the text inside, press Ctrl+C and copy the text in the clipboard.
I'm using this code here, but the thing is not doing it.
What can it be? Where's the mistake?
Thank you.

What I have tried:

C#
private void textBoxMovieName_KeyDown
                  (object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.C)
    {
        label6.Text = textBoxMovieName.SelectedText; //to preview the result
        //Clipboard.SetText(textBoxMovieName.SelectedText);
    }
}
Posted
Updated 13-Sep-23 6:36am
v5

You don't have to do anything. Copying to the clipboard is already supported by the textbox without doing anything at all.

The code you have will already show the selected text in your label and the TextBox will already copy that text to the clipboard.

The code you posted works, so this is a problem somewhere else.
 
Share this answer
 
Comments
_Q12_ 30-Aug-23 0:06am    
Heh, my bad, somewhere in the code I had this borrowed that took priority.

#region FullScreenKeyboard
//This is win32 plumbing
// 'Can only set the full screen mode when the video is playing >> else 'Catastrophic Error'
private const UInt32 WM_KEYDOWN = 256;
private const UInt32 WM_KEYUP = 257;
public bool PreFilterMessage(ref Message m)
{
if (isSearchActivated == false)
{
Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;


#region WM_KEYUP
if (m.Msg == WM_KEYUP)
{
if (keyCode == Keys.Right)
{
player.Ctlcontrols.currentPosition += 5;
}
if (keyCode == Keys.Left)
{
player.Ctlcontrols.currentPosition -= 5;
}
if (textBoxMovieName.SelectionLength > 0 && keyCode == (Keys.Control & Keys.C))
{
label6.Text = textBoxMovieName.SelectedText;
}
return true;
}
#endregion
The last block of code :
if (textBoxMovieName.SelectionLength > 0 && keyCode == (Keys.Control & Keys.C))
is not working correctly. If Im | instead of &&, it works when selection is >0 but whatever keypress. If I want to get the exact Ctrl+C from this line... is not working at all... any ideas?
Thank you.
Dave Kreskowiak 30-Aug-23 0:42am    
The debugger would have told you what's going on. The value of keycode is NOT going to be (Keys.Control & Keys.C). It's just going to be Keys.C. You have to check for the Control key being held down separately with a call to the Win32 function GetKeyState(VK_CONTROL).

You can find the information you need to import that function on PInvoke.net.
_Q12_ 30-Aug-23 0:54am    
See if you can find something for me, because this is not really my cup of tea. I used this code because otherwise it wouldnt work, so I got forced to use it. I also got help on it. See if you can find that PInvoke call to Control key. I will search for it as well, but you have better chances than me.
Dave Kreskowiak 30-Aug-23 8:09am    
You better make it your cup of tea because I'm not your research assistant, and this skill is a REQUIREMENT to do this job! I told you which site to go to and which function name to search for. If you can't do that very simple thing, hang up your keyboard/mouse and walk away.
Andre Oosthuizen 30-Aug-23 5:02am    
Quote: See if you can find something for me, because this is not really my cup of tea - ROFL. Another Dracula in the making...
Quote:
The last block of code :
C#
if (textBoxMovieName.SelectionLength > 0 && keyCode == (Keys.Control & Keys.C))

is not working correctly. If Im | instead of &&, it works when selection is >0 but whatever keypress. If I want to get the exact Ctrl+C from this line... is not working at all... any ideas?
Always start with the debugger: put a breakpoint on the line, and when it hits look at the value of keyCode. You will find that it doesn't match Keys.Control & Keys.C because & is a binary AND operation: Since Keys.<any key you can press> does not include the Keys.Control bitfield, the result will ALWAYS by zero. You need Keys.Control | Keys.C in order to match both.
 
Share this answer
 
Comments
_Q12_ 30-Aug-23 1:12am    
Well, when I use | instead of &&, it will action C key weather I press or NOT the Control key. I specifically want it with Control+C and not just a simple C key.
I discovered that
if (keyCode == Keys.C)
{
label6.Text = textBoxMovieName.SelectedText;
}
will work for every C key press. All good on this side. But.. when I use this code:
if (keyCode == Keys.Control)
{
label6.Text = textBoxMovieName.SelectedText;
}
it is not working. Like you said, probably returning a 0. So practically when I tested this little last code, teoretically should have actioned when I press the Control Key ! And it did nothing. So the Control Key is the key to the solution !
_Q12_ 30-Aug-23 1:23am    
I discovered that its another key for Control Key and is named ControlKey !
if (keyCode == (Keys.ControlKey & Keys.C))
{
label6.Text = textBoxMovieName.SelectedText;
}
So there are 2, Keys.Control and Keys.ControlKey - but none are working !
_Q12_ 30-Aug-23 1:30am    
---
I tested with
if (keyCode == Keys.ControlKey)
{
label6.Text = textBoxMovieName.SelectedText;
}
So, this way, it fired when I press the Control Key
I also debug it as well.
but, it is still not working when is in this form:
if (keyCode == Keys.ControlKey & keyCode == Keys.C)
_Q12_ 30-Aug-23 1:55am    
if (keyCode == Keys.ControlKey & keyCode == Keys.C) -doesnt work
OriginalGriff 30-Aug-23 1:58am    
Read what I said: "You need Keys.Control | Keys.C in order to match both"
& is a binary AND operator: it only has 1 bits in locations where they are 1 in BOTH operands.

This is basic stuff - you should know all this long before you get to GUI code!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900