2

If I try to press CTRL+A (to select all the text in the textbox) it makes the windows error noise.

If I try to press CTRL+Backspace (to delete the last word instead of the last character) it just inserts the windows "Unknown character" thing (it looks like a rectangle).

This is just a textbox, I haven't changed any of the properties.

Jon
  • 2,566
  • 6
  • 32
  • 52
  • 1
    Please consider tagging with either [tag:winforms] or [tag:wpf]... or does it happen on both platforms? – Mathieu Guindon Aug 31 '13 at 00:23
  • 2
    I can't reproduce this. You need to [edit] to provide a lot more detail, because as it is there's no information here. – Ken White Aug 31 '13 at 00:26
  • @KenWhite I don't know what else to include... CTRL+A doesn't select any text and makes the default windows error noise, CTRL+Backspace makes the unknown character. – Jon Aug 31 '13 at 00:29
  • I can't reproduce this with Ctrl+A. (I just tried with a new app in VS2012). Ctrl+Backspace for word deletion is not a default behavior for WinForms; you need to implement that yourself via [code](http://stackoverflow.com/questions/1124639/winforms-textbox-using-ctrl-backspace-to-delete-whole-word?rq=1). You'll need to provide a whole lot more information here in order for us to help. – Ken White Aug 31 '13 at 00:30
  • 6
    You forgot to mention that you have the Multiline property set to True. And yes, Ctrl+A doesn't actually work in that mode, you have to implement it yourself. Not hard with a KeyDown event handler. – Hans Passant Aug 31 '13 at 00:30
  • @HansPassant Do you know why they don't work with multiline set to true? – Jon Aug 31 '13 at 00:31
  • 3
    It is because it is [documented](http://msdn.microsoft.com/en-us/library/12w624ff.aspx) to work that way. – Hans Passant Aug 31 '13 at 00:37
  • 1
    I understand, I'm just curious as to why it doesn't (if you know) – Jon Aug 31 '13 at 00:38
  • I suspect usability testers made the point that if there is a lot of text its not desired for the selectionstart/carot to changes lines. But as Hans said its easy enough to code in yourself. – Jeremy Thompson Aug 31 '13 at 02:02

1 Answers1

6

As Hans Passant said

Ctrl+A doesn't actually work in that mode, you have to implement it yourself. Not hard with a KeyDown event handler.

To Implement what you want, you can use the KeyDown event of the textbox

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control)
        {
            if (e.KeyCode == Keys.A)
            {
                textBox1.SelectAll();
            }
            if (e.KeyCode == Keys.Back)
            {
                e.SuppressKeyPress = true;
                int selStart = textBox1.SelectionStart;
                while (selStart > 0 && textBox1.Text.Substring(selStart - 1, 1) == " ")
                {
                    selStart--;
                }
                int prevSpacePos = -1;
                if (selStart != 0)
                {
                    prevSpacePos = textBox1.Text.LastIndexOf(' ', selStart - 1);
                }
                textBox1.Select(prevSpacePos + 1, textBox1.SelectionStart - prevSpacePos - 1);
                textBox1.SelectedText = "";
            }
        }
    }