TX Words: Shortcuts

Using shortcuts is a useful way to access word processing functions by using key combinations. Some shortcuts (like Ctrl+Z for undo, Ctrl+Y for redo,Ctrl+C for copy, Ctrl+X for cut and Ctrl+V for paste) are built-in. This sample shows how to implement a user defined shortcut behavior by using the TextControl.KeyDown event.

The source code for this example is contained in the following directories:

  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for Windows Forms\CSharp\TX Words\Shortcuts
  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for Windows Forms\VB.NET\TX Words\Shortcuts

Using the Sample

This sample implements the following shortcuts:

  • Ctrl+Insert: Toggles the TextControl insertion mode between Insert and Overwrite.
  • Ctrl+A: Selects all text in the TextControl.
  • Ctrl+S: Save the changes of active document.
  • Ctrl+O: Invokes the built-in TextControl load dialog to open a document.
  • Ctrl+F: Invokes the built-in TextControl find dialog.
  • Ctrl+P: Invokes the built-in TextControl print dialog.

To check the implementation add or load some text and apply the defined shortcuts.

The Code Behind

As mentioned before, the shortcuts are handled by a TextControl.KeyDown event handler:

private void TextControl_KeyDown(object sender, KeyEventArgs e) {
        switch (e.KeyCode) {
                case Keys.Insert:   // Toggle insertion mode
                        if (e.Control || e.Alt || e.Shift) { break; }
                        ToggleInsertionMode();
                        break;

                case Keys.A:        // Ctrl + A: Select all
                        if (!e.Control || e.Alt || e.Shift) { break; }
                        m_txTextControl.SelectAll();
                        break;

                case Keys.S:        // Ctrl + S: Save
                        if (!e.Control || e.Alt || e.Shift) { break; }
                        Save(m_strActiveDocumentPath);
                        break;

                case Keys.O:        // Ctrl + O: Open
                        if (!e.Control || e.Alt || e.Shift) { break; }
                        Open();
                        break;

                case Keys.F:        // Ctrl + F: Search
                        if (!e.Control || e.Alt || e.Shift) { break; }
                        m_txTextControl.Find();
                        break;

                case Keys.P:        // Ctrl + P: Print
                        if (!e.Control || e.Alt || e.Shift) { break; }
                        if (m_txTextControl.CanPrint) {
                                m_txTextControl.Print(m_strActiveDocumentName);
                        }
                        break;
        }
}
Private Sub TextControl_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        Select Case e.KeyCode
                Case Keys.Insert   ' Toggle insertion mode
                        If e.Control OrElse e.Alt OrElse e.Shift Then
                                Exit Select
                        End If
                        ToggleInsertionMode()

                Case Keys.A        ' Ctrl + A: Select all
                        If Not e.Control OrElse e.Alt OrElse e.Shift Then
                                Exit Select
                        End If
                        m_txTextControl.SelectAll()

                Case Keys.S        ' Ctrl + S: Save
                        If Not e.Control OrElse e.Alt OrElse e.Shift Then
                                Exit Select
                        End If
                        Save(m_strActiveDocumentPath)

                Case Keys.O        ' Ctrl + O: Open
                        If Not e.Control OrElse e.Alt OrElse e.Shift Then
                                Exit Select
                        End If
                        Open()

                Case Keys.F        ' Ctrl + F: Search
                        If Not e.Control OrElse e.Alt OrElse e.Shift Then
                                Exit Select
                        End If
                        m_txTextControl.Find()

                Case Keys.P        ' Ctrl + P: Print
                        If Not e.Control OrElse e.Alt OrElse e.Shift Then
                                Exit Select
                        End If
                        If m_txTextControl.CanPrint Then
                                m_txTextControl.Print(m_strActiveDocumentName)
                        End If
        End Select
End Sub