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

前端 未结 6 708
既然无缘
既然无缘 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:27
    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.

    Thanks to all who helped me to fix this problem.

    0 讨论(0)
  • 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);
        }
    } 
    
    0 讨论(0)
  • 2020-11-27 05:33
    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();
    }
    
    0 讨论(0)
  • 2020-11-27 05:42

    To prevent users to copy/paste using the keyboard set ShortcutsEnabled property to false. To prevent users to copy/paste from the context menu set ContextMenu property to new ContextMenu().

    if (copyPasteEnabled) {
       textBox1.ShortcutsEnabled = true;
       textBox1.ContextMenu = null;
    } else {
       textBox1.ShortcutsEnabled = false;
       textBox1.ContextMenu = new ContextMenu();
    }
    
    0 讨论(0)
  • 2020-11-27 05:50

    You'd have to subclass the textbox and then override the WndProc method to intercept the windows messages before the control does.

    Here's an example that illustrates a TextBox that intercepts the WM_PASTE message.

    And for reference, here's the definition of the message constants:

    • WM_PASTE
    • WM_COPY
    • WM_CUT

    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);
       }
    }
    
    0 讨论(0)
  • 2020-11-27 05:51

    In WinForms, the easiest way to disable cut, copy and paste features on a textbox is to set the ShortcutsEnabled property to false.

    0 讨论(0)
提交回复
热议问题