Visual Basic Code of the Week (COTW)
Issue #132
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 2000 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Determining highlighted word in a Rich Text Box

Job Available at Microcomputer Consulting Group Inc. (MCG Inc)

If anyone is interested in a Visual Basic developer job in the New York Metro area please forward your resume to The brief requirements are 5+ years of Visual Basic development, must be able to solve any kind of development issue, deal with customers and have great communications skills. Very good English is essential since you will be interacting with clients on a daily basis. For some details about the company check their web site at MCG Inc offers very competitive salary with lots of benefits. It's a GREAT company to work for. If you do not meet the above requirements, please do not send your resume, you will not be considered for the job.


In this Issue

This issue introduces a routine to calculate the word that the mouse is hovering over in a rich text box control.

If you have any questions about using this module, let us know at


This module is useful for a variety of reasons. One great use is to allow a right-click to perform some particular action on that particular word (such as a spell check or deletion). It can also be used to change the formatting of the highlighted word.

There is also a bonus routine in this issue which determines if a character is AlphaNumeric. Our definition of alphanumeric is any letter from A to Z, any number from 0 to 9 or an underscore.


Public Function IsAlphaNumeric(sChar As String) As Boolean

Returns True if sChar is alpha numeric. Our definition is A-Z, 0-9 and _ (underscore).

Public Function MouseOverWord(oRichTextBox As RichTextBox, X As Single, Y As Single) As String

This routine does all the work. oRichTextBox is the rich text box control that is on the form. X and Y are the current mouse positions (such as what is returned in the MouseMove event of the RichTextBox. The MouseOverWord routine will return the alphanumeric word that the mouse is currently located over.

If there is no word the mouse is over it will return a blank string.

Sample Usage

This sample shows how to use the MouseOverWord routine. It assumes you have a form with a RichTextBox control on it.

Private Sub RTControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim sWord As String

    sWord = MouseOverWord(RTControl, X, Y)
    ' sWord will contain the "highlighted" word.
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945