C# 如何在 Winforms 文本框上允许 CTRL-V(粘贴)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1876663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:34:43  来源:igfitidea点击:

How do I allow CTRL-V (Paste) on a Winforms Textbox?

c#.netwinforms

提问by Neil N

I have several textboxes on a windows form.

我在 Windows 窗体上有几个文本框。

I can't paste text into any of them using CTRL-V, though I can still right click and select paste. This is pretty annoying.

我无法使用 CTRL-V 将文本粘贴到其中任何一个中,但我仍然可以右键单击并选择粘贴。这很烦人。

I have tried this with the form's KeyPreview as both true and false. TextBox.ShortcutsEnabled is also true.

我已经用表单的 KeyPreview 尝试了这个,无论是对还是错。TextBox.ShortcutsEnabled 也是如此。

采纳答案by Jeremy McGee

Check to see if you have a menu on the form with a shortcut for Ctrl-V.

检查表单上是否有带有 Ctrl-V 快捷方式的菜单。

回答by Josh Stodola

The code you posted has nothing to do with your Ctrl + V problem, that is for certain. Not much else I can tell you unless you post some more code.

您发布的代码与您的 Ctrl + V 问题无关,这是肯定的。除非你发布更多代码,否则我不能告诉你更多。

Special code should not be needed for Ctrl + V, but one guess I have is to make sure you have YourTextBoxId.ShortcutsEnabledset to True.

不应需要特殊的代码,按Ctrl + V,但一想我已经是确保你已经YourTextBoxId.ShortcutsEnabled设置True

回答by Webleeuw

The following code should help:

以下代码应该会有所帮助:

private void textBox1_KeyUp(object sender, KeyEventArgs e) {
    if (e.KeyData == (Keys.Control | Keys.V))
        (sender as TextBox).Paste();
}

回答by PunkConservative

Yeah.. I know this is answered but I thought I'd throw my 2cents in just for fun. I also had a similar problem. Setting the TextBox.ShortcutsEnabled value to True did nothing for me. I was surprised to see the note left by Microsoft here: http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.shortcutsenabled.aspxregarding this issue. Quite interesting to say the least.

是的..我知道这是回答,但我想我会为了好玩而投入我的 2 美分。我也有类似的问题。将 TextBox.ShortcutsEnabled 值设置为 True 对我没有任何作用。我很惊讶地看到微软在这里留下的注释:http: //msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.shortcutsenabled.aspx关于这个问题。至少可以说很有趣。

Given that, I just implemented the functionality through the key even handlers as indicated in the post by Webleeuw.

鉴于此,我只是通过 Webleeuw 的帖子中指出的关键偶数处理程序实现了该功能。

回答by sandeep

I was also going thru with the same problem. after a lot of googling finally I found the solution. It is because in the aplliciation ctrl+v shortcut was already defined(Edit menu-> Paste). After removing this...it work fine for me....Hope that it helps....

我也遇到了同样的问题。经过大量的谷歌搜索,我终于找到了解决方案。这是因为在应用程序中已经定义了 ctrl+v 快捷方式(编辑菜单-> 粘贴)。删除它后......它对我来说很好......希望它有帮助......

回答by syarbas

The TextBox control does not support the CTRL+Ashortcut key when the Multiline property value is true.

当 Multiline 属性值为 true 时,TextBox 控件不支持CTRL+A快捷键。

回答by Swapnil

You can comment already assigned same shortcut to other control, from designer.cs/.vb form of your form. In my case I solved it for menu in following way

您可以从表单的designer.cs/.vb 表单中注释已分配给其他控件的相同快捷方式。在我的情况下,我通过以下方式解决了菜单

//this.copyToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)
((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C)));

回答by Robbie

Go to properties for the control:

转到控件的属性:

Properties>Behaviour>Shortcuts Enabled = true

属性>行为>快捷方式已启用 = true

job done - will now accept default windows shortcut strokes for this control

工作完成 - 现在将接受此控件的默认 Windows 快捷方式