3

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 Form1_Load(object sender, EventArgs e)
    {

        richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
    }


    void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)8)
        {
            e.Handled = true;
            MessageBox.Show("Try not to delete... write freely and openly");
            //The msgbox shows, but the delete still happens within the form.

        }
    }

Does not show messagebox and does not stop the delete:

    private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.KeyDown += new KeyEventHandler(richTextBox1_KeyDown);
    }
    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Delete)
        {
            e.Handled = true;
            MessageBox.Show("Delete Pressed");
            // Does not show message box...
        }
    }
triunenature
  • 651
  • 2
  • 7
  • 23
  • You only want to prevent delete? It's OK for the user to type into the textbox? – Michael Gunter Jun 07 '13 at 20:21
  • Yea, they need to be able to add, just not delete – triunenature Jun 07 '13 at 20:30
  • Not allowing the user to correct a typing mistake is quite bizarre. – Hans Passant Jun 07 '13 at 21:11
  • Since, through highlighting, any key can be used to delete text, using a KeyDown event probably doesn't make sense. You might want to work with the TextChanged event, and prevent the length of the text from shortening. [Might find more details here](http://stackoverflow.com/questions/2186856/wpf-richtextbox-textchanged-event-how-to-find-deleted-or-inserted-text) – Wilson Jun 07 '13 at 21:30
  • @Hans, it's a free writing exercise. The idea is not to edit not to spell check, but write quickly and freely. – triunenature Jun 09 '13 at 00:09

5 Answers5

1

Per the MSDN documentation on KeyPressEventArgs.KeyChar, you cannot get or set the DELETE key using that event. You will need to use the KeyEventArgs.KeyCode instead, subscribing to the KeyDown and KeyUp events.

Douglas
  • 53,759
  • 13
  • 140
  • 188
1

My solution:

void richTextBox1_TextChanged(object sender, EventArgs e) {
  richTextBox1.SelectAll();
  richTextBox1.SelectionProtected = true;
  richTextBox1.Select(richTextBox1.Text.Length, 0);
}

Side note: yes, this will flicker. Proof of concept only. To avoid the flicker, see How to append text to RichTextBox without scrolling and losing selection?

Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225
1

Instead Of KeyPress event use KeyDown In RichText Box.

try this to prevent from deleting text in RichText Box

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 46)
                e.Handled = true;
        }

If you want to disallow both delete and backspace You Can Change KeyDown Event as follows

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 8 || e.KeyValue == 46)
                e.Handled = true;
        }
Shamseer K
  • 4,964
  • 2
  • 25
  • 43
0

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);
    }
}
SerkanOzvatan
  • 231
  • 1
  • 5
  • Problem - If, say, the user highlights text and hits any key, it will delete it. – Wilson Jun 07 '13 at 21:25
  • You can use a non selectable richTextBox then.I edit the answer. – SerkanOzvatan Jun 07 '13 at 22:01
  • Another problem: your ViewOnlyRichTextBox hides the prompt and in fact user can select text using this way: SHITF + (LEFT or RIGHT arrow), after that types any printable key and the selected text (which looks like normal without background) is removed and replaced by the typed characters. – King King Jun 08 '13 at 10:19
0

My solution is some kind of the combination of SerkanOzvatan's and LarsTech's answers. Here is the code:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
  {
    e.Handled = true;
    MessageBox.Show("Try not to delete... write freely and openly");
    // Does not show message box...
  }
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
   richTextBox1.SelectionProtected = richTextBox1.SelectionLength > 0;
}

It works great :)

And here is another solution of my own which also works great, especially if you want to do with a TextBox (not a RichTextBox), it doesn't have a SelectionProtected, and this is used OK for both TextBox and RichTextBox (just change the class name in the following code accordingly):

public class WritableRichTextBox : RichTextBox
{
    protected override bool ProcessKeyMessage(ref Message m)
    {
        int virtualKey = m.WParam.ToInt32();
        if (SelectionLength > 0 || virtualKey == 0x08 || virtualKey == 0x2e)
        {
            if (virtualKey != 0x25 && virtualKey != 0x26 && virtualKey != 0x27 && virtualKey != 0x28)
                return true;
        }
        return base.ProcessKeyMessage(ref m);
    }
}
King King
  • 61,710
  • 16
  • 105
  • 130