简体   繁体   English

如何使用 C# 禁用文本框上的复制、粘贴和删除功能

[英]how to disable copy, Paste and delete features on a textbox using C#

有人可以建议如何使用 C# 在 WinForms 中的文本框中处理剪切、复制和粘贴事件吗?

在 WinForms 中,禁用文本框上的剪切、复制和粘贴功能的最简单方法是将 ShortcutsEnabled 属性设置为 false。

You'd have to subclass the textbox and then override the WndProc method to intercept the windows messages before the control does.您必须对文本框进行子类化,然后覆盖 WndProc 方法以在控件执行之前拦截 Windows 消息。

Here's an example that illustrates a TextBox that intercepts the WM_PASTE message. 下面是一个示例,它说明了一个拦截 WM_PASTE 消息的 TextBox。

And for reference, here's the definition of the message constants:作为参考,这里是消息常量的定义:

You'd simply ignore the inbound message, like so:您只需忽略入站消息,如下所示:

protected override void WndProc(ref Message m)
{
   if (m.Msg == WM_PASTE || m.Msg == WM_COPY || m.Msg == WM_CUT)
   {
      // ignore input if it was from a keyboard shortcut
      // or a Menu command
   }
   else
   {
      // handle the windows message normally
      base.WndProc(ref m);
   }
}

Suppose you have a TextBox named textbox1 .假设您有一个名为textbox1的 TextBox。 It sounds like you want to disable the cut, copy and paste functionality of a TextBox.听起来您想禁用 TextBox 的剪切、复制和粘贴功能。

Try this quick and dirty proof of concept snippet:试试这个快速而肮脏的概念证明片段:

private void Form1_Load(object sender, EventArgs e)
{
    ContextMenu _blankContextMenu = new ContextMenu();
    textBox1.ContextMenu = _blankContextMenu; 
}


private const Keys CopyKeys = Keys.Control | Keys.C;
private const Keys PasteKeys = Keys.Control | Keys.V;

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if ((keyData == CopyKeys) || (keyData == PasteKeys))
    {
        return true;
    }
    else
    {
        return base.ProcessCmdKey(ref msg, keyData);
    }
} 

To prevent users to copy/paste using the keyboard set ShortcutsEnabled property to false.为防止用户使用键盘进行复制/粘贴,请将ShortcutsEnabled属性设置为 false。 To prevent users to copy/paste from the context menu set ContextMenu property to new ContextMenu().为了防止用户从上下文菜单中复制/粘贴,将ContextMenu属性设置为 new ContextMenu()。

if (copyPasteEnabled) {
   textBox1.ShortcutsEnabled = true;
   textBox1.ContextMenu = null;
} else {
   textBox1.ShortcutsEnabled = false;
   textBox1.ContextMenu = new ContextMenu();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        e.SuppressKeyPress = true;
    }

    if (e.Control == true)
    {
        switch (e.KeyCode)
        {
            case Keys.C:
            case Keys.P:
            case Keys.X:
                e.Handled = true;
                textBox1.SelectionLength = 0;
                break;
        }
    }
}

private void textBox1_Enter(object sender, EventArgs e)
{
    System.Windows.Forms.Clipboard.Clear();
}
int cusorposition = m_TextBox1.SelectionStart;
if (TextBox1.Text[0] == ' ')
{
//Trim Spaces at beginning.
      m_TextBox1.Text = m_TextBox1.Text.TrimStart(' ');
      m_TextBox1.Text = m_TextBox1.Text.TrimEnd(' ');
      m_TextBox1.SelectionStart = cusorposition ;
}

Hi I found a way how to get the current cursor position instead of handling cut, copy and Paste event in a text box named TextBox1.Here in the above I am keeping the backup of current Cursor Position and after trimming the extra spaces from the starting and from end position I am reassigning the current cursor position.嗨,我找到了一种如何获取当前光标位置而不是处理名为 TextBox1 的文本框中的剪切、复制和粘贴事件的方法。并从结束位置我重新分配当前光标位置。

Thanks to all who helped me to fix this problem.感谢所有帮助我解决这个问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM