TX Text Control .NET for Windows Forms

Keyboard Shortcuts

Topic

A TextControl on a Windows Forms form responds to a fixed set of keys as soon as it has the focus: the arrow keys and their combinations move the caret, Ctrl+C and Ctrl+V use the clipboard, Ctrl+Enter starts a new page. The tables below list every built-in assignment, for your own reference and for the help of your application. The last section adds shortcuts of your own, such as Ctrl+F for the Find dialog box.

The mouse actions, such as a double-click to select a word, are listed on Selections and Caret Position.

Move the Caret

KeysAction
Left Arrow, Right ArrowMoves the caret one character to the left or right
Up Arrow, Down ArrowMoves the caret one line up or down
Ctrl+Left ArrowMoves the caret to the beginning of the current word
Ctrl+Right ArrowMoves the caret to the beginning of the next word
HomeMoves the caret to the beginning of the line
EndMoves the caret to the end of the line
Ctrl+HomeMoves the caret to the start of the text
Ctrl+EndMoves the caret to the end of the text
Page Up, Page DownScrolls up or down by the height of the visible area minus one line, if there is a vertical scroll bar
Shift+Page Up, Shift+Page DownScrolls the document horizontally
Ctrl+Page Up, Ctrl+Page DownZooms the document in or out

Holding down Shift while pressing a key that moves the caret extends the selection to the new caret position. Shift+Ctrl+End, for example, selects everything from the caret to the end of the text.

Edit Text

KeysAction
Shift+EnterInserts a line break without starting a new paragraph
Ctrl+EnterInserts a page break
Ctrl+Shift+EnterInserts a column break, so that the text continues in the next column of a multi-column section
Ctrl+Shift+SpacebarInserts a non-breaking space
DeleteDeletes the selected text
Ctrl+Backspace, Shift+BackspaceDeletes the previous word
Ctrl+CCopies the selected text to the clipboard
Ctrl+XCopies the selected text to the clipboard and deletes it
Ctrl+VPastes the content of the clipboard in place of the selection
Ctrl+ZUndoes the last action
Ctrl+YRestores the last undone action

Clipboard Operations and Undo and Redo show the methods behind the clipboard and undo keys.

Insert Hyphens and Dashes

KeysInsertsWord may be divided there
Hyphen (-)A hyphen or minus signYes
Ctrl+HyphenAn end-of-line hyphen, which is not visible unless the word is divided at that pointYes
Ctrl+Shift+HyphenA hyphen for compound words that keeps the parts on one lineNo
Ctrl+Minus on the numeric keypadAn en dashYes
Ctrl+Alt+Minus on the numeric keypadAn em dashYes

Automatic hyphenation is described on Spell Checking and Hyphenation.

Keys in Tables

KeysAction
TabSelects the next table cell. In the last cell of a table, it adds a new row at the end of the table.
Shift+TabSelects the previous table cell
Ctrl+TabInserts a tab character in the cell
EnterAt the first input position of a document that begins with a table, inserts an empty line in front of the table

Outside of tables, the Tab key depends on AcceptsTab: with true, it types a tab character; with false, it moves the focus to the next control in the tab order of the form.

Form fields have keys of their own, for example the Spacebar to check a check box or open a drop-down list, and Tab to go to the next field in a protected form. They are listed on Form Fields and Conditional Instructions.

Add Your Own Shortcuts

Every key press raises the KeyDown event that TextControl inherits from the Windows Forms Control class. A handler of this event can call any method of the editor for a key combination and mark the key as handled, so that the editor ignores it. The following code in Form1.cs opens the Find and Replace dialog boxes with Ctrl+F and Ctrl+H, selects all text with Ctrl+A and switches between insert and overwrite mode with the Insert key:

using System;
using System.Windows.Forms;
using TXTextControl;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        textControl1.KeyDown += TextControl1_KeyDown;
    }

    private void TextControl1_KeyDown(object sender, KeyEventArgs e)
    {
        // Ctrl+F: open the built-in Find dialog box
        if (e.Control && !e.Alt && !e.Shift && e.KeyCode == Keys.F)
        {
            textControl1.Find();
            e.Handled = true;
        }
        // Ctrl+H: open the built-in Replace dialog box
        else if (e.Control && !e.Alt && !e.Shift && e.KeyCode == Keys.H)
        {
            textControl1.Replace();
            e.Handled = true;
        }
        // Ctrl+A: select the whole text
        else if (e.Control && !e.Alt && !e.Shift && e.KeyCode == Keys.A)
        {
            textControl1.SelectAll();
            e.Handled = true;
        }
        // Insert: switch between inserting and overwriting text
        else if (e.Modifiers == Keys.None && e.KeyCode == Keys.Insert)
        {
            textControl1.InsertionMode = textControl1.InsertionMode == InsertionMode.Insert
                ? InsertionMode.Overwrite
                : InsertionMode.Insert;
            e.Handled = true;
        }
    }
}

Keep the namespace that Visual Studio created around the class. Run the application and press Ctrl+F: the Find dialog box opens.

The code works as follows:

  • e.Control, e.Alt and e.Shift tell which modifier keys are held down, and e.KeyCode which key was pressed. The checks make sure that Ctrl+Shift+F, for example, doesn't trigger the same command.
  • Find and Replace without parameters open the built-in dialog boxes, and SelectAll selects the whole text.
  • InsertionMode is Insert when typed text is inserted and Overwrite when it replaces the text at the input position.
  • e.Handled = true tells the editor that the key has been processed, so its own handling of the key is skipped.

The TX Words sample of the setup, in %USERPROFILE%\Documents\TX Text Control 34.0.NET for Windows Forms\CSharp\TX Words\Shortcuts, handles Ctrl+O, Ctrl+S and Ctrl+P for opening, saving and printing the same way. The blog article Windows Forms: Removing the Next Word Right to the Input Position on CTRL + DEL adds Ctrl+Delete to delete the next word.

Insert a Line Break With Enter

Some applications, such as an editor for address blocks or table cells, need Enter to start a new line inside the same paragraph, as Shift+Enter does. The KeyPress event, also inherited from Control, receives the Enter key as the carriage return character '\r'. The handler below replaces it with a line break, which is the vertical tab character "\v" in the text of a TextControl:

// In the constructor, after InitializeComponent
textControl1.KeyPress += TextControl1_KeyPress;

private void TextControl1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
    {
        // Skip the new paragraph and insert a line break instead
        e.Handled = true;
        textControl1.Selection.Text = "\v";
    }
}

Selection.Text replaces the selected text, or inserts at the input position when nothing is selected.

Next Steps