如果 MenuStrip 设置了此快捷键,则快捷键 Ctrl+C Ctrl+V 在文本框中不起作用

发布于 2025-01-03 15:32:39 字数 890 浏览 1 评论 0原文

目标:具有复制和粘贴功能的菜单条,用户应看到快捷键。

MenuStrip block TextBoxes

问题:如果您有一个 MenuStrip 并设置了快捷键,则它们会被菜单“捕获”,但不再被菜单“捕获”文本框。这意味着您不能在文本框中使用 Ctrl+C / V - 只能通过右键单击。如果删除快捷方式,文本框可以正常工作。

这是为什么?如果我不想将条目命名为“Copy______Ctrl+C”,解决方案是什么?

示例项目: http://www.file-upload.net/download- 4098087/MenuBlocksSTRG.zip.html

MSDN 已关闭 ATM 我发现了此链接:

Goal: A Menustrip with Copy and Paste and the user shall see the Shortcut-Keys.

MenuStrip blocks TextBoxes

Problem: If you have a MenuStrip and set the ShortcutKeys the are "catched" by the Menu but no longer by the Textboxes. This means you cannot use Ctrl+C / V in the Textboxes - only by Right-Click. If you remove the Shortcuts the Textboxes work fine.

Why is that? Whats the solution if I dont want to name the Entry "Copy______Ctrl+C"?

Example Project: http://www.file-upload.net/download-4098087/MenuBlocksSTRG.zip.html

MSDN is down ATM i found this links:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

虐人心 2025-01-10 15:32:39

这应该适用于复制,并且您可以以相同的方式处理粘贴:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.C) && textBox1.ContainsFocus)
        {
            Clipboard.SetText(textBox1.SelectedText);
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

This should work for copy, and you can take care of paste in same way:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.C) && textBox1.ContainsFocus)
        {
            Clipboard.SetText(textBox1.SelectedText);
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
朕就是辣么酷 2025-01-10 15:32:39

如果仍然重要,简单的解决方案可能是:
仅显示快捷键文本,如图所示。

Ctrl + V

在文本框中将 ShortcutsEnabled 设置为 true。就这样!

If it still matters, the simple solution might be:
Show only the shortcut keys text, as in the image.

Ctrl + V

In the TextBox set ShortcutsEnabled to true. That's all!

半衬遮猫 2025-01-10 15:32:39

在这些情况下,您可能必须自己处理事情。

简单的例子:

private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
  if (this.ActiveControl is TextBox) {
    Clipboard.SetText(((TextBox)this.ActiveControl).SelectedText);
  } else {
    // do your menu Edit-Copy code here
  }
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e) {
  if (this.ActiveControl is TextBox) {
    ((TextBox)this.ActiveControl).SelectedText = Clipboard.GetText();
  } else {
    // do you menu Edit-Paste code here
  }
}

You probably have to handle things yourself in those cases.

Simple example:

private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
  if (this.ActiveControl is TextBox) {
    Clipboard.SetText(((TextBox)this.ActiveControl).SelectedText);
  } else {
    // do your menu Edit-Copy code here
  }
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e) {
  if (this.ActiveControl is TextBox) {
    ((TextBox)this.ActiveControl).SelectedText = Clipboard.GetText();
  } else {
    // do you menu Edit-Paste code here
  }
}
对你再特殊 2025-01-10 15:32:39

你需要这样的东西吗?

ToolStripMenuItem Quit = new ToolStripMenuItem();
        Quit.Name = "quitToolStripMenuItem";
        Quit.Text = "&Quit";
        Quit.ShortcutKeys = Keys.Alt | Keys.F4;
        Quit.Click += new EventHandler(quitToolStripMenuItem_Click);

You need something like this?

ToolStripMenuItem Quit = new ToolStripMenuItem();
        Quit.Name = "quitToolStripMenuItem";
        Quit.Text = "&Quit";
        Quit.ShortcutKeys = Keys.Alt | Keys.F4;
        Quit.Click += new EventHandler(quitToolStripMenuItem_Click);
对你而言 2025-01-10 15:32:39

对于更复杂的 UI,Ivan Ičin 的解决方案会变得困难。但它可以做得更简单。如果焦点位于文本框上,则返回 false:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.C) && textBox1.ContainsFocus) return false;
    return base.ProcessCmdKey(ref msg, keyData);
}

此外,菜单栏中的复制和粘贴点通常引用某些控件,我们将其称为 editorControl。所以我宁愿逆向测试。这样,可以将其他文本框添加到表单中,而无需更新此功能。

通过返回 false,也很容易处理 Ctrl X 和 Ctrl V。所以最后我使用这个:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (!_editorControl.ContainsFocus && (keyData is (Keys.Control | Keys.C) or (Keys.Control | Keys.V) or (Keys.Control | Keys.X))
    {
        return false;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

Ivan Ičin's solution would become difficult for more complex UIs. But it can be done simpler. Just return false if the focus is on the textbox:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.C) && textBox1.ContainsFocus) return false;
    return base.ProcessCmdKey(ref msg, keyData);
}

Also, the copy and paste points in the menubar normally refer to certain controls, let's call it editorControl. So I would rather inverse the test. This way other textboxes can be added to the form without the need to update this function.

With returning false it is also easy to handle Ctrl X and Ctrl V. So in the end I am using this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (!_editorControl.ContainsFocus && (keyData is (Keys.Control | Keys.C) or (Keys.Control | Keys.V) or (Keys.Control | Keys.X))
    {
        return false;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文