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

前端 未结 6 817
既然无缘
既然无缘 2020-11-27 04:57

Can anybody please suggest how to handle Cut,Copy and Paste events on a Text Box in WinForms using C#?

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 05:31

    Suppose you have a TextBox named textbox1. It sounds like you want to disable the cut, copy and paste functionality of a 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);
        }
    } 
    

提交回复
热议问题