Facilitate Use of Special Characters in a Visual Basic Text Editor Application

Introduction

This article describes a simple approach to implementing special character use in a Visual Basic based text editor. If your OS is configured for English and you’d like to leave it like that, and if you have not quite memorized all of the key code values required to insert all of the special characters you might use in writing; or if you work from a laptop without a number pad, then this article will offer an alternative approach and may be of interest.


Test-Application-in-VB.NET.gif

Figure 1: Test Application in Use

Character-Selection-in-VB.NET.gif

Figure 2: Character Selection from a Context Menu

Rather than remembering to lock the number pad and to type things like ALT+0192 or ALT+134, I found it easier to provide an interface in the form of a context menu. To handle case differences, I set it up to insert upper case versions of the characters if I use Shift+F1 to open the menu or lower case versions of the characters if I open the menu with F1 without the shift key. The lower case version is also available using a right click and if the user holds the shift key when selecting from the right-click context menu, the character will also be written in upper case. You won’t win any speed typing awards going this route but it is less error prone than keying the codes and will work for you even if you don’t have a number pad on your keyboard as is the case with my laptop.

I built the example on top of a fleshed out rich text editor that is also described and available on this site.

Getting Started

The solution contains a developed word processor built around the rich text box control; that much of the code will not be described herein but a description is available on this site in an earlier article. This article will only address that part of the code used to handle the context menu to do the character inserts.

Solution-Explorer-in-VB.NET.gif

Figure 3: Solution Explorer with the Project Visible

All of the code discussed in this article is contained in frmMain.vb. As a delta from the original editor, the main form was modified by the addition of an insert menu which contains most of the French Accent Marks and through the addition of a context menu with the same characters made available by the new insert menu. A similar approach could be used to allow the user to insert other special characters such as mathematical equation symbols or any other special character that might be of interest to the user.

Code: Main Form (frmMain.vb)

This class provides all of the functionality used in this example to allow the user to insert special characters. The bulk of the code in this class will not be discussed; only those parts that pertain to the placement of special characters into the document.

The first part of the code to depart from the original version of the Visual Basic based word processor project is the declarations region of the main form. In this section, two new variables are declared, the first is a point used to maintain awareness as to the physical location of the mouse (which is later used to decide where to show the context menu) and the second is a string variable entitled "mode" which is used to maintain awareness as to whether or not to use upper case or lower case when inserting the character.

#Region "Declarations"

    Private currentFile As String
    Private checkPrint As Integer
    Private pLocation As Point
    Private mode As String

#End Region

The next new part of the application is used to insert the French accent marks into the active document. The following event handlers and these are the handlers for the new insert menu. All work the same and when called, each will insert the requested accent mark into the document at the position of the cursor.

Region "Insert Menu"
 
    ''' <summary>
    ''' Accent Grave - à
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã ToolStripMenuItem.Click
        rtbDoc.SelectedText = "à"
    End Sub

    ''' <summary>
    ''' Accent Grave - è
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã¨ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã¨ToolStripMenuItem.Click
        rtbDoc.SelectedText = "è"
    End Sub

    ''' <summary>
    ''' Accent Grave - ù
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã¹ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã¹ToolStripMenuItem.Click
        rtbDoc.SelectedText = "ù"
    End Sub

    ''' <summary>
    ''' Accent Aigu - é
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã©ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã©ToolStripMenuItem.Click
        rtbDoc.SelectedText = "é"
    End Sub

    ''' <summary>
    ''' Accent circonflexe - â
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã¢ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã¢ToolStripMenuItem.Click
        rtbDoc.SelectedText = "â"
    End Sub

    ''' <summary>
    ''' Accent circonflexe - ê
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ÃªToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles ÃªToolStripMenuItem.Click
        rtbDoc.SelectedText = "ê"
    End Sub

    ''' <summary>
    ''' Accent circonflexe - î
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã®ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã®ToolStripMenuItem.Click
        rtbDoc.SelectedText = "î"
    End Sub

    ''' <summary>
    ''' Accent circonflexe - ô
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã´ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã´ToolStripMenuItem.Click
        rtbDoc.SelectedText = "ô"
    End Sub

    ''' <summary>
    ''' Accent circonflexe - û
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã»ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã»ToolStripMenuItem.Click
        rtbDoc.SelectedText = "û"
    End Sub

    ''' <summary>
    ''' Accent tréma - ë
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã«ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã«ToolStripMenuItem.Click
        rtbDoc.SelectedText = "ë"
    End Sub

    ''' <summary>
    ''' Accent tréma - ï
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã¯ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã¯ToolStripMenuItem.Click
        rtbDoc.SelectedText = "ï"
    End Sub

    ''' <summary>
    ''' Accent tréma - ü
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã¼ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã¼ToolStripMenuItem.Click
        rtbDoc.SelectedText = "ü"
    End Sub

    ''' <summary>
    ''' Accent Cedille - ç
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Ã§ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal 
    e As System.EventArgs) Handles Ã§ToolStripMenuItem.Click
        rtbDoc.SelectedText = "ç"
    End Sub

    ''' <summary>
    ''' Open quote
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub toolStripMenuItem10_Click(ByVal sender As System.Object, 
    ByVal e As System.EventArgs) Handles toolStripMenuItem10.Click
        rtbDoc.SelectedText = "«"
    End Sub

    ''' <summary>
    ''' Close quote
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub toolStripMenuItem11_Click(ByVal sender As System.Object, 
    ByVal e As System.EventArgs) Handles toolStripMenuItem11.Click=
        rtbDoc.SelectedText = "»"
    End Sub

#End Region

Handling the context menu is not significantly different; the primary difference being that the use of the shift key when selecting F1 will insert the characters in upper case (whereas the use of F1 alone or using the right mouse click event to launch the menu will insert characters in lower case). To support this functionality, the rich text box key down event is used to both define the location where the menu will appear and also to set the mode string variable to either UC or LC for upper or lower case. The mouse move event is used to update the Point pLocation variable to a screen value; that point is used by the key down handler to set the position of the context menu.

The code related to the context menu is contained in the following region:

#Region "Context Menu"

    Private Sub rtbDoc_KeyDown(ByVal sender As ObjectByVal e As 
    KeyEventArgs) Handles rtbDoc.KeyDown
 
        If (e.KeyCode = Keys.F1) Then
            contextMenuStrip1.Show(pLocation)
        End If
        If (e.Shift = TrueThen
            mode = "UC"
        Else
            mode = "LC"
        End If

     End Sub

    Private Sub rtbDoc_MouseMove(ByVal sender As ObjectByVal e As 
    MouseEventArgs) Handles rtbDoc.MouseMove

        Dim ctrl As Control
        ctrl = DirectCast(sender, Control)
        pLocation = ctrl.PointToScreen(New Point(e.X, e.Y))

     End Sub

    Private Sub toolStripMenuItem9_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem9.Click

         If (mode = "LC"Then
            rtbDoc.SelectedText = "à"
        Else
            rtbDoc.SelectedText = "à".ToUpper()
        End If

     End Sub

    Private Sub toolStripMenuItem12_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem12.Click
 
        If (mode = "LC"Then
            rtbDoc.SelectedText = "è"
        Else
            rtbDoc.SelectedText = "è".ToUpper()
       End If

     End Sub

    Private Sub toolStripMenuItem13_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem13.Click

        If (mode = "LC"Then
            rtbDoc.SelectedText = "ù"
        Else
            rtbDoc.SelectedText = "ù".ToUpper()
        End If

     End Sub

    Private Sub toolStripMenuItem14_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem14.Click

        If (mode = "LC"Then
            rtbDoc.SelectedText = "é"
        Else
            rtbDoc.SelectedText = "é".ToUpper()
        End If

     End Sub 

    Private Sub toolStripMenuItem15_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem15.Click

        If (mode = "LC"Then
            rtbDoc.SelectedText = "â"
        Else
            rtbDoc.SelectedText = "â".ToUpper()
        End If

     End Sub

    Private Sub toolStripMenuItem16_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem16.Click

        If (mode = "LC"Then
            rtbDoc.SelectedText = "ê"
        Else
            rtbDoc.SelectedText = "ê".ToUpper()
        End If

     End Sub

    Private Sub toolStripMenuItem17_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem17.Click

         If (mode = "LC"Then
            rtbDoc.SelectedText = "î"
        Else
            rtbDoc.SelectedText = "î".ToUpper()
        End If

     End Sub

    Private Sub toolStripMenuItem18_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem18.Click

        If (mode = "LC"Then
            rtbDoc.SelectedText = "ô"
        Else
            rtbDoc.SelectedText = "ô".ToUpper()
        End If

    End Sub

    Private Sub toolStripMenuItem19_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem19.Click

        If (mode = "LC"Then
            rtbDoc.SelectedText = "û"
        Else
            rtbDoc.SelectedText = "û".ToUpper()
        End If

    End Sub

    Private Sub toolStripMenuItem20_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem20.Click

        If (mode = "LC"Then
            rtbDoc.SelectedText = "ë"
        Else
            rtbDoc.SelectedText = "ë".ToUpper()
        End If

    End Sub

    Private Sub toolStripMenuItem21_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem21.Click

         If (mode = "LC"Then
            rtbDoc.SelectedText = "ï"
        Else
            rtbDoc.SelectedText = "ï".ToUpper()
        End If

    End Sub

    Private Sub toolStripMenuItem22_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem22.Click

        If (mode = "LC"Then
            rtbDoc.SelectedText = "ü"
        Else
            rtbDoc.SelectedText = "ü".ToUpper()
       End If

     End Sub

    Private Sub toolStripMenuItem23_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem23.Click

         If (mode = "LC"Then
            rtbDoc.SelectedText = "ç"
        Else
            rtbDoc.SelectedText = "ç".ToUpper()
        End If

     End Sub

    Private Sub toolStripMenuItem24_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem24.Click

         If (mode = "LC"Then
            rtbDoc.SelectedText = "«"
        Else
            rtbDoc.SelectedText = "«".ToUpper()
        End If

    End Sub

    Private Sub toolStripMenuItem25_Click(ByVal sender As ObjectByVal e As 
    EventArgs) Handles toolStripMenuItem25.Click
        If (mode = "LC"Then
            rtbDoc.SelectedText = "»"
        Else
            rtbDoc.SelectedText = "»".ToUpper()
        End If

    End Sub

 #End Region

Summary

This article was intended to demonstrate a simple approach to adding a UI to a text editor that is intended to ease the insertion of special characters into the document. The example shows the insertion of upper or lower case French accent marks but one could use it to insert any special characters.


Similar Articles