Disable delete button on RichTextBox WF

前端 未结 5 814
无人及你
无人及你 2021-01-14 20:36

I am trying to disable people from deleting a textbox in a richtextbox. The project is using windows form.

Here is the code I have:

    private void         


        
5条回答
  •  渐次进展
    2021-01-14 21:02

    You must add Back key to prevent delete :

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
        {
            e.Handled = true;
            MessageBox.Show("Delete Pressed");
            // Does not show message box...
        }
    }
    

    Edit:

    Non-selectable RichTextBox :

    public class ViewOnlyRichTextBox : System.Windows.Forms.RichTextBox {
        // constants for the message sending
        const int WM_SETFOCUS = 0x0007;
        const int WM_KILLFOCUS = 0x0008;
    
        protected override void WndProc(ref Message m) {
            if(m.Msg == WM_SETFOCUS) m.Msg = WM_KILLFOCUS;
    
            base.WndProc (ref m);
        }
    }
    

提交回复
热议问题